[Tutor] Re: Translating to Python [perl --> python]

Lloyd Kvam pythontutor@venix.com
Tue, 02 Jul 2002 10:34:44 -0400


try:
     c = form['c'].value
except KeyError:
     c = ""

The except statement must be paired with a try.  Also, I hope that
the lack of indentation is an artifact of your email program.
Python uses indentation  rather than braces or other character
markers to indicate the extent of "code blocks". So for instance:

if <something is true>:
	<block of code for
	this being true is indented>
elif <some other thing>:
	<also indented >
else:
	<indented block for neither thing true>
<back to main flow with no indent>

Kyle Babich wrote:

> Ok, well then something is still wrong because I'm still getting syntax
> errors that I can't figure out.  Is there a python tutorial that is
> more like cgi101.com (only python)?  (Ie. including source codes, live
> working examples, and different sections for different commands)
> 
> Btw, here is the code again:
> 
> #!/usr/bin/env python
> 
> import cgi
> 
> print "Content-type: text/html\n\n"
> 
> form = cgi.FieldStorage()
> c = form['c'].value
> except KeyError :
> c = ""
> content = c
> if c == 'abc':
> content = "abc123"
> elif c == 'def':
> content = "def456"
> else:
> print "error: content failed\n"
> 
> file = open("text.txt")
> text = file.read()
> file.close()
> 
> print """
> 
> %(content)s
> 
> 
> <br>&nbsp;<br>&nbsp;<br>&nbsp;
> 
> %(text)s
> 
> """ % vars()
> 
> 
> It says there is an error near except KeyError: but I've never used
> python before so I don't know what to change.
> 
> Thanks,
> Kyle
> 
> On Tue, 02 Jul 2002 09:06:06 -0400, "Lloyd Kvam"
> <pythontutor@venix.com> said:
> 
>>(I presume Danny is asleep by this time.)
>>Simply delete the two lines that contain cgitb references, or make
>>them comments:
>>#import cgitb
>>#cgitb.enable()
>>
>>cgitb ONLY comes into play if your script fails and raises an
>>exception.
>>You can omit cgitb without affecting your normal processing.
>>
>>Kyle Babich wrote:
>>
>>
>>>I would like to do the entire thing without cgitb because I don't have
>>>the module and I don't want to try to convincing my host to install it.
>>> Is it possible to do it without cgitb?
>>>
>>>On Tue, 2 Jul 2002 01:16:11 -0700 (PDT), "Danny Yoo"
>>><dyoo@hkn.eecs.berkeley.edu> said:
>>>
>>>
>>>>On Mon, 1 Jul 2002, Kyle Babich wrote:
>>>>
>>>>
>>>>
>>>>>How could I do this without cgitb?
>>>>>
>>>>>
>>>>Hi Kyle,
>>>>
>>>>Which part, exactly, would you like to do without the 'cgitb' traceback
>>>>module?  cgitb modifies the programming environment slightly so that
>>>>raised exceptions give nicely formatted error messages back to the web
>>>>browser.
>>>>
>>>>
>>>>Try commenting the cgitb lines first, and deliberately introduce a
>>>>syntax
>>>>error in the script --- you'll notice that the web server doesn't
>>>>output
>>>>too much useful information back to the user (although it will be in
>>>>the
>>>>error log).  On Apache, a bug in a CGI script brings up the
>>>>infuriatingly
>>>>uninformative "Internal Server Error" page.
>>>>
>>>>
>>>>Afterwards, uncomment the cgitb-related lines and try again, and you'll
>>>>see a difference.  It's not mandatory, but it is pretty useful.  I
>>>>usually
>>>>keep 'cgitb' on because it's such a nice debugging tool, and looks
>>>>nicer
>>>>than having to do a:
>>>>
>>>>   tail -f /var/log/apache/error.log
>>>>
>>>>which would be an alternative way of watching script problems.
>>>>
>>>>
>>>>If you have more questions, please feel free to ask!  By the way, it
>>>>sounds like you're coming from a Perl background.  How much Perl do you
>>>>know already?  We can tailor our answers to take advantage of your
>>>>experiences in other languages, if that would be helpful for you.
>>>>
>>>>
>>>>Good luck!
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>>_______________________________________________
>>>Tutor maillist  -  Tutor@python.org
>>>http://mail.python.org/mailman/listinfo/tutor
>>>
>>>
>>>
>>
>>-- 
>>Lloyd Kvam
>>Venix Corp.
>>1 Court Street, Suite 378
>>Lebanon, NH 03766-1358
>>
>>voice: 
>>603-443-6155
>>fax: 
>>801-459-9582
>>
>>
>>
>>
>>
>>
> 


-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice: 
603-443-6155
fax: 
801-459-9582