(embedding) running Python as coprocess

William Park opengeometry at yahoo.ca
Wed Feb 19 18:32:50 EST 2003


sismex01 at hebmex.com wrote:
> How does it work?
> Any usage examples you might want to share?
> 
> Sounds very interesting :-)

Well, if you are asking about my extension of Bash with Python
embedded...  The syntax mirrors the normal Python, ie.
    embeddedpython scriptfile
    embeddedpython -c "string"
except that Python doesn't terminate between calls, so you can access
data from previous call.

For example,
    embeddedpython -c "import math"
    embeddedpython -c "print math.pi"	--> 3.14159265359
If file 'aaa' has content,
    A = [1,2]
    B=['a','b']
Then, you can do
    embeddedpython aaa
    embeddedpython -c "print math.pi, A+B"	--> 3.14159265359 [1, 2, 'a', 'b']

With the normal Python, each call is separate invocation.  With embedded
Python, however, each call is just continuation of previous calls, so
you get access to previous data.

Interestingly, if you run Python in subshell, then any modifications
don't percolate back up to the parent Python.  This is normal for shell,
though.  For example,
    ( embeddedpython -c "B=[11,12]"
      embeddedpython -c "print A, B" )	--> [1, 2] [11, 12]
But, back in the main shell,
    embeddedpython -c "print A, B"	--> [1, 2] ['a', 'b']

-- 
William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
Linux solution for data management and processing. 




More information about the Python-list mailing list