PyOne

Python One-liner helper

back [Japanese]

Last Modified: Wed Feb 26 01:41:54 EST 2003 (02/26, 15:41 JST)

What's it?

PyOne is a helper script for quick & dirty one-liner in Python. The idea is somewhat similar to Pyawk, but PyOne supports more instant interface. Basically it performs macro expansion and eval.

PyOne is NOT RECOMMENDED for beginners. One-liner scripts are neither easy to debug nor easy to reuse. This is for those who are Python lovers, but still captives of Perl one-liner.

(Updated, May 13/2004: I've noticed this appeared at Daily Python url... But believe me, after trying this, I found using indentation with vi is still way easier than this!)


Download


Description

It converts a given script to properly indented Python code and executes it. If a single expression is given it simply eval it and displays the the retuen value.

Usage

pyone [-d] [-i modules] [-f modules] script args ...

Options

-d
debug mode. (dump the expanded code and exit)
-i modules
add 'import modules' at the beginning of the script.
-f modules
add 'from modules import *' for each module.

Syntax

;
inserts a newline and make proper indentation.
Ex. "A; B; C" is expanded as
       A
       B
       C
       
{ ... }
makes the inner part indented.
Ex. "A { B; C }" is expanded as
       A:
         B
         C
       
EL{ ... }
wraps the inner part as a loop executed for each line of files specified by the command line (or stdin).
NOTE: Don't separate "EL" and "{".

The following variables are available inside the loop.

Precisely, it inserts the folloing code:

       L = -1
         while 1:
         S = getnextline(args)
         if not S: break
         L = L + 1
         s = string.strip(S)
         F = string.split(s, DELIM)
         I = map(toint, F)
         (... your code here ...)
       

Special variables

DELIM
field separator used in EL { } loop.
args[]
command line arguments.

Examples

    $ pyone 2+3*5.6
    $ pyone -f cdb 'd=init("hoge.cdb");EL{if d.get(F[0]): print s}' testfiles
    $ wget -q -O- http://slashdot.org/ | \
      pyone -f sgmllib 't=[""];class p(SGMLParser){def handle_data(s,x){global t;t[-1]+=x;} \
                        def start_td(s,x){t.append("")} def end_td(s){print t[-1];del(t[-1])}} \
                        x=p(); EL{x.feed(s)}'

Author

Yusuke Shinyama (yusuke at cs . nyu . edu)


Yusuke Shinyama