sys.stdin.readline()
Alex Martelli
aleaxit at yahoo.com
Wed Sep 1 07:26:23 EDT 2004
Mike Maxwell <maxwell at ldc.upenn.edu> wrote:
...
> The guy down the hall does these kinds of things with perl one-liners,
> but I have more dignity than to use perl... Unfortunately, it's looking
> more and more complex to do one-liners like this in Python. Am I
> overlooking s.t.?
No, I think you're correctly observing that Python isn't oriented to
one-liners -- not at all. Most interesting things in Python require
more than one line. You could write a "oneliners shell" that takes some
defined separator and turns it into a newline, of course, e.g.:
bangoneliner.py:
#!/usr/bin/env python
import sys
oneliner = sys.argv.pop(1)
exec '\n'.join(oneliner.split('!'))
now, sometying like
bangoneliner.py 'for x in xrange(7):! if x%2:! print x'
should work (note that inserting the spaces after the bangs to simulate
proper indentation IS a silly fuss, but you hafta...:-).
Alex
More information about the Python-list
mailing list