[Tutor] question about mpmath product expression
Does anybody know if there is a precision difference when I use mpmath and take an expression: from mpmath import * mp.dps = 100 mu0 = [mpf('4') * pi * power(10, -7) rather then: mu0 = fprod([mpf('4'), pi, power(10, -7)]) ? Thanks, -- Bernd ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] reading binary files
Hi I am trying to read data from a file that has format item_name num_items item_type items eg TIME 1 0.0 DISTANCE 10 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 TIME 1 1.0 DISTANCE 10 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 I can read this if the data are in ASCII format using in_file = open("my_file.dat","r") data1 = in_file.read() tokens = data1.split() and then stepping through the resulting list but the data also appear in the same format in a binary file. I tried converting the binary file to an ASCII file using ifile = open("my_file.dat","rb") ofile = open("new_file.dat","w") base64.decode(ifile, ofile) but that gave the error "Error: Incorrect padding". I imagine that there is a straightforward way of doing this but haven't found it so far. Would be grateful for any suggestions! Thanks Alun Griffiths - Visit Pipex Business: The homepage for UK Small Businesses Go to http://www.pipex.co.uk/business-services ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] regex: not start with FOO
Hello, I'd like to match any line that does not start with FOO. (Using just a reg-ex rule) 1) What is the effective difference between: (?!^FOO).* ^(?!FOO).* 2) Is there a better way to do this? Thanks, :) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading binary files
wrote> I am trying to read data from a file that has format item_name num_items item_type items eg TIME 1 0.0 DISTANCE 10 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 Where is the item_type? I can read this if the data are in ASCII format using in_file = open("my_file.dat","r") data1 = in_file.read() tokens = data1.split() It might be easier to process line by line using readline or readlines rather than read but otherwise, ok so far... and then stepping through the resulting list but the data also appear in the same format in a binary file. When you say a binary file do you mean an ASCII file encoded into binary using some standard algorithm? Or do you mean the data is binary so that, for example, the number 1 would appear as 4 bytes? If so do you know how strings (the name) are delimited? Also how many could be present - is length a single or multiple bytes? and are the reors fixed length or variable? If variable what is the field/record separator? You may need to load the file into a hex editor of debugger to determine the answers... Having done that the struct module will allow you to read the data. You can see a basic example of using struct in my tutorial topic about handling files. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reading binary files
On Mon, 2009-02-02 at 11:31 +, etrade.griffi...@dsl.pipex.com wrote: > Hi > > I am trying to read data from a file that has format > > item_name num_items item_type items > > eg > > TIME 1 0.0 > DISTANCE 10 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 > TIME 1 1.0 > DISTANCE 10 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 > > I can read this if the data are in ASCII format using > > in_file = open("my_file.dat","r") > data1 = in_file.read() > tokens = data1.split() > > and then stepping through the resulting list but the data > also appear in the same format in a binary file. I tried > converting the binary file to an ASCII file using > > ifile = open("my_file.dat","rb") > ofile = open("new_file.dat","w") > base64.decode(ifile, ofile) > > but that gave the error "Error: Incorrect padding". I imagine > that there is a straightforward way of doing this but haven't > found it so far. Would be grateful for any suggestions! > > Thanks > > Alun Griffiths > Honestly I'm not sure what you're asking for but in general for reading binary data the I use the struct module. Check it out in the documentation. John Purser ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] newton's sqrt formula
# program to find square root square = input ('Please enter a number to be rooted, ') square = square * 1.0 guess = input('Please guess at the root, ') guess = guess * 1.0 newguess = 0. while guess**2 != square: # Newton's formula newguess = guess - (guess * guess - square) / (guess * 2) guess = newguess guess**2 - square print print print guess, ' is the square root of ', square print print print 'bye' Last month there was a square root program discussed. I wondered if the tide of my ignorance had receded enough that I could take a whack at messing with it. I offer this rewrite for your critique. Can it be terser, faster, prettier? Thank you. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] newton's sqrt formula
"WM." wrote square = input ('Please enter a number to be rooted, ') square = square * 1.0 Use raw_input() instead of input() and don't multiply by 1.0 - instead convert to float using float(): square = float( raw_input ('Please enter a number to be rooted, ')) guess = input('Please guess at the root, ') guess = guess * 1.0 newguess = 0. while guess**2 != square: # Newton's formula newguess = guess - (guess * guess - square) / (guess * 2) guess = newguess You could just combine these two guess = guess - (guess * guess - square) / (guess * 2) guess**2 - square That line does not do anything! print print print guess, ' is the square root of ', square print print print 'bye' Partly a style thing but I would prefer you either used triple quoted strings and format chars or inserted \n characters. ie either: print """ %s is the square root of %s bye""" % (guess, square) OR print "\n\n\n",guess," is the square root of", square,"\n\n\nbye!" Or combine both: print "\n\n\n%s is the square root of %s\n\n\nbye!" % (guess, square) Actually, if it was me I'd use two prints: print "\n\n\n%s is the square root of %s" % (guess, square) print"\n\n\nbye!" I offer this rewrite for your critique. Can it be terser, faster, prettier? Thank you. Not very critical but maybe it helps... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regex: not start with FOO
On Tue, Feb 3, 2009 at 9:46 AM, Bernard Rankin wrote: > Hello, > > > I'd like to match any line that does not start with FOO. (Using just a > reg-ex rule) > > 1) What is the effective difference between: > > (?!^FOO).* > > ^(?!FOO).* > > 2) Is there a better way to do this? > myline = 'FOO things in line' >>> myline.startswith('FOO') True Cheers, ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Installing python via ftp in virtual domain
I have a client who is hosting under virtual domain services that do not provide python. He has unlimited disk space available ( or so the hoster says) and they would allow installation of binaries in the virtual domain via ftp. It's a linux 'box' with a /private folder under the domain root. given that I have python 2.5 installed on my own desktop, which is also linux, could I expect to be able to install python and all libraries via FTP? If so, I'd welcome pointers, caveats and potentially issues. If it does not appear workable, I have alternatives, but the client wants me to try this initially. Thanks tim ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing python via ftp in virtual domain
2009/2/3 Tim Johnson : > I have a client who is hosting under virtual domain services that do not > provide python. > He has unlimited disk space available ( or so the hoster says) and they > would allow installation of binaries in the virtual domain via ftp. > > It's a linux 'box' with a /private folder under the domain root. given > that I have python 2.5 installed on my own desktop, which is also linux, > could I expect to be able to install python and all libraries via FTP? I guess the issues you'd face are: - different architecture (could be 64bit?) - wrong libraries It might be easier if you can build a statically-linked version of python -- although it appears that can have issues: http://bytes.com/groups/python/23235-build-static-python-executable-linux Or upload the python sources and build it there (do they provide gcc?). -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] newton's sqrt formula
On Mon, Feb 2, 2009 at 6:50 PM, WM. wrote: ># Newton's formula >newguess = guess - (guess * guess - square) / (guess * 2) >guess = newguess or guess = (guess + square/guess)/2 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing python via ftp in virtual domain
On Monday 02 February 2009, John Fouhy wrote: > 2009/2/3 Tim Johnson : > > I have a client who is hosting under virtual domain services that do not > > provide python. > > He has unlimited disk space available ( or so the hoster says) and they > > would allow installation of binaries in the virtual domain via ftp. > > > > It's a linux 'box' with a /private folder under the domain root. given > > that I have python 2.5 installed on my own desktop, which is also linux, > > could I expect to be able to install python and all libraries via FTP? > > I guess the issues you'd face are: > - different architecture (could be 64bit?) Let's say that they are using 32-bit architecture, then > - wrong libraries how do we resolve paths to libraries? tim - :-) who has other alternatives. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing python via ftp in virtual domain
2009/2/3 Tim Johnson : >> - wrong libraries > how do we resolve paths to libraries? Well, like I suggested, you could try building a staticly-linked version. Then it doesn't matter. Otherwise, I'm not sure. Maybe you could figure out what libraries you need, upload them, and play around with LD_LIBRARY_PATH. This is really a linux question, rather than a python question, though, so this may not be the best place to ask. (but see: http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html and http://blogs.sun.com/ali/entry/avoiding_ld_library_path_the ) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] newton's square root formula
# program to find square root square = float(raw_input ("Please enter a number to be rooted, ")) guess = input("Please guess at the root, ") i = 0 while guess**2 != square: i+=1 # Newton's formula guess = guess - (guess * guess - square) / (guess * 2) print i print "\n\n\n%s is the square root of %s" % (guess, square) print "\n%s loops were run." % (i) print "\n\n\nbye" # Here is my program, enhanced by Alan's good advice. The reason I wanted to re-write the other program was, it had a limited number of loops and I felt accuracy should be the measure. So, just now, I added a loop counter here and found that the formula is a little buggy. Make 'square = 7' and you will be in the 'i = 500' area before you can find ControlC. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regex: not start with FOO
> > I'd like to match any line that does not start with FOO. (Using just a > > reg-ex > rule) > > > > 1) What is the effective difference between: > > > > (?!^FOO).* > > > > ^(?!FOO).* > > > > 2) Is there a better way to do this? > > > > myline = 'FOO things in line' > > >>> myline.startswith('FOO') > True Right. However, I am trying to just do this in a general "does this match" regex environment. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Alarm Clock (suggestions please)
Hi All, As I am getting a little older, I sometimes take small naps to recharge my batteries:) Is this while loop OK? What kind of error checking should I use? thanks -david #!/usr/bin/python import time import subprocess import sys doit = 1 alarmhour = int(raw_input("Please enter the hour. (24h): ")) alarmmin = int(raw_input("Please enter the minute. (01-59) ")) while(doit): mytime = list(time.localtime()) hour = mytime[3] minute = mytime[4] if hour == alarmhour and minute == alarmmin: subprocess.call('mplayer -loop 9 ring.wav', shell=True) sys.exit() doit = 0 -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Alarm Clock (suggestions please)
2009/2/3 David : > while(doit): >mytime = list(time.localtime()) >hour = mytime[3] >minute = mytime[4] >if hour == alarmhour and minute == alarmmin: >subprocess.call('mplayer -loop 9 ring.wav', shell=True) >sys.exit() Hi David, What you've written here is called a Busy Wait -- essentially, your program will be checking the local time as fast as it possibly can, which could be hundreds or thousands of times per second. It will cause your CPU (or, at least, the core python is on) to run at 100%. I imagine you don't actually care if your alarm is a few milliseconds late, so you could add a call to time.sleep(1) to the body of the loop. This will cause python to sleep for one second every iteration, thus allowing other programs to get some CPU time, and saving your power bill. (in fact, since you're only specifying hour and minute for your alarm, you may as well sleep for 60 seconds each iteration) Also, you could just write while(True). Your control variable doit isn't actually doing anything in this program. Regarding error checking, you could do tests to make sure the hour and minutes are in-range. Perhaps you could add code to check whether the alarm time is more than a certain number of hours in the future. Depends how complex you want to make it. (you could also inspect sys.argv -- this would allow you to specify the time on the command line, rather than requiring user input) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor