Re: [Tutor] Engarde program was: i++
"David Heiser" <[EMAIL PROTECTED]> wrote > > def is_yes(question): > while True: > try: > s = raw_input(question).lower()[0] > if s == 'y': > return True > elif s == 'n': > return False > except: > pass ## This traps the condition where a user just > presses enter > print '\nplease select y, n, yes, or no\n' What value do you think the pass adds over just having the print statement inside the except block? So far as I can tell they will both do exactly the same so the pass is not needed. Or am I missing something here? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Multi-line comments?
"Brad Tompkins" <[EMAIL PROTECTED]> wrote > If there isn't a way, can someone recommend a text editor (I know > emacs and > probably vi can do this, but they seem difficult to use) that will > comment > out blocks of text automatically for me? The Pythonwin IDE has the Edit->Source->Comment out region command IDLE has Format->Comment out region Both use the shortcut Alt+3 But usually long comments are better exposed as doc strings. Comments should be reserved for explaining why you wrote the code the way you did - unusual constructs etc, use docstrings to explain what the code is for. 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] Multi-line comments?
* Brad Tompkins (Wed, 6 Jun 2007 16:41:31 -0700) > Is there a way to make use of multi-line comments when programming using > python? Having to stick a # in front of every line gets pretty tedious when > I want to make a comment more detailed than I normally would. > > If there isn't a way, can someone recommend a text editor (I know emacs and > probably vi can do this, but they seem difficult to use) that will comment > out blocks of text automatically for me? EditPad Pro under Windows and Komodo Edit from ActiveState... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to Embed Python code in C
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi all, i want to embed Python code in C code, need any tutorial for starting. Ramanuj -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.5 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFGZ9jvp6Ts2SMzgZoRAkhrAJ9YZ2JB2n/McMsGnOHxmglaDZHmYACgrk1U MJCMrnGKdzpKtmws7HYcF8s= =bNmX -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Engarde program was: i++
On Wed, 6 Jun 2007, David Heiser wrote: > or.. > > def is_yes(question): > while True: > try: > s = raw_input(question).lower()[0] > if s == 'y': > return True > elif s == 'n': > return False > except: > pass ## This traps the condition where a user just > presses enter > print '\nplease select y, n, yes, or no\n' Hi David, This does have a different functionality and is is more permissive than the original code. What if the user types in "yoo-hoo"? The original code would reject this, but the function above will treat it as as yes. It does depend on what one wants. Since there is no comment or documentation on is_yes() that says how it should behave, I guess we can say that anything goes, but that's probably a situation we should fix. For this particular situation, I'd avoid the try/except block that is used for catching array-out-of-bounds errors. If we do want the above functionality, we can take advantage of string slicing to similar effect: ### def is_yes(question): """Asks for a response until the user presses something beginning either with 'y' or 'n'. Returns True if 'y', False otherwise.""" while True: s = raw_input(question).lower() if s[:1] == 'y': return True elif s[:1] == 'n': return False print '\nplease select y, n, yes, or no\n' ### ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Engarde program was: i++
What if the user enters "maybe". -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Alan Gauld Sent: Thursday, June 07, 2007 1:45 AM To: tutor@python.org Subject: Re: [Tutor] Engarde program was: i++ "David Heiser" <[EMAIL PROTECTED]> wrote > > def is_yes(question): > while True: > try: > s = raw_input(question).lower()[0] > if s == 'y': > return True > elif s == 'n': > return False > except: > pass ## This traps the condition where a user just > presses enter > print '\nplease select y, n, yes, or no\n' What value do you think the pass adds over just having the print statement inside the except block? So far as I can tell they will both do exactly the same so the pass is not needed. Or am I missing something here? 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] Engarde program was: i++
Very nice trick. Thanks. P.S. You can still use: s = raw_input(question).lower()[:1] if s == ...: -Original Message- From: Danny Yoo [mailto:[EMAIL PROTECTED] Sent: Wednesday, June 06, 2007 5:53 PM To: David Heiser Cc: tutor@python.org Subject: Re: [Tutor] Engarde program was: i++ On Wed, 6 Jun 2007, David Heiser wrote: > or.. > > def is_yes(question): > while True: > try: > s = raw_input(question).lower()[0] > if s == 'y': > return True > elif s == 'n': > return False > except: > pass ## This traps the condition where a user just > presses enter > print '\nplease select y, n, yes, or no\n' Hi David, This does have a different functionality and is is more permissive than the original code. What if the user types in "yoo-hoo"? The original code would reject this, but the function above will treat it as as yes. It does depend on what one wants. Since there is no comment or documentation on is_yes() that says how it should behave, I guess we can say that anything goes, but that's probably a situation we should fix. For this particular situation, I'd avoid the try/except block that is used for catching array-out-of-bounds errors. If we do want the above functionality, we can take advantage of string slicing to similar effect: ### def is_yes(question): """Asks for a response until the user presses something beginning either with 'y' or 'n'. Returns True if 'y', False otherwise.""" while True: s = raw_input(question).lower() if s[:1] == 'y': return True elif s[:1] == 'n': return False print '\nplease select y, n, yes, or no\n' ### ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Best way to POST XML to CGI
All, I have a Python program that makes images of data and I want to be able to make these via a web interface. I want to keep it simple so I thought I would send an XML file to a cgi program (XML is what I am supposed to use). I have the parsing of the XML all figured out but the way I am sending the XML and receiving it does not look like they are the best ways. I have an HTML form that I use to collect input on what kind of image. I then take that form input and make it into an XML string. I then take that XML string and POST it to a CGI script. I am using cgi to retrieve the XML string. Once I have the string I get the needed info and make an image. The Part that just does not look right to me is using form = cgi.FieldStorage() Of someone else wanted to just post an XML string they would have to have the same form name "XMLhttp" that is not very user friendly. My guess is I am missing something about how cgi can work. I bet Python has a simple way to receive a XML post so I do not have to look for a specific form name? Any help would be appreciated. Thanks, John Web page code to post XML in a text area http:///cgi-bin/parsXML2.py"; target=text/xml method=post> Code that receives the XML POST form = cgi.FieldStorage() if not (form.has_key("XMLhttp")): print "content-type: text/html\n\n" print "Error" else: xmlString = form["XMLhttp"].value print "content-type: text/html\n\n" req = ParseXML() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Engarde program was: i++
> What if the user enters "maybe". Sorry, I said the except block i meant the else block of the original posters code. In both his case and yours the message gets printed. But as Danny pointed out you need the try/except in your case because you are indexing a potentially empty string. The OP didn't have that limitation to deal with. Alan G. > > > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On > Behalf > Of Alan Gauld > Sent: Thursday, June 07, 2007 1:45 AM > To: tutor@python.org > Subject: Re: [Tutor] Engarde program was: i++ > > > > "David Heiser" <[EMAIL PROTECTED]> wrote >> >> def is_yes(question): >> while True: >> try: >> s = raw_input(question).lower()[0] >> if s == 'y': >> return True >> elif s == 'n': >> return False >> except: >> pass ## This traps the condition where a user just >> presses enter >> print '\nplease select y, n, yes, or no\n' > > What value do you think the pass adds over just having > the print statement inside the except block? So far as I can > tell they will both do exactly the same so the pass is > not needed. Or am I missing something here? > > 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 > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Best way to POST XML to CGI
"Ertl, John C CIV 63134" <[EMAIL PROTECTED]> wrote > I have a Python program that makes images of data > and I want to be able to make these via a web interface. Fair enough. > I want to keep it simple so I thought I would send > an XML file to a cgi program But thats not simple. CGI is not really designed to deal with XML, they are almost complementary technologies. CGI sends name/value pairs either in GET or POST format. XML encodes the values into a text file. Different things. XML tends to be used for RPC or B2B type applications, CGI is typically used for basic form submissions. Sure you can do it, but I'm not sure there is much point! I'd probably look to use CGI to capture the details then create the XML file at the server end. Its a lot more efficient in terms of network bandwidth too. XML is about the most inefficient network "protocol" ever invented. > I have an HTML form that I use to collect input on what > kind of image. I then take that form input and make it > into an XML string. I then take that XML string and POST > it to a CGI script. As I say, a pretty complex process compared to a simple CGI submit action. > I bet Python has a simple way to receive a XML post > so I do not have to look for a specific form name? Python offers many ways to deal with XML file transmissions, including XML/RPC and SOAP. But CGI is probably not the best approach for XML IMHO. It can be done in the same way as posting a normal text file but its pretty much turning an easy job into a hard one for no benefit. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Engarde program was: i++
How about doing it this way: def is_yes(question): yn = { 'y':True, 'yes':True, 'n':False, 'no':False } while True: try: return yn[raw_input(question).lower().strip()] except KeyError: print '\nplease select y, n, yes, or no\n' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Best way to POST XML to CGI
Alan, Thanks for the input. I am trying to make something that is capable of being a bit more B2B than a standard HTML form...but be able to have a friendly interface for a person. I have not liked SOAP in the past...way to much extra stuff. I was trying to think REST but I have to admit I am missing some pieces. Maybe XML/RPC. Thanks again for the advice...I will rethink how I am doing this. John Ertl Meteorologist FNMOC 7 Grace Hopper Ave. Monterey, CA 93943 (831) 656-5704 [EMAIL PROTECTED] From: [EMAIL PROTECTED] on behalf of Alan Gauld Sent: Thu 6/7/2007 9:30 AM To: tutor@python.org Subject: Re: [Tutor] Best way to POST XML to CGI "Ertl, John C CIV 63134" <[EMAIL PROTECTED]> wrote > I have a Python program that makes images of data > and I want to be able to make these via a web interface. Fair enough. > I want to keep it simple so I thought I would send > an XML file to a cgi program But thats not simple. CGI is not really designed to deal with XML, they are almost complementary technologies. CGI sends name/value pairs either in GET or POST format. XML encodes the values into a text file. Different things. XML tends to be used for RPC or B2B type applications, CGI is typically used for basic form submissions. Sure you can do it, but I'm not sure there is much point! I'd probably look to use CGI to capture the details then create the XML file at the server end. Its a lot more efficient in terms of network bandwidth too. XML is about the most inefficient network "protocol" ever invented. > I have an HTML form that I use to collect input on what > kind of image. I then take that form input and make it > into an XML string. I then take that XML string and POST > it to a CGI script. As I say, a pretty complex process compared to a simple CGI submit action. > I bet Python has a simple way to receive a XML post > so I do not have to look for a specific form name? Python offers many ways to deal with XML file transmissions, including XML/RPC and SOAP. But CGI is probably not the best approach for XML IMHO. It can be done in the same way as posting a normal text file but its pretty much turning an easy job into a hard one for no benefit. 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
[Tutor] Properties
Hello, and I apologize in advance for the question. I have decided to publish a class I use to handle data segments to Google Code for the world to see (I plan to make millions off training classes, books and lectures :-). I need to make it a bit more 'generic' than the class I have been using, and while I'm working the rewrite I thought it would be cool to add unit tests and use properties. The problem is, I only recall that properties are 'cool'. I can't locate the documentation on why or how to use them. If you could point me to the proper documentation I would be most appreciative. Also, I plan to use unittest to write my unit tests. Is this module still considered acceptable, or has something else taken it's place? Thanks for your help, --greg ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Properties
Greg Lindstrom wrote: > Hello, and I apologize in advance for the question. No apologies needed, this list would be quite boring without any questions :-) > > I have decided to publish a class I use to handle data segments to > Google Code for the world to see (I plan to make millions off training > classes, books and lectures :-). I need to make it a bit more 'generic' > than the class I have been using, and while I'm working the rewrite I > thought it would be cool to add unit tests and use properties. The > problem is, I only recall that properties are 'cool'. I can't locate > the documentation on why or how to use them. If you could point me to > the proper documentation I would be most appreciative. Hmmm...if that is all you can remember about properties, then maybe you don't need them, many programs don't...even cool programs :-) I don't know of any good introductory material on properties but here are some starting points: http://www.python.org/download/releases/2.2/descrintro/#property http://docs.python.org/lib/built-in-funcs.html#l2h-57 > Also, I plan to use unittest to write my unit tests. Is this module > still considered acceptable, or has something else taken it's place? unittest is fine. There are a few other unit test frameworks that are also popular but unittest and doctest are the only ones in the standard library. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Invoking Python from Vim
Hi, Bit of a Vim specific question this one but I hope someone might have an answer. I currently have the following line in my .gvimrc file (I'm using Ubuntu Linux): map :!gnome-terminal -e=python\ -i\ % This opens a window and runs the Python program I am working on. I don't really like the fact I can't do anything else in vim until I close the window and I don't really need the interactive prompt that I am left with. If I invoke Python without the -i then I don't get to see the error message if my program exits with an error. In 'normal' vim I use: map :w\|!python % This doesn't work for GVim which I prefer to use. Do any Vim users have a better way of running a Python program while it is being edited in Vim? Thanks, Matt ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Invoking Python from Vim
On Thursday 07 June 2007, Matt Smith wrote: > Hi, > > Bit of a Vim specific question this one but I hope someone might have an > answer. I currently have the following line in my .gvimrc file (I'm > using Ubuntu Linux): > > map :!gnome-terminal -e=python\ -i\ % > > This opens a window and runs the Python program I am working on. I don't > really like the fact I can't do anything else in vim until I close the > window and I don't really need the interactive prompt that I am left > with. If I invoke Python without the -i then I don't get to see the > error message if my program exits with an error. > > In 'normal' vim I use: > > map :w\|!python % > > This doesn't work for GVim which I prefer to use. Hi Matt: I've just started using gvim for python after several years of using xemacs for python programming. Currently I'm using vim.python on kubuntu 7.04 with the python interpreter embedded, along with the taglist.vim and python_box.vim plugins. They go a long way towards an "IDE" for python. python_box has two methods to test and execute whole files. You can also test simple python code snippets from ex as in :python help(dict) and I've barely begun to scratch the surface . Tim ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Invoking Python from Vim
"Matt Smith" <[EMAIL PROTECTED]> wrote > Do any Vim users have a better way of running a Python program while > it > is being edited in Vim? My personal preference is to have 3 windows open: 1) gvim for editing the files 2) a console for running the files using command recall to do so 3) a console running a python shell prompt (actually nowadays I'm using a a PyCrust shell) And I alt-tab between the windows. This way I can experiment in PyCrust, copy the resultant experiments into gvim and run the program, keeping the error messages etc visible without sacrificing gvim window space. The downside is the alt-tab required to switch to the right window and an up-arrow press to recall the python command to re-execute each time. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Questions about easygui and compiling
Hello, I am very new to python and have a couple of questions, to which I would very much appreciate answers from the members of this list. First, I am trying to learn to use easygui to present simple dialogs and get input from the user. To this end, I wrote a very short test program, just to make sure I new how to use easygui. Although I get no error messages when I run it, the dialog boxes do not appear. What might I be doing wrong? It may be helpful to know (a) that I am learning python on a Mac iBook g4 (running OS 10.4), (b) that I wrote the test program using the Smultron text editor, and (c) that I am using python launcher to run the program. I would be only too happy to send a copy of the program as an attachment or pasted in the body of a future e-mail message, should someone on this list feel that doing so would be helpful in answering my question. Second, how does one compile python programs? Cordially, Rafael Bejarano ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Questions about easygui and compiling
I have no experience with EasyGUI, but to answer your other question, you do not "compile" Python in the way you compile C or Java. Python can be compiled into a .pyc file, which is slightly faster to start running and obfuscates the source, but it doesn't really do much, practically. You can also use Py2EXE or PyInstaller to make standalone executables for Windows. Simply running a script through the Python interpreter is as far as you can get for now, although there is a project, name "Psycho", which translates Python to x86 Assembly, though it is only available for Windows and Linux, and doesn't always speed things up. Hope that helps! Rafael Bejarano wrote: > Hello, > > I am very new to python and have a couple of questions, to which I > would very much appreciate answers from the members of this list. > > First, I am trying to learn to use easygui to present simple dialogs > and get input from the user. To this end, I wrote a very short test > program, just to make sure I new how to use easygui. Although I get > no error messages when I run it, the dialog boxes do not appear. What > might I be doing wrong? It may be helpful to know (a) that I am > learning python on a Mac iBook g4 (running OS 10.4), (b) that I wrote > the test program using the Smultron text editor, and (c) that I am > using python launcher to run the program. I would be only too happy > to send a copy of the program as an attachment or pasted in the body > of a future e-mail message, should someone on this list feel that > doing so would be helpful in answering my question. > > Second, how does one compile python programs? > > Cordially, > Rafael Bejarano > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor