do python's nifty indentation rules spell the death of one-liners?
A. Lloyd Flanagan
alloydflanagan at attbi.com
Wed Apr 16 19:46:54 CEST 2003
Dan Jacobson <jidanni at dman.ddts.net> wrote in message news:<87he90b8rp.fsf at jidanni.org>...
> I need to use one liners every day, especially in Makefiles. I thus
> need to know how to make any kind of python program into a big long one
> liner. However, right out of the starting gates I run into
> $ python -c 'print 2;for i in (1,4):print i'
> File "<string>", line 1
> print 2;for i in (1,4):print i
> SyntaxError: invalid syntax
> This makes no output and returns no error value to the shell:
> $ python -c 'import math' -c 'for i in range(200,500,50): print
You don't have to do these as one liners.
You can use a shell trick called a 'here document'. It lets you put a
set of lines directly in the shell script, and pass them as input to a
program. Here's an example:
python << EOF
y = 0
for i in range(10):
y += i
print i, y
The '<< EOF' tells the script to send everything to python until the
line that contains just EOF (and you can use any label, not just
EOF). So for your example:
python << DONE
for i in (1,4):
python << END_MATH
for i in range(200,500,50):
These all worked fine on HP/UX 10, using korn shell, with python 2.1
(no snide remarks, please, I didn't choose the setup :).
More information about the Python-list