Re: [Tutor] creating the equivalent of string.strip()
"Christopher Spears" <[EMAIL PROTECTED]> wrote > I decided to post a solution to this problem that uses > regular expressions. > > def my_strip(s): >remove_leading = re.sub(r'^\s+','',s) >remove_trailing = > re.sub(r'\s+$','',remove_leading) >new_s = remove_trailing >return new_s You can replace the last two lines with return remove_trailing There is no need for new_s (In fact you could just return the last sub result and dispense with remove_training too!) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Controlling MS Windows programs?
"Fast Primes" <[EMAIL PROTECTED]> wrote > I am looking for ways to control multiple Windows programs > from Python, such that I can enter data via Python and have > that data passed to an underlying third party program's GUI > screen, ... > Is it possible to come at this in a more straight forward way > than having to concern oneself with COM and the related > Windows internals? COM *is* the straightforward way, that is what COM is for. If you can't use COM you have to go through the far less reliable route of "screen scraping" the GUI. This involves passing windows messages to the target application to simulate user events. Look at the PostMessage function in the Win32 API (Check it on MSDN) as an example. Getting data back out can be even more tricky unless you can get the "handle" of the widgets concerned. That can usually be done with a combination of FindWindow() and GetChildWindows() calls You can access the API functions via ctypes. But COM is actually easier in most cases Windows was not designed for scripting! Thats why DDE/OLE/COM was developed. 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] matching a street address with regular expressions
"Christopher Spears" <[EMAIL PROTECTED]> wrote > create a regular expression that will match a street > address. Here is one of my attempts. > > Obviously, I can just create a pattern "\d+ \w+ \w+". > However, the pattern would be useless if I had a > street name like 3120 De la Cruz Boulevard. Any > hints? Yes, don't go too far. Matching addresses is a fantastically difficult thing to do perfectly. There are several companies whose only product is software to match and check addresses and they sell it for hundreds of thousands of dollars a license! They spend years developing and improving it. Our suppllier is up to version 16 and they only release new versions every 2 years... Of course they are matching formats from countries around the world and providing GUI management consoles and MIS reports etc etc. But the core function of correctly identifying valid addresses remains a hugely complex task. Its a good idea to try just enough of it to realise how hard it is. Then stop. :-) 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] Timers in Python
On 10/4/07, Kamal <[EMAIL PROTECTED]> wrote: > Is there a method in Python which does what > setInterval('someFunction()',5000) does in Javascript. > > Basically, I want to call functionOne() every x minutes, and wondering > whats the best way to do it. Yes, the time module in the standard library has sleep(x), where x is in seconds. The Python Library Reference, section 14.2, goes into a bit more detail. -Rob A. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Timers in Python
"Rob Andrews" <[EMAIL PROTECTED]> wrote >> Is there a method in Python which does what >> setInterval('someFunction()',5000) does in Javascript. >> >> Basically, I want to call functionOne() every x minutes, and >> wondering >> whats the best way to do it. > > Yes, the time module in the standard library has sleep(x), where x > is > in seconds. Unfortunately sleep blocks the program so you may need to wrap it in a thread to launch the required function at the required time without blocking the main program. There probably is a third party module that will do this but I don't know of any. If its within a GUI context then I think both Tkinter and wxPython have timer facilities although I've never used them. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Timers in Python
Alan Gauld wrote: > If its within a GUI context then I think both Tkinter and wxPython > have timer facilities although I've never used them. Tkinter: http://www.pythonware.com/library/tkinter/introduction/x9507-alarm-handlers-and-other.htm ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Timers in Python
Kamal wrote: > hello everyone, > > Is there a method in Python which does what > setInterval('someFunction()',5000) does in Javascript. > > Basically, I want to call functionOne() every x minutes, and wondering > whats the best way to do it. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65222 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Timers in Python
On 04/10/2007, Kamal <[EMAIL PROTECTED]> wrote: > Basically, I want to call functionOne() every x minutes, and wondering > whats the best way to do it. If you need to run the functions concurrently, use threads. Else you can setup a simple signal-handler for SIGALRM and set the time accordingly: import signal def setup_signal(): # Wait 5 seconds before alarming signal.alarm(5) signal.signal(signal.SIGALRM, signal_handler) def signal_handler(signum, frame): print "I got an alarm!" # need to resetup the signal setup_signal() setup_signal() # A pointless loop for x in xrange(5): if (x % 10) == 0: print x -- - Rikard - http://bos.hack.org/cv/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] getting a result from an if condition
Hi Everybody, I'm having some problems with get an if block to work. import re regex=re.compile('(some expression)') # What I'm trying to get to work if (m=regex.search('some long string')): print m.groups() - The thing that isn't working is the m=regex in the if line. Is this possible in python? Thanks, Tino ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] getting a result from an if condition
Tino Dai wrote: > Hi Everybody, > > I'm having some problems with get an if block to work. > > import re > > regex=re.compile('(some expression)') > > # What I'm trying to get to work > if (m=regex.search('some long string')): > print m.groups() > > - The thing that isn't working is the m=regex in the if line. Is this > possible in python? No. Assignment in Python is a statement, not an expression. It does not have a value and it can't be used in a conditional. Use m=regex.search('some long string') if (m): print m.groups() Kents ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Timers in Python
Kent Johnson wrote: > Kamal wrote: >> hello everyone, >> >> Is there a method in Python which does what >> setInterval('someFunction()',5000) does in Javascript. >> >> Basically, I want to call functionOne() every x minutes, and wondering >> whats the best way to do it. You can also look at the sched module that comes with the standard distribution. -- ~noufal http://nibrahim.net.in/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] using python to execute from Dir A in Dir B
Hi All, lets say I am in Dir A (out of my control because I have submitted a job to a queuing system) and I have a python script which is running in this directory - the one I submitted to the queue what I need to do is have my python script run another executable, but it must do it from a directory different from the one I am in, it needs to run in the /scratch/ directory (again not my choice) Is this possible, and is it easy? the way I have been using python to run other executables is os.system('path/my.executable input') but this dumps the output in the directory I am currently in, and not the directory the my.executable lives in - I am hoping there is a simple way to tell python to run the executable in its directory, and have its output live there as well. If I were using CSH, I could do all this very simply by having these lines in my script ### .csh file cd /scratch my_exe.csh I am hoping this idea makes sense and is simple with python... Thanks, Andre ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using python to execute from Dir A in Dir B
Andre Walker-Loud wrote: > Hi All, > > lets say I am in Dir A (out of my control because I have submitted a > job to a queuing system) > > and I have a python script which is running in this directory - the > one I submitted to the queue > > what I need to do is have my python script run another executable, > but it must do it from a directory different from the one I am in, it > needs to run in the /scratch/ directory (again not my choice) > > Is this possible, and is it easy? Use subprocess.Popen() with the cwd argument. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using python to execute from Dir A in Dir B
On Thu, Oct 04, 2007 at 03:15:41PM -0400, Andre Walker-Loud wrote: > what I need to do is have my python script run another executable, > but it must do it from a directory different from the one I am in, it > needs to run in the /scratch/ directory (again not my choice) > > Is this possible, and is it easy? > > the way I have been using python to run other executables is > > os.system('path/my.executable input') Have a look at the subprocess module. You should be able to do something like: subprocess.Popen(['path/my.executable'], cwd='path') I am not sure of the actual keyword to be used, so please look at the docs... Tiago. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to do histogram
Dear all- I want to get a histogram. And I did the following. from pylab import * x=(1,1,2,2,2,2,3,4) hist(x) Then I get (array([2, 0, 0, 4, 0, 0, 1, 0, 0, 1]), array([ 1. , 1.3, 1.6, 1.9, 2.2, 2.5, 2.8, 3.1, 3.4, 3.7]), ) But actually I want to get a picture. What's the problem? What should I do? Thanks. Fangwen - Building a website is a piece of cake. Yahoo! Small Business gives you all the tools to get online.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to do histogram
On 04/10/2007, Fangwen Lu <[EMAIL PROTECTED]> wrote: > What's the problem? We have no idea. Perhaps you could give us some info of what errors the Python-process returns. Things will be easier to help out that way. -- - Rikard - http://bos.hack.org/cv/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using python to execute from Dir A in Dir B
"Andre Walker-Loud" <[EMAIL PROTECTED]> wrote > If I were using CSH, I could do all this very simply by having these > lines in my script > > ### .csh file > > cd /scratch > my_exe.csh The best answer is to use subprocess as Kent suggested but you can also use os.chdir(path) before using os.system() But system() is deprecated in favour of the subprocess module. Of course you could also modify your script to take a path as a command line argument and use that to direct the output explicitly... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to do histogram
Fangwen Lu wrote: > Dear all- > > I want to get a histogram. And I did the following. > from pylab import * > x=(1,1,2,2,2,2,3,4) > hist(x) > > Then I get > (array([2, 0, 0, 4, 0, 0, 1, 0, 0, 1]), array([ 1. , 1.3, 1.6, 1.9, > 2.2, 2.5, 2.8, 3.1, 3.4, 3.7]), ) > > But actually I want to get a picture. > What's the problem? What should I do? Did you call show() ? There is a histogram example here: http://matplotlib.sourceforge.net/screenshots.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] another quickie
hello all, im sorry but i might be asking a lot of Tkinter questions for a bit. im still working on my first GUI, it is very simple all i want it to do is open a text file, write to it, than save it. so far i have a GUI with the ability to right text (nothing amazing), but i don't know how to have it display the text from a text file any help would be great from Tkinter import * top=Tk() F=Frame(top) F.pack() F.txtBox=Text(top) F.txtBox.pack() F.pack() this is all i have so far ^_^" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] matching a street address with regular expressions
Christopher Spears wrote: > One of the exercises in Core Python Programming is to > create a regular expression that will match a street > address. Here is one of my attempts. > street = "1180 Bordeaux Drive" patt = "\d+ \w+" import re m = re.match(patt, street) if m is not None: m.group() > ... > '1180 Bordeaux' > > Obviously, I can just create a pattern "\d+ \w+ \w+". > However, the pattern would be useless if I had a > street name like 3120 De la Cruz Boulevard. Any > hints? > Maybe : r'^(\d+)\s+(.*?)(?:\s+)?(\d+.*)?$' street = "1180 Bordeaux Drive 5th floor apt 'A'" then : \1 : '1180' \2 : 'Bordeaux Drive' \3 : "5th floor apt 'A'" or : street = "1180 Bordeaux Drive" then : \1 : '1180' \2 : 'Bordeaux Drive' \3 : "" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] random number generator
Hello, I am writing a little program to test a theory and as part of teaching myself Python. I've only been at this about a week now. I have a program that "should" work but doesn't. It generates a random number between 1 and 2 out to 10 decimal places. I think there is something wrong with how my random number is generated or defined or how my guesses are defined. I added a line to tell me what the random number is and then if I enter it as a guess it doesn't match and exit the loop. Any idea what I'm doing wrong? Here is a sample output: --- I'm thinking out to 10 decimal places. Good luck. 1.14981949962 Make a guess: 1.14981949962 Higher... Make another guess: 1.14981949963 Lower... 1.14981949963 Make another guess: --- Here is my code: --- # Number guessing game # # The computer will choose a number between 1 and 2 (to ten decimal places) # and the player will try to guess the number. The program will tell the # player the number is either higher or lower than the number they guessed. import random import os os.system("clear") print "\nWelcome to 'Guess My Number'!" print "\nI'm thinking of a number between 1 and 2." print "\nYes, that's right. Between 1 and 2." print "\nYou have heard of decimals right? Well, I'm" print "\nthinking out to 10 decimal places. Good luck.\n" # set random value random.seed() number = random.random() + 1 print number guess = float(raw_input("Make a guess: ")) tries = 1 # the guess loop while (guess != number): if (guess > number): print "Lower..." else: print "Higher..." guess = float(raw_input("Make another guess: ")) tries += 1 print "Congratulations! You guessed my number! The number was", number print "It took you only", tries, "tries!\n" # end --- Thanks, Jim -- Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] another quickie
On 10/4/07, max baseman <[EMAIL PROTECTED]> wrote: > hello all, im sorry but i might be asking a lot of Tkinter questions > for a bit. > im still working on my first GUI, it is very simple all i want it to > do is open a text file, write to it, than save it. so far i have a > GUI with the ability to right text (nothing amazing), but i don't > know how to have it display the text from a text file any help would > be great The book, Python Programming 3E by Mark Lutz has over 300 pages of Tkinter tutorial in it, covering just about anything you'd want to do with Tkinter. I've seen it "used" on the Internet for a little over $10. It retails for $60 (Over 1500 pages of pure Python programming). It is an O'Reilly book, and they have the code examples for the book on their site at: http://examples.oreilly.com/python3/ Lutz starts off simple, and gets as complicated as you can stand. Bite off small pieces, and chew well. > > from Tkinter import * > top=Tk() > F=Frame(top) > F.pack() > F.txtBox=Text(top) > F.txtBox.pack() > F.pack() > > this is all i have so far ^_^" Lutz develops a full-blown text-editor, using Tkinter, in PP3E. Happy Programming! -- b h a a l u u at g m a i l dot c o m http://www.geocities.com/ek.bhaaluu/index.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using python to execute from Dir A in Dir B
Thank you everyone for the help. I have two solutions, but I would love one that uses the subprocess.Popen() - I have no experience with this module.class - if someone with more experience would feel inclined to provide an example, I would be very much appreciative. SOLUTION 1 (my brute force method) create a csh file which does what I want, # filename: hack.csh #!/bin/csh set RUN_DIR = $1 set EXEC_DIR = $2 cd ${RUN_DIR} ${EXEC_DIR}/my.exe # then I call this in my python code from some arbitrary directory # python script... os.system('./hack.csh /scratch exec_dir') # SOLUTION 2 (courtesy of my computer-superhero friend - again simple) in my python script... # import os curdir = os.path.abspath('.') # in case I need to get back to where I am - in my case no os.chdir('RUN_DIR') # in my case RUN_DIR = /scratch if I wanted to get back to my old directory - then add os.chdir(curdir) # so both of these methods are sort of brute force - being completely unfamiliar with the subprocess module, again is someone would like to provide an example, or at least more hints than 'you should use subprocess.Popen()' I thank you in advance. Cheers, Andre On Oct 4, 2007, at 5:13 PM, Alan Gauld wrote: > "Andre Walker-Loud" <[EMAIL PROTECTED]> wrote > >> If I were using CSH, I could do all this very simply by having these >> lines in my script >> >> ### .csh file >> >> cd /scratch >> my_exe.csh > > The best answer is to use subprocess as Kent suggested > but you can also use os.chdir(path) before using os.system() > But system() is deprecated in favour of the subprocess module. > > Of course you could also modify your script to take a > path as a command line argument and use that to direct > the output explicitly... > > Alan G. > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] random number generator
I'm no Python wizard, I'm still learning myself. But I think you need another "if" statement to check if "guess" is equal to "number". if guess == number: print "Congratulations!" Something like that. On 10/4/07, Jim Hutchinson <[EMAIL PROTECTED]> wrote: > Hello, > > I am writing a little program to test a theory and as part of teaching > myself Python. I've only been at this about a week now. I have a > program that "should" work but doesn't. It generates a random number > between 1 and 2 out to 10 decimal places. I think there is something > wrong with how my random number is generated or defined or how my > guesses are defined. I added a line to tell me what the random number > is and then if I enter it as a guess it doesn't match and exit the > loop. Any idea what I'm doing wrong? Here is a sample output: > > --- > I'm thinking out to 10 decimal places. Good luck. > > 1.14981949962 > Make a guess: 1.14981949962 > Higher... > Make another guess: 1.14981949963 > Lower... > 1.14981949963 > Make another guess: > --- > > Here is my code: > > --- > # Number guessing game > # > # The computer will choose a number between 1 and 2 (to ten decimal places) > # and the player will try to guess the number. The program will tell the > # player the number is either higher or lower than the number they guessed. > import random > import os > os.system("clear") > print "\nWelcome to 'Guess My Number'!" > print "\nI'm thinking of a number between 1 and 2." > print "\nYes, that's right. Between 1 and 2." > print "\nYou have heard of decimals right? Well, I'm" > print "\nthinking out to 10 decimal places. Good luck.\n" > # set random value > random.seed() > number = random.random() + 1 > print number > guess = float(raw_input("Make a guess: ")) > tries = 1 > # the guess loop > while (guess != number): > if (guess > number): > print "Lower..." > else: > print "Higher..." > guess = float(raw_input("Make another guess: ")) > tries += 1 > print "Congratulations! You guessed my number! The number was", number > print "It took you only", tries, "tries!\n" > # end > --- > > Thanks, > Jim > > -- > Please avoid sending me Word or PowerPoint attachments. > See http://www.gnu.org/philosophy/no-word-attachments.html > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- Jesus is coming!Rev. 1:7 The Bottom LineJohn 3:3-7 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] random number generator
On Thu, 4 Oct 2007, Jim Hutchinson wrote: > Any idea what I'm doing wrong? > while (guess != number): This is your problem. Like all^h^h^h most numbers in computing, floating point numbers are stored in binary. They only approximate the decimal values they print out as. Two numbers can print as the same value but actually have different values. If you want to see if the numbers are identical to 10 decimal places, use this instead: while (abs(guess-number) > 0.01): ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] random number generator
On Thu, 4 Oct 2007, Jerry VanBrimmer wrote: > I'm no Python wizard, I'm still learning myself. But I think you need > another "if" statement to check if "guess" is equal to "number". > > if guess == number: > print "Congratulations!" No, he's got the equivalent function in his while statement: while (guess != number): The idea is to stay in the loop as long as they're UNequal, and then drop out to the "Congratulations" part. But comparing floats as equal or not equal is never a very robust idea. http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] random number generator
I need to start using the reply all button... Andrew James wrote: > while guess != number: >guess = float(raw_input("Make another guess: ")) >if guess > number: >print "Lower..." >elif guess < number: >print "Higher..." >tries += 1 > > You're asking people to change their choice before the while loop > ends. Now this looks a little ugly as it will be asking people to make > two guesses right off the bat, or it'll use the term "another" for > their first guess. Shouldn't be too hard to change. Or do this. > > while guess != number: >if guess > number: >print "Lower..." >elif guess < number: >print "Higher..." >tries += 1 >if guess != number: >guess = float(raw_input("Make another guess: ")) > > > > > Jerry VanBrimmer wrote: >> I'm no Python wizard, I'm still learning myself. But I think you need >> another "if" statement to check if "guess" is equal to "number". >> >> if guess == number: >> print "Congratulations!" >> >> >> Something like that. >> >> >> >> On 10/4/07, Jim Hutchinson <[EMAIL PROTECTED]> wrote: >> >>> Hello, >>> >>> I am writing a little program to test a theory and as part of teaching >>> myself Python. I've only been at this about a week now. I have a >>> program that "should" work but doesn't. It generates a random number >>> between 1 and 2 out to 10 decimal places. I think there is something >>> wrong with how my random number is generated or defined or how my >>> guesses are defined. I added a line to tell me what the random number >>> is and then if I enter it as a guess it doesn't match and exit the >>> loop. Any idea what I'm doing wrong? Here is a sample output: >>> >>> --- >>> I'm thinking out to 10 decimal places. Good luck. >>> >>> 1.14981949962 >>> Make a guess: 1.14981949962 >>> Higher... >>> Make another guess: 1.14981949963 >>> Lower... >>> 1.14981949963 >>> Make another guess: >>> --- >>> >>> Here is my code: >>> >>> --- >>> # Number guessing game >>> # >>> # The computer will choose a number between 1 and 2 (to ten decimal >>> places) >>> # and the player will try to guess the number. The program will tell >>> the >>> # player the number is either higher or lower than the number they >>> guessed. >>> import random >>> import os >>> os.system("clear") >>> print "\nWelcome to 'Guess My Number'!" >>> print "\nI'm thinking of a number between 1 and 2." >>> print "\nYes, that's right. Between 1 and 2." >>> print "\nYou have heard of decimals right? Well, I'm" >>> print "\nthinking out to 10 decimal places. Good luck.\n" >>> # set random value >>> random.seed() >>> number = random.random() + 1 >>> print number >>> guess = float(raw_input("Make a guess: ")) >>> tries = 1 >>> # the guess loop >>> while (guess != number): >>> if (guess > number): >>> print "Lower..." >>> else: >>> print "Higher..." >>> guess = float(raw_input("Make another guess: ")) >>> tries += 1 >>> print "Congratulations! You guessed my number! The number was", number >>> print "It took you only", tries, "tries!\n" >>> # end >>> --- >>> >>> Thanks, >>> Jim >>> >>> -- >>> Please avoid sending me Word or PowerPoint attachments. >>> See http://www.gnu.org/philosophy/no-word-attachments.html >>> ___ >>> Tutor maillist - Tutor@python.org >>> http://mail.python.org/mailman/listinfo/tutor >>> >>> >> >> >> > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor