sys.stdin.readline()

Alex Martelli aleaxit at yahoo.com
Wed Sep 1 08:25:31 EDT 2004


Mike Maxwell <maxwell at ldc.upenn.edu> wrote:

> Alex Martelli wrote:
> > Mike Maxwell <maxwell at ldc.upenn.edu> wrote:
> > 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.  
> 
> <rant>
> I don't care whether it's "interesting", I just want to get some work
> done.

Something that lets you get work done IS thereby interesting.  Most
interesting things in Python require more than one line.  So, I don't
see the basis for your rant.

>  And since most of the text processing tools in Unixes that I 
> would otherwise use (grep, sed, tr) don't support Unicode, and are 
> inconsistent in their regular expression notation to boot, it would be
> nice if I could write regex operations in a single, consistent 
> programming language.  Python is a single, consistent programming 
> language, but as you say, it doesn't lend itself to one-liners.

No, but a supporting script similar to the one I suggest below can
easily be adapter to offer more sensible functionality than any oneliner
might sensibly support -- for example (for the kinds of tasks you imply)
by including and automatically using such modules as re and fileinput.

> > now, sometying like
> > 
> > bangoneliner.py 'for x in xrange(7):!  if x%2:!    print x'
> > 
> > should work 
> 
> Hmm, I may give that a try...thanks!

You're welcome.


> > note that inserting the spaces after the bangs to simulate
> > proper indentation IS a silly fuss, but you hafta...:-).
> 
> Well, I guess I could translate some other char (one that's easier to
> count than spaces) into indents, too.

Sure, or you could use (e.g.) '!3' to translate into 'newline then three
spaces', or use block start/endmarkers and translate them into
indents/dedents, etc, etc.

Personally, given your now-restated problem, that you need 'better'
versions of grep, sed and tr, I would take another tack -- I would
reimplement _those_ in Python with its re sublanguage and Unicode
support.  Using them should be easier and tighter than putting newliners
together, I think.


Alex



More information about the Python-list mailing list