do python's nifty indentation rules spell the death of one-liners?

Grant Edwards grante at visi.com
Wed Apr 16 20:06:49 CEST 2003


In article <a8b7f07a.0304160946.73760d3a at posting.google.com>, A. Lloyd Flanagan wrote:
> 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
>>   i,360/2/math.pi*math.asin(i/1238.4)'
> ...
> 
> 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:
> 
> #!/bin/ksh
> python << EOF
> y = 0
> for i in range(10):
>         y += i
>         print i, y
> EOF
> 
> 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
> print 2
> for i in (1,4):
>      print i
> DONE

But how do you do it in a Makefile?

-- 
Grant Edwards                   grante             Yow!  I want to dress you
                                  at               up as TALLULAH BANKHEAD and
                               visi.com            cover you with VASELINE and
                                                   WHEAT THINS...




More information about the Python-list mailing list