Re: [Tutor] [tutor] threading problem in GUI
Great :) Just to be clear about that: you can see the "connect" line as a dynamic registration process (with the symetric disconnect operation available via ... "disconnect"). The only thing you need is to connect (at runtime) the even before it's called (obvious isn't it ? ;) ), but you have no other constraint. You can even choose to connect/disconnect events when they are used/unused ... You can also connect a single event many times (to different functions) or you can connect many events to the same function. This is the "power" of this system :) Like now if you want to log your strings on a file, you just define the function writing in the file and connect the event to this function: you'll have both the GUI and the log-file outputs ! This is, AFAIK, the best existing implementation for user interfaces ! Pierre nephish a écrit : > Pierre Barbier de Reuille wrote: > >> nephish a écrit : >> >> >>> one more thing. >>> if i uncomment the lines >>> gtk.threads_enter() >>> and >>> gtk.threads_leave() >>> the whole thing locks up when the function is called. >>> the gui, and the thread both lock up. >>> >> >> >> Well, that's just normal. However, what you should do is to send a >> signal from your thread with the text to append in your textbuffer. >> Then, you catch the signal in your main widget to show it ! >> >> To emit a signal use : >> >>gtk.gdk.threads_enter() >>self.emit("writing", str) >>gtk.gdk.threads_leave() >> >> To catch it: >> >> >>emitting_object.connect("writing", self.method_handling_to_signal) >> >> Well, I used that and it just work ! Using the signal does not solve any >> threading problem but allow you to separate between the event and the >> answer to the event, which is a good habit to take ! >> >> Pierre >> >> >> >> > I finally got it working !! > thanks so much for all your help > i would not have gotten it working without you, > (or this mailing list) > > God bless > shawn > -- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77fax : (33) 4 67 61 56 68 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Emulate jetdirect
Hi All, I just would make some kind of printer filter for an HP printer, that is connected to a jetdirect printserver. The functionality is something like this: netcat -l -p 9100 | filter.py | netcat 10.36.11.11 9100 I would like to make it as a simple socketserver in python for the port 9100, reading from the socket and sending it to the real jetdirect printserver over another socket. So it is kind of printer proxy server. The filter would make some text processing with regular expressions on each line. Another wish to use it as an intelligent printer, that can send the printout to the e-mail address, that is somewhere in the printout. To send order response via a printing. netcat -l -p 9100 | senditasemail.py Can someone show me any example to start it ? János ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [tutor] threading problem in GUI
Pierre Barbier de Reuille wrote: >Great :) > >Just to be clear about that: you can see the "connect" line as a dynamic >registration process (with the symetric disconnect operation available >via ... "disconnect"). The only thing you need is to connect (at >runtime) the even before it's called (obvious isn't it ? ;) ), but you >have no other constraint. You can even choose to connect/disconnect >events when they are used/unused ... You can also connect a single event >many times (to different functions) or you can connect many events to >the same function. This is the "power" of this system :) Like now if you >want to log your strings on a file, you just define the function writing >in the file and connect the event to this function: you'll have both the >GUI and the log-file outputs ! > >This is, AFAIK, the best existing implementation for user interfaces ! > >Pierre > >nephish a écrit : > > >>Pierre Barbier de Reuille wrote: >> >> >> >>>nephish a écrit : >>> >>> >>> >>> one more thing. if i uncomment the lines gtk.threads_enter() and gtk.threads_leave() the whole thing locks up when the function is called. the gui, and the thread both lock up. >>>Well, that's just normal. However, what you should do is to send a >>>signal from your thread with the text to append in your textbuffer. >>>Then, you catch the signal in your main widget to show it ! >>> >>>To emit a signal use : >>> >>> gtk.gdk.threads_enter() >>> self.emit("writing", str) >>> gtk.gdk.threads_leave() >>> >>>To catch it: >>> >>> >>> emitting_object.connect("writing", self.method_handling_to_signal) >>> >>>Well, I used that and it just work ! Using the signal does not solve any >>>threading problem but allow you to separate between the event and the >>>answer to the event, which is a good habit to take ! >>> >>>Pierre >>> >>> >>> >>> >>> >>> >>I finally got it working !! >>thanks so much for all your help >>i would not have gotten it working without you, >>(or this mailing list) >> >>God bless >>shawn >> >> >> > > > indeed. i really want to get into this stuff more. i think a whole lot of what i have been doing could really be done easier and cleaner. thanks for everything, shawn ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] python strings?
below is my program. what i would like to do is to get the last line to print the number of years that the user enters and also the principal # A program to compute the value of an investment # years into the future def main(): print "This program calculates the future value of an investment over years" principal = input("Enter the initial principal: ") apr = input("Enter the annual interest rate: ") years = input("Enter the number of years: ") for i in range(10): principal = principal * (1 + apr) print "The value in years is", principal main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Making Applets
Alan G. wrote: <<>> <<>> Thanks, Alan. I'll give it a look. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] input/output redirection
Title: Message Hi List, I wanted to take the stdout of a process and redirect it to stdin of a python script, then after playing with the input in the script I want to send it back to stdout (all of this to be done while the original process runs happily along). I can't seem to figure out the correct syntax if this is possible. e.g. C:\> runner.exe | script.py C:\> runner.exe > script.py # this overwrites your script!:-) e.g. #!/usr/bin/env python import re while 1: packet = sys.stdin.read() if field in packet: # change it and put it back on the command window and get the next bunch of stuff sys.stdout.write() I hope my question/intention is clear. Thanks to the list! This is the best list I've ever belonged to. There are a lot of knowledgeable people who help, and no one ever flames you for asking questions out of ignorance. Thanks, Frank ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] input/output redirection
Lane, Frank L wrote: > Hi List, > > I wanted to take the stdout of a process and redirect it to stdin of a > python script, then after playing with the input in the script I want to > send it back to stdout (all of this to be done while the original > process runs happily along). I can't seem to figure out the correct > syntax if this is possible. > > e.g. > > C:\> runner.exe | script.py > C:\> runner.exe > script.py # this overwrites your script!:-) > > e.g. > > #!/usr/bin/env python > import re > > while 1: > packet = sys.stdin.read() In the previous line, all input is read which is not ideal. Depending on the input a single line or part of it should be read so that the script can start working before runner.exe finishes. Now, once you have a workable chunck of the input, you can happily process it and write it out. > if field in packet: > # change it and put it back on the command window and get the > next bunch of stuff > sys.stdout.write() In the previous line, write is called with no arguments. It should recieve the string to output. > I hope my question/intention is clear. The given code is not a working example. It would help if the code was more complete. Javier ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python strings?
On Thu, 2005-09-08 at 15:15 +0100, [EMAIL PROTECTED] wrote: > for i in range(10): > principal = principal * (1 + apr) To calculate compound interest, you in fact don't need to use a loop at all (you could use: 'final = principal * (apr ** years)') but if you really want to use a loop, I would use a while loop (although this may not be the best way involving loops, my loop-fu isn't great). The while loop would look like: while years: principal = principal * (1 + apr) years = years - 1 If you planned to use fractions of years, then you need to tweak this. > print "The value in years is", principal To actually answer your question, in order to print this correctly, you need to tell Python to convert the integer (principal) to a string by using the built-in function str() like so: print "The value in years is", str(principal) To include the years, you would just add 'str(years)' in an appropriate position, although if you've used the while loop, then referring to years will simply give you 0, as you've been adjusting it. The solution to this is to assign 'years' to a different variable initially (and use one in the loop and the other here) in order to preserve it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] input/output redirection
On Thu, 8 Sep 2005, Lane, Frank L wrote: > I wanted to take the stdout of a process and redirect it to stdin of a > python script, then after playing with the input in the script I want to > send it back to stdout (all of this to be done while the original > process runs happily along). I can't seem to figure out the correct > syntax if this is possible. > > e.g. > > C:\> runner.exe | script.py Hi Frank, You've almost got it. The syntax above should have worked. But the problem here is that you're running into a problem with the way Windows runs Python scripts. Alan Gauld ran into this a few weeks ago: http://mail.python.org/pipermail/tutor/2005-August/041019.html You may need to put a CMD wrapper around the Python script. Apparently, this makes Windows pleased enough to let it be used as part of a shell pipeline. The thread above should mention the workaround to get things working. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Substring substitution
Hello, I have a string, and I use a regular expression to search a match in it. When I find one, I would like to break down the string, using the matched part of it, to be able to perform some formatting and to later build a brand new string with the separate parts. The regular expression part works ok, but my problem is to extract the matched pattern from the string. I'm not sure how to do that... sString = 'mt_03_04_04_anim' # Create regular expression object oRe = re.compile( "\d\d_\d\d\_\d\d" ) # Break-up path aString = sString.split( os.sep ) # Iterate individual components for i in range( 0, len( aString ) ): sSubString = aString[i] # Search with shot number of 2 digits oMatch = oRe.search( sSubString ) if oMatch != None: # Replace last sequence of two digits by 3 digits!! Any suggestion would be welcomed. Thanks! Bernard ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] input/output redirection
* Lane, Frank L <[EMAIL PROTECTED]> [2005-09-08 09:49]: > Hi List, > > I wanted to take the stdout of a process and redirect it to stdin of a > python script, then after playing with the input in the script I want to > send it back to stdout (all of this to be done while the original > process runs happily along). I can't seem to figure out the correct > syntax if this is possible. > > e.g. > > C:\> runner.exe | script.py > C:\> runner.exe > script.py # this overwrites your script!:-) I use fileinput for most of my stdin text processing. It also give me the flexibility of using it as a pipe OR assigning a wordlist of files to the script instead. http://www.python.org/doc/2.4.1/lib/module-fileinput.html import fileinput for line in fileinput.input(): process(line) -- David Rock [EMAIL PROTECTED] pgp3RtUbUyLUC.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Substring substitution
Bernard Lebel wrote: > Hello, > > I have a string, and I use a regular expression to search a match in > it. When I find one, I would like to break down the string, using the > matched part of it, to be able to perform some formatting and to later > build a brand new string with the separate parts. > > The regular expression part works ok, but my problem is to extract the > matched pattern from the string. I'm not sure how to do that... > > > sString = 'mt_03_04_04_anim' > > # Create regular expression object > oRe = re.compile( "\d\d_\d\d\_\d\d" ) > > # Break-up path > aString = sString.split( os.sep ) > > # Iterate individual components > for i in range( 0, len( aString ) ): > > sSubString = aString[i] > > # Search with shot number of 2 digits > oMatch = oRe.search( sSubString ) > > if oMatch != None: > # Replace last sequence of two digits by 3 digits!! Hi Bernard, It sounds like you need to put some groups into your regex and use re.sub(). By putting groups in the regex you can refer to pieces of the match. For example >>> import re >>> s = 'mt_03_04_04_anim' >>> oRe = re.compile( "(\d\d_\d\d\_)(\d\d)" ) >>> m = oRe.search(s) >>> m.group(1) '03_04_' >>> m.group(2) '04' With re.sub(), you provide a replacement pattern that can refer to the groups from the match pattern. So to insert new characters between the groups is easy: >>> oRe.sub(r'\1XX\2', s) 'mt_03_04_XX04_anim' This may be enough power to do what you want, I'm not sure from your description. But re.sub() has another trick up its sleeve - the replacement 'expression' can be a callable which is passed the match object and returns the string to replace it with. For example, if you wanted to find all the two digit numbers in a string and add one to them, you could do it like this: >>> def incMatch(m): ... s = m.group(0) # use the whole match ... return str(int(s)+1).zfill(2) ... >>> re.sub(r'\d\d', incMatch, '01_09_23') '02_10_24' This capability can be used to do complicated replacements. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python strings?
>> print "The value in years is", principal > To actually answer your question, in order to print this correctly, > you > need to tell Python to convert the integer (principal) to a string > by > using the built-in function str() like so: Not so. print automatically calls str() on all its arguments so the programmer doesn't need to. An alternative way to solve your probnlem is to use format strings: print "Years = %s;\tPrincipal = %s" % (years,principal) HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python strings?
On Thu, 2005-09-08 at 21:00 +0100, Alan G wrote: > Not so. print automatically calls str() on all its arguments so > the programmer doesn't need to. So it does. I think I was trying to concatenate, which it doesn't like. Sorry if I caused any confusion. Dan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Substring substitution
Hi Kent, This is nice! There is one thing though. When I run the oRe.sub() call, I get an error: Traceback (most recent call last): File "", line 1, in ? File "\\Linuxserver\prod\XSI\WORKGROUP_4.0\Data\Scripts\pipeline\filesystem\bb_processshotdigits.py", line 63, in ? processPath( r'C:\temp\MT_03_03_03\allo.txt', False ) File "\\Linuxserver\prod\XSI\WORKGROUP_4.0\Data\Scripts\pipeline\filesystem\bb_processshotdigits.py", line 45, in processPath else: sSubString = matchShot( sSubString ) File "\\Linuxserver\prod\XSI\WORKGROUP_4.0\Data\Scripts\pipeline\filesystem\bb_processshotdigits.py", line 11, in matchShot sNewString = oRe.sub( r'\10\2', sSubString ) File "D:\Python24\Lib\sre.py", line 260, in filter return sre_parse.expand_template(template, match) File "D:\Python24\Lib\sre_parse.py", line 781, in expand_template raise error, "invalid group reference" sre_constants.error: invalid group reference This is my new match function: def matchShot( sSubString ): # Create regular expression object oRe = re.compile( "(\d\d_\d\d\_)(\d\d)" ) oMatch = oRe.search( sSubString ) if oMatch != None: sNewString = oRe.sub( r'\10\2', sSubString ) return sNewString else: return sSubString I have read the sub() documentation entry but I have to confess that made things more confusing for me... Thanks again Bernard On 9/8/05, Kent Johnson <[EMAIL PROTECTED]> wrote: > Bernard Lebel wrote: > > Hello, > > > > I have a string, and I use a regular expression to search a match in > > it. When I find one, I would like to break down the string, using the > > matched part of it, to be able to perform some formatting and to later > > build a brand new string with the separate parts. > > > > The regular expression part works ok, but my problem is to extract the > > matched pattern from the string. I'm not sure how to do that... > > > > > > sString = 'mt_03_04_04_anim' > > > > # Create regular expression object > > oRe = re.compile( "\d\d_\d\d\_\d\d" ) > > > > # Break-up path > > aString = sString.split( os.sep ) > > > > # Iterate individual components > > for i in range( 0, len( aString ) ): > > > > sSubString = aString[i] > > > > # Search with shot number of 2 digits > > oMatch = oRe.search( sSubString ) > > > > if oMatch != None: > > # Replace last sequence of two digits by 3 digits!! > > Hi Bernard, > > It sounds like you need to put some groups into your regex and use re.sub(). > > By putting groups in the regex you can refer to pieces of the match. For > example > > >>> import re > >>> s = 'mt_03_04_04_anim' > >>> oRe = re.compile( "(\d\d_\d\d\_)(\d\d)" ) > >>> m = oRe.search(s) > >>> m.group(1) > '03_04_' > >>> m.group(2) > '04' > > With re.sub(), you provide a replacement pattern that can refer to the groups > from the match pattern. So to insert new characters between the groups is > easy: > > >>> oRe.sub(r'\1XX\2', s) > 'mt_03_04_XX04_anim' > > This may be enough power to do what you want, I'm not sure from your > description. But re.sub() has another trick up its sleeve - the replacement > 'expression' can be a callable which is passed the match object and returns > the string to replace it with. For example, if you wanted to find all the two > digit numbers in a string and add one to them, you could do it like this: > > >>> def incMatch(m): > ... s = m.group(0) # use the whole match > ... return str(int(s)+1).zfill(2) > ... > >>> re.sub(r'\d\d', incMatch, '01_09_23') > '02_10_24' > > This capability can be used to do complicated replacements. > > Kent > > ___ > 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] python strings?
On 08/09/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: below is my program. what i would like to do is to get the last line toprint the number of years that the user enters and also the principal There are a couple (at least!) of ways to get the values into the string that you print. The first way is to pass several values to the print command: print "The value in", years, "is", principal Another way is to use placeholders: print "The value in %d years is %.2f" % (years, principal) The placeholders (shown by the % sign) get replaced by the variables in the list, in order. "%d" means format the value as an integer. "%.2f" means format it as a floating point value with two decimal places. See http://diveintopython.org/native_data_types/formatting_strings.html for more details. You could replace the main bit of the program with a single line, if you're feeling brave! principal = input("Enter the initial principal: ") apr = input("Enter the annual interest rate: ") years = input("Enter the number of years: ") print "The value in %d years will be %.2f" % (years, principal * (1 + apr)**years) Hope this is useful! Olly # A program to compute the value of an investment# years into the future def main():print "This program calculates the future value of an investment overyears"principal = input("Enter the initial principal: ")apr = input("Enter the annual interest rate: ") years = input("Enter the number of years: ")for i in range(10):principal = principal * (1 + apr)print "The value in years is", principalmain() ___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Substring substitution
Ok I think I understand what is going: I'm using a 0 in the replacement argument, between the two groups. If I try with a letter or other types of characters it works fine. So how can use a digit here? Thanks Bernard On 9/8/05, Bernard Lebel <[EMAIL PROTECTED]> wrote: > Hi Kent, > > This is nice! > > There is one thing though. When I run the oRe.sub() call, I get an error: > > > Traceback (most recent call last): > File "", line 1, in ? > File > "\\Linuxserver\prod\XSI\WORKGROUP_4.0\Data\Scripts\pipeline\filesystem\bb_processshotdigits.py", > line 63, in ? > processPath( r'C:\temp\MT_03_03_03\allo.txt', False ) > File > "\\Linuxserver\prod\XSI\WORKGROUP_4.0\Data\Scripts\pipeline\filesystem\bb_processshotdigits.py", > line 45, in processPath > else: sSubString = matchShot( sSubString ) > File > "\\Linuxserver\prod\XSI\WORKGROUP_4.0\Data\Scripts\pipeline\filesystem\bb_processshotdigits.py", > line 11, in matchShot > sNewString = oRe.sub( r'\10\2', sSubString ) > File "D:\Python24\Lib\sre.py", line 260, in filter > return sre_parse.expand_template(template, match) > File "D:\Python24\Lib\sre_parse.py", line 781, in expand_template > raise error, "invalid group reference" > sre_constants.error: invalid group reference > > > > This is my new match function: > > > > def matchShot( sSubString ): > > # Create regular expression object > oRe = re.compile( "(\d\d_\d\d\_)(\d\d)" ) > > oMatch = oRe.search( sSubString ) > if oMatch != None: > sNewString = oRe.sub( r'\10\2', sSubString ) > return sNewString > else: > return sSubString > > > > I have read the sub() documentation entry but I have to confess that > made things more confusing for me... > > Thanks again > Bernard ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Substring substitution
Bernard Lebel wrote: > Ok I think I understand what is going: I'm using a 0 in the > replacement argument, between the two groups. If I try with a letter > or other types of characters it works fine. So how can use a digit > here? There is a longer syntax for \1 - \g<1> means the same thing but without the ambiguity of where it ends. So you can use r'\g<1>0\2' as your substitution string. >>def matchShot( sSubString ): >> >># Create regular expression object >>oRe = re.compile( "(\d\d_\d\d\_)(\d\d)" ) >> >>oMatch = oRe.search( sSubString ) >>if oMatch != None: >>sNewString = oRe.sub( r'\10\2', sSubString ) >>return sNewString >>else: >>return sSubString You don't have to do the search, oRe.sub() won't do anything if there is no match. Also if you are doing this a lot you should pull the re.compile() out of the function (so oRe is a module variable), this is an expensive step that only has to be done once. You hinted in your original post that you are trying to find strings where the last _\d\d has only two digits. The re you are using will also match something like 'mt_03_04_044_anim' and your matchShot() will change that to 'mt_03_04_0044_anim'. If that is not what you want you have to put some kind of a guard at the end of the re - something that won't match a digit. If you always have the _ at the end it is easy, just use r"(\d\d_\d\d\_)(\d\d_)". If you can't count on the underscore you will have to be more clever. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] making a table
I would like to construct a table for my program but it does not seem to be coming out evenly. Could someone please let me know what to do so that everything will work out correctly? def main(): print "This program shows a table of Celsius temperatures and there Fahrenheit equivalents every 10 degrees from 0C to 100C" print "C F" for i in(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100): fahrenheit = (9.0/5.0) * i + 32 print i print " ", print fahrenheit main() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] making a table
On Thu, 8 Sep 2005 [EMAIL PROTECTED] wrote: > I would like to construct a table for my program but it does not seem to > be coming out evenly. Could someone please let me know what to do so > that everything will work out correctly? Hello, I'll assume for the moment that you want, in your print statements: > print i > print " ", > print fahrenheit to see a single line of output. I see that you have a trailing comma on the second print statement. Can you explain what the comma does there? Good luck to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] making a table
At 04:34 PM 9/8/2005, [EMAIL PROTECTED] wrote: I would like to construct a table for my program but it does not seem to be coming out evenly. Could someone please let me know what to do so that everything will work out correctly? def main(): print "This program shows a table of Celsius temperatures and there Fahrenheit equivalents every 10 degrees from 0C to 100C" print "C F" for i in(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100): fahrenheit = (9.0/5.0) * i + 32 print i print " ", print fahrenheit main() 1 - you are getting C and F on different lines because there is no , after print i. After you fix that you will get: C F 0 32.0 10 50.0 ... similar lines deleted 40 104.0 ... similar lines deleted 100 212.0 Now the trick is to get things lined up. Here % formatting comes in: print " C F" ... print '%3s %5.1f' % (i, farenheit) Giving: C F 0 32.0 10 50.0 etc. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor