Re: [Tutor] operator, mult
"col speed" wrote I got the following function while googling: def totient(n): from operator import mult if n == 1: return 1 return reduce(mult, [(p-1) * p**(m-1) for p,m in prime_factors_mult(n)]) I already have the "prime_factors" function. The problem is that I cannot find "mult". Given it says mult is in operators then it must be (or have been in a previous version) a standard Python operator that is intended. Did it menton which version of Python was used? Is it an old site? I tried using "mul" which is in "operator" but that is obviously not the same thing. How so? What did it do? from operator import mul reduce(mul,[1,2,3,4]) 24 Does what I would expect it to do... What do you think mult should do? Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling post request
"Tiago Katcipis" wrote I am trying to make a small HTTP server on python just to handle some POST and GET requests. I never worked with http before and dont know if i am doing something completely stupid (probably yes). Not stupid so much as overly complicated. The cgi module will do all the hard work for you including transparently handling POST and GET requests Read the web HOWTO document: http://docs.python.org/howto/webservers.html Then read the CGI module documentation which includes a very quick introduction to CGI programming. If you are intending expanding the web site to anything beyond trivial consider using a Framework such as Django, Turbo Gears or Pylons. Unless you are trying to do something very clever - which given your level of knowledge I'd guess is not the case - then the standard cgi module should do all you need! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling post request
thanks for the help, im really not used on doing web stuff, ill try reading the how to. On Wed, Jan 28, 2009 at 9:03 AM, Alan Gauld wrote: > "Tiago Katcipis" wrote > > I am trying to make a small HTTP server on python just to handle some POST >> and GET requests. I never worked with http before and dont know if i am >> doing something completely stupid (probably yes). >> > > Not stupid so much as overly complicated. > > The cgi module will do all the hard work for you including transparently > handling POST and GET requests > > Read the web HOWTO document: > > http://docs.python.org/howto/webservers.html > > Then read the CGI module documentation which includes a very quick > introduction to CGI programming. If you are intending expanding the web > site to anything beyond trivial consider using a Framework such as > Django, Turbo Gears or Pylons. > > Unless you are trying to do something very clever - which given your > level of knowledge I'd guess is not the case - then the standard > cgi module should do all you need! > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- "it might be a profitable thing to learn Java, but it has no intellectual value whatsoever" Alexander Stepanov ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling post request
actualy i have found what i wanted on the rfile attribute, i already tried before to read it but the application just freezes when i try to read() or readline() on it. But after i make the request, and both client and server sides freezes, if i kill the client side the server becomes able of reading the rfile. Does anyone know why i just cant read the rfile without killing the client side? it feels like the client side is waiting for something while the server is unable to read the rfile while the client side is waiting. On Wed, Jan 28, 2009 at 9:09 AM, Tiago Katcipis wrote: > thanks for the help, im really not used on doing web stuff, ill try reading > the how to. > > > On Wed, Jan 28, 2009 at 9:03 AM, Alan Gauld wrote: > >> "Tiago Katcipis" wrote >> >> I am trying to make a small HTTP server on python just to handle some >>> POST >>> and GET requests. I never worked with http before and dont know if i am >>> doing something completely stupid (probably yes). >>> >> >> Not stupid so much as overly complicated. >> >> The cgi module will do all the hard work for you including transparently >> handling POST and GET requests >> >> Read the web HOWTO document: >> >> http://docs.python.org/howto/webservers.html >> >> Then read the CGI module documentation which includes a very quick >> introduction to CGI programming. If you are intending expanding the web >> site to anything beyond trivial consider using a Framework such as >> Django, Turbo Gears or Pylons. >> >> Unless you are trying to do something very clever - which given your >> level of knowledge I'd guess is not the case - then the standard >> cgi module should do all you need! >> >> HTH, >> >> >> -- >> Alan Gauld >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > > > -- > "it might be a profitable thing to learn Java, but it has no intellectual > value whatsoever" Alexander Stepanov > -- "it might be a profitable thing to learn Java, but it has no intellectual value whatsoever" Alexander Stepanov ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling post request
> From: Tiago Katcipis > > I am trying to make a small HTTP server on python just to handle some POST > and GET requests. I never worked with http before and dont know if i am > doing something completely stupid (probably yes). I read anything possible > already and i just cant find how i access the information sent on the POST > request, i need these because some parameters needed by the server must be > sent on the POST request, my client test does this: > > > f = urllib2.urlopen(url, urllib.urlencode('http://my_server_adress:port', > {'Teste' : 'teste', 'Teste2' : 't2', 'Teste3' : 't3'})) > f.close() > > The server runs ok and receives the POST request just fine, but im not > finding where the data that i have sent on the post request is being held. > Sorry if the question is extremely stupid, i really tried a lot of things > already and read a lot, maybe i just have let something pass when i was > reading or i am understanding something terribly wrong :-(. Hello, I needed to do the same recently to test my scripts that do GET and POST to a web site. Found how to get at the posted data within CGIHTTPServer.py particularly the run_cgi method of the CGIHTTPRequestHandler class. At first I just ran an instance of the CGIHTTPServer and had a few CGI scripts until I read the code for the run_cgi method above. Now my test script runs an HTTP server in a daemon thread using my subclass of the CGIHTTPRequestHandler then the main thread then runs my GET and POST code. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Handling post request
I am trying to make a small HTTP server on python just to handle some POST and GET requests. I never worked with http before and dont know if i am doing something completely stupid (probably yes). I read anything possible already and i just cant find how i access the information sent on the POST request, i need these because some parameters needed by the server must be sent on the POST request, my client test does this: f = urllib2.urlopen(url, urllib.urlencode('http://my_server_adress:port', {'Teste' : 'teste', 'Teste2' : 't2', 'Teste3' : 't3'})) f.close() The server runs ok and receives the POST request just fine, but im not finding where the data that i have sent on the post request is being held. Sorry if the question is extremely stupid, i really tried a lot of things already and read a lot, maybe i just have let something pass when i was reading or i am understanding something terribly wrong :-(. O already have take a look at: http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer http://docs.python.org/library/basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler.handle_one_request http://effbot.org/librarybook/simplehttpserver.htm http://personalpages.tds.net/~kent37/kk/00010.html my server code is: def adcionar_tratador_server(endereco_servidor, tratador): BaseHTTPServer.HTTPServer(endereco_servidor, tratador).serve_forever() class TratadorRequisicaoHTTPIDLocutor(BaseHTTPServer.BaseHTTPRequestHandler): def do_HEAD(self): print self.command print self.path print self.headers print self.headers.getplist() print self.raw_requestline print urlparse.urlparse(self.path) return 'ok' def do_GET(self): print self.command print self.path print self.headers print self.headers.getplist() print self.raw_requestline print urlparse.urlparse(self.path) return 'ok' def do_POST(self): print self.command print self.path print self.headers print self.headers.getplist() print self.raw_requestline print urlparse.urlparse(self.path) return 'ok' adcionar_tratador_server(('', 8000) , TratadorRequisicaoHTTPIDLocutor) -- "it might be a profitable thing to learn Java, but it has no intellectual value whatsoever" Alexander Stepanov ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling post request
thank you all for the help but i have finnaly been able to do what i wanted. I will not use CGI scripts, its very simple what im doing and i just wanted the parameters sent on the POST like: * "name1=value1&name2=value2&name3=value3"* but reading about CGI i discovered that the size of these parameters are in content-lenght, when reading the rfile with the content-length as the number of bytes to read it has worked fine. best regards On Wed, Jan 28, 2009 at 9:20 AM, Justin Ezequiel < justin.mailingli...@gmail.com> wrote: > > From: Tiago Katcipis > > > > I am trying to make a small HTTP server on python just to handle some > POST > > and GET requests. I never worked with http before and dont know if i am > > doing something completely stupid (probably yes). I read anything > possible > > already and i just cant find how i access the information sent on the > POST > > request, i need these because some parameters needed by the server must > be > > sent on the POST request, my client test does this: > > > > > > f = urllib2.urlopen(url, urllib.urlencode('http://my_server_adress:port > ', > > {'Teste' : 'teste', 'Teste2' : 't2', 'Teste3' : 't3'})) > > f.close() > > > > The server runs ok and receives the POST request just fine, but im not > > finding where the data that i have sent on the post request is being > held. > > Sorry if the question is extremely stupid, i really tried a lot of things > > already and read a lot, maybe i just have let something pass when i was > > reading or i am understanding something terribly wrong :-(. > > Hello, I needed to do the same recently to test my scripts that do GET > and POST to a web site. > Found how to get at the posted data within CGIHTTPServer.py > particularly the run_cgi method of the CGIHTTPRequestHandler class. > > At first I just ran an instance of the CGIHTTPServer and had a few CGI > scripts until I read the code for the run_cgi method above. > Now my test script runs an HTTP server in a daemon thread using my > subclass of the CGIHTTPRequestHandler then the main thread then runs > my GET and POST code. > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- "it might be a profitable thing to learn Java, but it has no intellectual value whatsoever" Alexander Stepanov ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python grep
Since there have been a couple of threads about searching recently, I thought this might be of interest: http://www.redmountainsw.com/wordpress/archives/python-grep Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Handling post request
"Tiago Katcipis" wrote thank you all for the help but i have finnaly been able to do what i wanted. I will not use CGI scripts, its very simple what im doing and i just wanted the parameters sent on the POST like: * "name1=value1&name2=value2&name3=value3"* The cgi module would have given you that in about 5 lines of code, so you did an awful lot of work that you didn't need to! But at least you will have learned a lot about http servers that might be useful in the future. :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How Do I Put an Image on a Canvas and Display it?
Well, I new one day I'd have to understand namespaces better. VBG. Your article and others helped. I'm not sure I understand the print42 example. Is it that the name is produced in the first module, and the body of the it isn't available to the second module? I'll post in a few minutes a comment about my PIL vs Tkinter post made earlier than this one. It's less related to this thread. Wayne Watson wrote: I'm using pythonWin. Yes, basically, restore the interactive session. I had done several imports, and wanted the clear them to see what would happen with others I planned on entering without having them affected by earlier ones. Keyboard imports. Just entering "import Tkinter" at the >>> prompt. ALAN GAULD wrote: > how do I clear the keyboard imports? I'm not sure what you mean by keyboard imports? Do you mean how to restore an interactive session? If so, then in IDLE you can use: Shell->Restart shell. I don't know how to do that in any of the other IDEs... You can of course del() any name, including modules... Alan G. -- Signature.html Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) Copper and its alloys have been found effective in hospital sinks, hand rails, beds, ... in significantly reducing bacteria. Estimates are 1/20 people admitted to a hospital become infected, and 1/20 die from the infection. -- NPR Science Friday, 01/16/2009 Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Signature.html Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) Copper and its alloys have been found effective in hospital sinks, hand rails, beds, ... in significantly reducing bacteria. Estimates are 1/20 people admitted to a hospital become infected, and 1/20 die from the infection. -- NPR Science Friday, 01/16/2009 Web Page: ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find a Word in *.py (Win XP)
Title: Signature.html Just using the standard Win XP Pro folder search. I target the folder with my py programs, use *.py to search, and specify I'm looking for angle in the files it finds. Kent Johnson wrote: On Sun, Jan 25, 2009 at 10:31 AM, Wayne Watson wrote: If I open a folder in Win XP with a number of py files, and when I search for *.py for the word angle, I get zippo. However, the word exists in one of the py files. Probably the py files are scrambled in some way. How nevertheless do I find the file. BTW, I know where it is, but I'm wondering why this doesn't work. How are you searching? .py files are plain text. Kent -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) Copper and its alloys have been found effective in hospital sinks, hand rails, beds, ... in significantly reducing bacteria. Estimates are 1/20 people admitted to a hospital become infected, and 1/20 die from the infection. -- NPR Science Friday, 01/16/2009 Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To Arc (PIL) or Not to Arc (Tkinter)
Title: Signature.html I've been playing with PIL and Tkinter a bit, and see that PIL does not have any facility to view the image file I draw on. I open a file from my folder with a py program as an Image. The only way, without using some other module, is to save the changed file, then display it with a paint or photo program. Is that correct, or did I miss something in PIL? Alan Gauld wrote: "Wayne Watson" wrote When I noticed that both have an arc method that gave me pause. Apparently, PIL provides IP (image processing: ...while Tkinter provides widgets. In PIL, arc allows one to draw on images but doesn't allow one to display them. The display and presentation is the function of Tkinter, and can draw on the widget canvas where an image, from PIL, might be placed. Comments? Yes you have understood pretty well. PIL provides more sophisticated IP than Tkinter can do on its own. For simple images such as charts/graphs you might decide to draw them direct in Tkinter without using PIL. For that reason Tkinter canvas allows you to draw shapes, such as arc, directly on the canvas. For more sophisticated use (such as complex charts) you might use a plotting library to create the image and then display the image in Tkinter. Or to manipulate sophisticated graphics (like photos) you would probably use PIL or ImageMagik or somesuch. HTH, -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) Copper and its alloys have been found effective in hospital sinks, hand rails, beds, ... in significantly reducing bacteria. Estimates are 1/20 people admitted to a hospital become infected, and 1/20 die from the infection. -- NPR Science Friday, 01/16/2009 Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To Arc (PIL) or Not to Arc (Tkinter)
On Wed, Jan 28, 2009 at 8:25 AM, Wayne Watson wrote: > I've been playing with PIL and Tkinter a bit, and see that PIL does not have > any facility to view the image file I draw on. I open a file from my folder > with a py program as an Image. The only way, without using some other > module, is to save the changed file, then display it with a paint or photo > program. Is that correct, or did I miss something in PIL? The ImageTk module integrates PIL with Tkinter: http://effbot.org/imagingbook/imagetk.htm Here is a simple image viewer library, just call showImage() with your loaded Image object: # an image viewer import ImageTk from Tkinter import Tk, Label class UI(Label): def __init__(self, master, im): if im.mode == "1": # bitmap image self.image = ImageTk.BitmapImage(im, foreground="white") Label.__init__(self, master, image=self.image, bg="black", bd=0) else: # photo image self.image = ImageTk.PhotoImage(im) Label.__init__(self, master, image=self.image, bd=0) def showImage(im): root = Tk() UI(root, im).pack() root.mainloop() Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find a Word in *.py (Win XP)
"Wayne Watson" wrote Just using the standard Win XP Pro folder search. I target the folder with my py programs, use *.py to search, and specify I'm looking for angle in the files it finds. Yes, I get the same behaviour! I tried searching for 'import' which should be almost every file! It came up blank. When I tried tkinter it found one file with Tkinter in the file name. It seems it is not looking inside the file! Weird. However the DOS command findstr works as expected! Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find a Word in *.py (Win XP)
Alan Gauld wrote: "Wayne Watson" wrote Just using the standard Win XP Pro folder search. I target the folder with my py programs, use *.py to search, and specify I'm looking for angle in the files it finds. Yes, I get the same behaviour! I tried searching for 'import' which should be almost every file! It came up blank. When I tried tkinter it found one file with Tkinter in the file name. It seems it is not looking inside the file! Weird. It's because of XP search treating what it thinks of as text files differently from what it doesn't think of as text files. It does the same for .sql. Immensely irritating. There is some registry fudge which will work around it, but I gave up on using Windows search years ago. (FWIW, I use the pay-for xplorer2 everywhere I go which has been invaluable to me, but YMMV). TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To Arc (PIL) or Not to Arc (Tkinter)
Title: Signature.html Thanks. I was beginning to think the only way to get this done is via Tkinter's canvas. I guess I can see why PIL later became amended with libraries like Tkinter. I think PIL came first, and apparently depended upon some then present elements of Python for displaying images. Working on the imaging side of Python has been a bit daunting without a good text. My oldish Learning Python is of no help in this area. I've ordered some interlibrary loans through the local library, but they've been slow in coming. The internet is fragmented on this subject. Probably the best is from NM Tech, and your reference here. Nothing cohesive out there. I may have to make a book trip down to the big cities this week. I'll rattle our library about the books. They've been impacted by layoffs. UI == Utility Imager? Kent Johnson wrote: On Wed, Jan 28, 2009 at 8:25 AM, Wayne Watson wrote: I've been playing with PIL and Tkinter a bit, and see that PIL does not have any facility to view the image file I draw on. I open a file from my folder with a py program as an Image. The only way, without using some other module, is to save the changed file, then display it with a paint or photo program. Is that correct, or did I miss something in PIL? The ImageTk module integrates PIL with Tkinter: http://effbot.org/imagingbook/imagetk.htm Here is a simple image viewer library, just call showImage() with your loaded Image object: # an image viewer import ImageTk from Tkinter import Tk, Label class UI(Label): def __init__(self, master, im): if im.mode == "1": # bitmap image self.image = ImageTk.BitmapImage(im, foreground="white") Label.__init__(self, master, image=self.image, bg="black", bd=0) else: # photo image self.image = ImageTk.PhotoImage(im) Label.__init__(self, master, image=self.image, bd=0) def showImage(im): root = Tk() UI(root, im).pack() root.mainloop() Kent -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) Copper and its alloys have been found effective in hospital sinks, hand rails, beds, ... in significantly reducing bacteria. Estimates are 1/20 people admitted to a hospital become infected, and 1/20 die from the infection. -- NPR Science Friday, 01/16/2009 Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find a Word in *.py (Win XP)
Title: Signature.html Thanks. I'll post a msg to a XP group about this. I suspect Python hashes the code somehow. Findstr? Wow, that's got to be old. Where did you find that? Alan Gauld wrote: "Wayne Watson" wrote Just using the standard Win XP Pro folder search. I target the folder with my py programs, use *.py to search, and specify I'm looking for angle in the files it finds. Yes, I get the same behaviour! I tried searching for 'import' which should be almost every file! It came up blank. When I tried tkinter it found one file with Tkinter in the file name. It seems it is not looking inside the file! Weird. However the DOS command findstr works as expected! Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) Copper and its alloys have been found effective in hospital sinks, hand rails, beds, ... in significantly reducing bacteria. Estimates are 1/20 people admitted to a hospital become infected, and 1/20 die from the infection. -- NPR Science Friday, 01/16/2009 Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To Arc (PIL) or Not to Arc (Tkinter)
On Wed, Jan 28, 2009 at 10:30 AM, Wayne Watson wrote: > Thanks. I was beginning to think the only way to get this done is via > Tkinter's canvas. > > I guess I can see why PIL later became amended with libraries like Tkinter. > I think PIL came first, and apparently depended upon some then present > elements of Python for displaying images. I don't know the history but Tkinter and PIL really are focused on separate jobs; Tkinter is a general-purpose GUI framework, PIL is for image manipulation. > UI == Utility Imager? User Interface. This is just a quick hack I wrote to help solve problems in the Python Challenge. Many of them involve image manipulation and display. http://www.pythonchallenge.com/ Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find a Word in *.py (Win XP)
You know, it probably wouldn't be terribly difficult to write the search in python. Then you *could* find strings inside rather easily. HTH, Wayne On Wed, Jan 28, 2009 at 9:30 AM, Wayne Watson wrote: > Thanks. I'll post a msg to a XP group about this. I suspect Python hashes > the code somehow. > > Findstr? Wow, that's got to be old. Where did you find that? > > > Alan Gauld wrote: > > > "Wayne Watson" > wrote > > Just using the standard Win XP Pro folder search. > I target the folder with my py programs, use *.py to > search, and specify I'm looking for angle in the files it finds. > > > Yes, I get the same behaviour! > I tried searching for 'import' which should be almost every file! > It came up blank. When I tried tkinter it found one file with Tkinter > in the file name. It seems it is not looking inside the file! > > Weird. > > However the DOS command findstr works as expected! > > Alan G > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > -- > >Wayne Watson (Watson Adventures, Prop., Nevada City, CA) > > (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)* > Copper and its alloys have been found effective in hospital > sinks, hand rails, beds, ... in significantly reducing > bacteria. Estimates are 1/20 people admitted to a hospital > become infected, and 1/20 die from the infection. >-- NPR Science Friday, 01/16/2009 * > Web Page: > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn't. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find a Word in *.py (Win XP)
On Wed, Jan 28, 2009 at 10:56 AM, W W wrote: > You know, it probably wouldn't be terribly difficult to write the search in > python. Then you *could* find strings inside rather easily. Other than being written in Perl ;-) ack is a nice command-line search tool: http://petdance.com/ack/ or the Python equivalent: http://pypi.python.org/pypi/grin Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find a Word in *.py (Win XP)
On Wed, Jan 28, 2009 at 1:29 PM, Wayne Watson wrote: > These look Linux based. > > Kent Johnson wrote: > Other than being written in Perl ;-) ack is a nice command-line search tool: > http://petdance.com/ack/ > > or the Python equivalent: > http://pypi.python.org/pypi/grin No, they are Perl and Python based command-line programs. I know that ack runs fine on Mac OSX and Windows; AFAIK grin is plain Python so it probably does too. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Defining "bit" type
> "Vicent" wrote > >> Anyway, I am working with Python 2.5.4, and I am interested in >> defining a >> new type called "bit" (if possible), which represents a number that >> can only >> take values 0 or 1 —that's what we would call a "binary variable", in a >> Mathematical Programming context. The python manual is usually a good thing to read : 3.4.1 Bit-string Operations on Integer Types Plain and long integer types support additional operations that make sense only for bit-strings. Negative numbers are treated as their 2's complement value (for long integers, this assumes a sufficiently large number of bits that no overflow occurs during the operation). The priorities of the binary bit-wise operations are all lower than the numeric operations and higher than the comparisons; the unary operation "~" has the same priority as the other unary numeric operations ("+" and "-"). This table lists the bit-string operations sorted in ascending priority (operations in the same box have the same priority): Operation Result Notes |x | y| bitwise /or/ of x and y |x ^ y| bitwise /exclusive or/ of x and y |x & y| bitwise /and/ of x and y |x << n|x shifted left by n bits(1), (2) |x >> n|x shifted right by n bits (1), (3) |~x|the bits of x inverted Notes: *(1)* Negative shift counts are illegal and cause a ValueError to be raised. *(2)* A left shift by n bits is equivalent to multiplication by |pow(2, n)| without overflow check. *(3)* A right shift by n bits is equivalent to division by |pow(2, n)| without overflow check. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find a Word in *.py (Win XP)
"W W" wrote You know, it probably wouldn't be terribly difficult to write the search in python. Then you *could* find strings inside rather easily. Given that there's findstr in DOS and grep in *nix(and for DOS too!) it hardly seems worthwhile reinventing that particular wheel! :-) But the Explorer based search does have advantages in that you can manipulate the found files using the windows GUI where as with the command line tools you either have to construct some kind of pipeline or take a note of the found files. How insane that XP doesn't search the text in "non text" files! -- 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] Finding the End of a Def?
I'm sure with a refresher on Perl, I could write code to do it. However, I'll just use my eyes. Ricardo Aráoz wrote: Wayne Watson wrote: I'm not at all tempted (binding). VBG. Page scroll and a steady eye on the left margin works well for my current purposes. However, I am surprised such a feature doesn't exist. W W wrote: On Sat, Jan 24, 2009 at 7:06 AM, Wayne Watsonwrote: If you are familiar with vi and C, one could enter a simple keystroke and jump from an opening paren to the corresponding closing one. I've long forgotten most of C, but here's a rough segment of a program: main() ( while (x ==True) ( a =5; ) ... ) If your cursor was on the second "(", pressing a key would take you to the third one. I want something like it for def. Where's the end of the def. Some def blocks are so long it's almost impossible to find where they end. Presuming, of course, that your def statement is followed by another, finding the "next" def would get you there. Other than that... you may just have to figure out how to write your own bindings. HTH, Wayne Does your editor support regular _expression_ search? If it does you just look for a beginning of line followed by any non-blank character (non-comment character too if you want to be fancy), shouldn't be too hard to automate. For defs inside classes you look for a beginning of line, 4 spaces (tab or 8spaces) followed by non-blank char. -- Signature.html Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) Copper and its alloys have been found effective in hospital sinks, hand rails, beds, ... in significantly reducing bacteria. Estimates are 1/20 people admitted to a hospital become infected, and 1/20 die from the infection. -- NPR Science Friday, 01/16/2009 Web Page: ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To Arc (PIL) or Not to Arc (Tkinter)
I think PIL came first, and apparently depended upon some then present elements of Python for displaying images. I'm pretty sure Tkinter came before PIL. Tkinter has been around since at least Python 1.3, and I think maybe even 1.2.X! I don't think PIL appeared till about v2.0 But PIL is not, and never has been a display tool, it is about maniplulating images in memory. How you display them is another issue entirely. Tkinter is a wrapper around Tk which was designed (in 1988) as a way of building simple GUIs around command line tools. Specifically tools intended to control bespoke bits of electronic hardware. So its graphical requirements were very limited. Conciseness, small footprint and ease of use were its primary goals. And all that is as it should be - separation of concerns is a very good design pattern. Its a tetament to PIL that the latest version is 1.1.6 which was developed for Python 1.5.2 and still works fine with Python 2.6! I suspect changes will be needed for v3 though... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python grep
"Kent Johnson" wrote Since there have been a couple of threads about searching recently, I thought this might be of interest: http://www.redmountainsw.com/wordpress/archives/python-grep I'll throw in PowerGrep which is a grep like tool that knows about multiple file formats such as MS Office filers. Its commercial, not very cheap but does what it says on the tin and I don't know of any other equivalent. http://www.powergrep.com/ But you need to be a pretty determined searcher to need it! Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] IDLE vs PythonWin
Title: Signature.html About three weeks ago I decided to give PythonWin a whirl. I believe I've noticed about as many code "wrecks" as with IDLE. That is, while working repeatedly on a program, the editor got confused about what it had available, usually remnants of previous code. I just tried to print a very small program in PWin and it printed five blank pages with a small title on each. I had to turn off the printer. This is not good. Although, when I first fired up PWin, I liked the idea of the interactive window being part of the larger window with the program windows, I'm not much in favor of it any more. It may be time to move on to another editor or return to IDLE. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) Copper and its alloys have been found effective in hospital sinks, hand rails, beds, ... in significantly reducing bacteria. Estimates are 1/20 people admitted to a hospital become infected, and 1/20 die from the infection. -- NPR Science Friday, 01/16/2009 Web Page:___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Find a Word in *.py (Win XP)
> How insane that XP doesn't search the text in "non text" files! > > -- > Alan Gauld http://support.microsoft.com/default.aspx?scid=KB;EN-US;q309173 I used Method 2 under Resolution recently when I reinstalled XP on a new harddisk. HTH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] configuring web browser PP3E
hi. i'm reading 'Programming Python' and having some trouble with adding a web interface in chapter one. (page 72) basically, i open the cgi101.html file in Firefox. i enter a name and hit Submit Query: but instead of getting the Hello "name"! page returned to me I get the error page. #!/usr/bin/python import cgi form = cgi.FieldStorage()# parse form data print "Content-type: text/html\n"# hdr plus blank line print "Reply Page"# html reply page if not form.has_key('user'): print "Who are you?" else: print "Hello %s!" % cgi.escape(form['user'].value) the book says i might have to modify the path to my Python in the #! line at the top of the script file and make it executable with a chmod command. i've tried many things to get this working. also, i'm not getting anywhere with the webserver.py file either. here is the error message: Traceback (most recent call last): File "webserver.py", line 22, in ? srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler) File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/SocketServer.py", line 330, in __init__ File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/BaseHTTPServer.py", line 100, in server_bind File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/SocketServer.py", line 341, in server_bind File "", line 1, in bind socket.error: (13, 'Permission denied') can anyone help? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] configuring web browser PP3E
On Thu, Jan 29, 2009 at 11:31 AM, bmol...@att.net wrote: > Traceback (most recent call last): > File "webserver.py", line 22, in ? >srvrobj = HTTPServer(srvraddr, CGIHTTPRequestHandler) > File > "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/SocketServer.py", > line 330, in __init__ > File > "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/BaseHTTPServer.py", > line 100, in server_bind > File > "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/SocketServer.py", > line 341, in server_bind > File "", line 1, in bind > socket.error: (13, 'Permission denied') srvraddr is being passed a port of 80. I suspect that you're running on a Mac and that Mac's being UNIX systems, do not allow you as a normal user to listen on ports < 1000 without root privileges. Solution 1: Run this under sudo Solution 2: Change the port to 8000 cheers James ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] building Python 2.6?
Hello, I am trying to build python 2.6 on a machine (web server) that I do not have root access to. (has 2.4 installed) Python 2.5 builds fine, but I am getting an error when I run "make" for 2.6.1 Here is the command line I am using: ../configure -prefix=/home/username/local-python/ --enable-unicode=ucs4 (I don't know what the ucs4 thing is, but all the HOWTOs I saw say to use it. ) Here is what the local python reports itself as: Python 2.4.3 (#1, Jul 29 2007, 14:09:31) [GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2 Here is the error after I run "make": [SNIP] /home/username/local-src/Python-2.6.1/Modules/_ctypes/_ctypes.c:3941: error: syntax error before '*' token /home/username/local-src/Python-2.6.1/Modules/_ctypes/_ctypes.c:3942: warning: function declaration isn't a prototype /home/username/local-src/Python-2.6.1/Modules/_ctypes/_ctypes.c: In function `CFuncPtr_nonzero': /home/username/local-src/Python-2.6.1/Modules/_ctypes/_ctypes.c:3943: error: `self' undeclared (first use in this function) Failed to find the necessary bits to build these modules: _tkinter bsddb185 sunaudiodev To find the necessary bits, look in setup.py in detect_modules() for the module's name. Failed to build these modules: _ctypes running build_script ~~~ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] configuring web browser PP3E
On Thu, Jan 29, 2009 at 12:17 PM, bmol...@att.net wrote: > yes, i'm running on Mac OS X / Unix, but i'm afraid i don't know "where" to > change the port to 80. i'm working with the following files straight from > the book 'Programming Python': You're not blind are you ? Because I am. > cgi101.py (here it is...) > #!/usr/bin/python > import cgi > form = cgi.FieldStorage()# parse form data > print "Content-type: text/html\n"# hdr plus blank line > print "Reply Page"# html reply page > if not form.has_key('user'): > print "Who are you?" > else: > print "Hello %s!" % cgi.escape(form['user'].value) > cgi101.html (here it is...) > > Interactive Page > > > Enter your name: > > > > > and webserver.py (here it is...) > ## > # implement HTTP web server in Python which knows how to run server > # side CGI scripts; serves files/scripts from current working dir; > # python scripts must be stored in webdir\cgi-bin or webdir\htbin; > ## > webdir = '.' # where your html files and cgi-bin script directory live > port = 80# default http://localhost/, else use http://localhost:/ Chat the above line. [...] cheers James ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] operator, mult
This is a way of finding how many coprimes there are for a number - eg 144 How many positive numbers less than or equal to 144 are relatively prime to 144? Factor 144 = 2 4 × 3 2 . Use the formula for each prime: >From 2 4 , we get 2 - 1 = 1 and 2 4 - 1 = 2 3 = 8. >From 3 2 , we get 3 - 1 = 2 and 3 2 - 1 = 3 1 = 3. Multiply these numbers together to get the answer. 1 × 8 × 2 × 3 = 48. What I expected "mult" to do was (somehow)to work out what the *powers* of the prime factors would be. Another reason I didn't think it was "mul" is the part that says " prime_factors_mult(n)" as the prime_factors function is just "prime_factors(n)" - without the "_mult". The website is http://wiki.python.org/moin/ProblemSets/99%20Prolog%20Problems%20Solutions#Problem33.3ADetermineiftwonumbersarecoprime I've attached a script that *should *work out the number of coprimes of 144 *Please don't bother too much about this. I've included it for your information as syou have replied, but I think I'll leave it until I understand a bit more - I'm biting off more than I can chew.* Message: 6 Date: Wed, 28 Jan 2009 08:29:09 - From: "Alan Gauld" Subject: Re: [Tutor] operator, mult To: tutor@python.org Message-ID: Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original "col speed" wrote > I got the following function while googling: > > def totient(n): >from operator import mult >if n == 1: return 1 >return reduce(mult, [(p-1) * p**(m-1) for p,m in > prime_factors_mult(n)]) > > I already have the "prime_factors" function. The problem is that I > cannot > find "mult". Given it says mult is in operators then it must be (or have been in a previous version) a standard Python operator that is intended. Did it menton which version of Python was used? Is it an old site? > I tried using "mul" which is in "operator" but that is > obviously not the same thing. How so? What did it do? >>> from operator import mul >>> reduce(mul,[1,2,3,4]) 24 Does what I would expect it to do... What do you think mult should do? Alan G #!/usr/bin/env python def prime_factors(n): """find factors of n using trial division by primes. >>> prime_factors(315) [3, 3, 5, 7] """ factors = [] for p in primeGenerator(n): if p*p > n: break while n % p == 0: n /= p factors.append(p) if n != 1: factors.append(n) def totient(n): """calculate Euler's totient function. If [[p_0,m_0], [p_1,m_1], ... ] is a prime factorization of 'n', then the totient function phi(n) is given by: (p_0 - 1)*p_0**(m_0-1) * (p_1 - 1)*p_1**(m_1-1) * ... >>> phi(1) 1 >>> phi(10) 4 """ from operator import mul # change to "mult"? if n == 1: return 1 return reduce(mul, [(p-1) * p**(m-1) for p,m in prime_factors_mul(n)]) # change to "mult? print totient(144) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] operator, mult
2009/1/29 col speed : [...] > What I expected "mult" to do was (somehow)to work out what the powers of > the prime factors would be. Another reason I didn't think it was "mul" is > the part that says " prime_factors_mult(n)" as the prime_factors function > is just "prime_factors(n)" - without the "_mult". Well, it's been a while since my number theory course, so I was just going from the code comments: def totient(n): """calculate Euler's totient function. If [[p_0,m_0], [p_1,m_1], ... ] is a prime factorization of 'n', then the totient function phi(n) is given by: (p_0 - 1)*p_0**(m_0-1) * (p_1 - 1)*p_1**(m_1-1) * ... >>> phi(1) 1 >>> phi(10) 4 """ from operator import mult if n == 1: return 1 return reduce(mult, [(p-1) * p**(m-1) for p,m in prime_factors_mult(n)]) If we imagine for a moment that we have: prime_facs = [(p_0, m_0), (p_1, m_1), (p_2, m_2), (p_3, m_3)] then reduce(operator.mul, [(p-1) * p**(m-1) for p,m in prime_facs]) translates exactly to (p_0-1)*p_0**(m_0-1) * (p_1-1)*p_1**(m_1-1) * (p_2-1)*p_2**(m_2-1) * (p_3-1)*p_3**(m_3-1) which seems to match the description in the comment. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] operator, mult
Thanks John, That sorted me out, sometimes I just can't get things worked out in my head, then get a sense of "instant enlightenment", which your comments did for me. I am ashamed to say I was using the wrong prime factors function, then changing the mult to mul all started to make sense. Thanks again Colin 2009/1/29 John Fouhy > 2009/1/29 col speed : > [...] > > What I expected "mult" to do was (somehow)to work out what the powers > of > > the prime factors would be. Another reason I didn't think it was "mul" is > > the part that says " prime_factors_mult(n)" as the prime_factors > function > > is just "prime_factors(n)" - without the "_mult". > > Well, it's been a while since my number theory course, so I was just > going from the code comments: > > def totient(n): >"""calculate Euler's totient function. > >If [[p_0,m_0], [p_1,m_1], ... ] is a prime factorization of 'n', >then the totient function phi(n) is given by: > >(p_0 - 1)*p_0**(m_0-1) * (p_1 - 1)*p_1**(m_1-1) * ... > > >>> phi(1) >1 >>>> phi(10) >4 >""" > from operator import mult > >if n == 1: return 1 > >return reduce(mult, [(p-1) * p**(m-1) for p,m in prime_factors_mult(n)]) > > If we imagine for a moment that we have: > >prime_facs = [(p_0, m_0), (p_1, m_1), (p_2, m_2), (p_3, m_3)] > > then > >reduce(operator.mul, [(p-1) * p**(m-1) for p,m in prime_facs]) > > translates exactly to > >(p_0-1)*p_0**(m_0-1) * (p_1-1)*p_1**(m_1-1) * (p_2-1)*p_2**(m_2-1) > * (p_3-1)*p_3**(m_3-1) > > which seems to match the description in the comment. > > -- > John. > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Defining "bit" type
On Tue, Jan 27, 2009 at 14:26, Ricardo Aráoz wrote: > > "Vicent" wrote > > Anyway, I am working with Python 2.5.4, and I am interested in defining a > new type called "bit" (if possible), which represents a number that can > only > take values 0 or 1 —that's what we would call a "binary variable", in a > Mathematical Programming context. > > The python manual is usually a good thing to read : > > > 3.4.1 Bit-string Operations on Integer Types > > Plain and long integer types support additional operations that make sense > only for bit-strings. Negative numbers are treated as their 2's complement > value (for long integers, this assumes a sufficiently large number of bits > that no overflow occurs during the operation). > > The priorities of the binary bit-wise operations are all lower than the > numeric operations and higher than the comparisons; the unary operation "~" > has the same priority as the other unary numeric operations ("+" and "-"). > > > This table lists the bit-string operations sorted in ascending priority > (operations in the same box have the same priority): > > Operation Result Notes x | y bitwise *or* of x and y > x ^ y bitwise *exclusive or* of x and y > x & y bitwise *and* of x and y > x << n x shifted left by n bits (1), (2) x >> n x shifted right by nbits > (1), > (3) ~x the bits of x inverted > > Notes: > *(1)* Negative shift counts are illegal and cause a ValueError to be > raised. *(2)* A left shift by n bits is equivalent to multiplication by pow(2, > n) without overflow check. *(3)* A right shift by n bits is equivalent to > division by pow(2, n) without overflow check. > Ricardo, Thank you. I think I already read this, but I didn't realize that it could be useful for me. I mean, I was looking for a class that stores only 0-1 values and that is 1-bit-sized, in order to save memory and/or disk space. That's why I thought that "bool" type would be OK for me, but maybe I could also consider integers (or translating those data into integers), because of those operations that are already implemented and that you have just showed me. Anyway, I think similar operators already exist for single "bool" data. That approach you suggest would be useful in case I wanted to manage with strings of 0's and 1's (which could be possible). Thank you! -- Vicent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor