[Chicago] Unexpected indent: secondary damage in syntax error?
Joshua Herman
zitterbewegung at gmail.com
Sat Oct 20 15:01:53 EDT 2018
Dear Christos,
I went over your code and in addition to the try: without except you had
multiple issues with indentation which I have attempted to correct (but I
could have also changed the logic in your program). Also, I refactored
your two trys into one try that goes over the whole loop. I assume this was
also written with python 2.7 and you had print statements that were using
python 3.x print syntax.
Sincerely,
Joshua herman
On Sat, Oct 20, 2018 at 1:39 PM Carl Karsten <carl at personnelware.com> wrote:
> 87 try:
>
> doesn't have an except. which is an error.
> On Sat, Oct 20, 2018 at 1:19 PM Christos Hayward
> <christos.jonathan.hayward at gmail.com> wrote:
> >
> > I'm working on a very amateur firewalled CGI script for a friend's pet
> project, and I am getting an IndentationError on line 106, the line for a
> closing print statement. If I unindent "print" all the way to the left, the
> error remains; if I comment out the (multiline) statement, an error is
> reported at line 112 (the script is 111 lines).
> >
> > I have looked above line 106 for a syntax error that could be causing
> secondary damage, and I haven't been able to put my finger on it yet.
> >
> > What am I doing to trigger a reported IndentationError?
> >
> >
> > Thanks,
> > --
> >
> > Christos Hayward, author, The Best of Jonathan's Corner
> >
> > _______________________________________________
> > Chicago mailing list
> > Chicago at python.org
> > https://mail.python.org/mailman/listinfo/chicago
>
>
>
> --
> Carl K
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> https://mail.python.org/mailman/listinfo/chicago
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20181020/6a689107/attachment.html>
-------------- next part --------------
#!/usr/bin/python
import cgi
import cPickle
import os
import random
import string
import sys
sys.stderr.write('Starting...')
STORAGE = '/home/paul/empathy/pickled'
ACCESS = '/home/paul/logs/empathy_log'
def get_cgi(field, default = ''):
if cgi_form.has_key(field):
return cgi_form[field].value
else:
return default
def load_saved():
global saved
sys.stderr.write('load_saved()\n')
try:
saved = cPickle.load(open(STORAGE))
except:
saved = {'index': 0, 'entries': []}
def save_values():
sys.stderr.write('save_values()\n')
if get_cgi('submitted'):
sys.stderr.write('submitted')
sys.stderr.write(repr(cgi_form.keys()) + '\n')
for key in cgi_form.keys():
try:
parts = key.split('_')
entry = int(parts[1])
slot = int(parts[2])
if slot == 0:
saved['entries'][entry][1] = get_cgi(key)
else:
sys.stderr.write('Entry: ' + str(entry))
sys.stderr.write('Slot: ' + str(slot))
sys.stderr.write(get_cgi(key))
sys.stderr.write(repr(saved['entries'][entry][2]))
saved['entries'][entry][2][slot - 1] = get_cgi(key)
sys.stderr.write(repr(saved['entries'][entry][2]))
except IndexError:
pass
saved['index'] += 1
sys.stderr.write(repr(saved) + '\n')
cPickle.dump(saved, open(STORAGE + '.' + str(os.getpid()), 'w'))
os.rename(STORAGE + '.' + str(os.getpid()), STORAGE)
if get_cgi('entry'):
open(ACCESS, 'a').write('Statement: ' +
saved['entries'][int(get_cgi('entry'))][1] + '\n' + 'Response: ' +
get_cgi('response') + '\n\n')
if __name__ == '__main__':
cgi_form = cgi.FieldStorage()
load_saved()
save_values()
print '''Content-type: text/html
<html>
<head>
<title>Empathy</title>
<style type="text/css">
body, input[type="text"], textarea
{
font-family: Verdana, Arial, sans;
width: 100%;
}
input.header
{
font-weight: bold;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Empathy</h1>
<form action='' method='POST'>
<input type='hidden' name='submitted' value='submitted'>
'''
for index in range(saved['index']):
try:
print '''<input type='text' class='header' name='saved_%s_0'value="%s">''' % (str(index), saved['entries'][index][1])
counter = 0
for current in saved['entries'][index][2]:
counter += 1
if current:
sys.stderr.write(current + '\n')
print '''<input type='text' name='saved_%s_%s' value="%s">''' % (str(index), str(counter), current)
except IndexError:
pass
print '''
<input type='submit'>
</form>
</body>
</html>
'''
More information about the Chicago
mailing list