Function which will accept stdin or arguments
function launch_python {
exec 3>&-
exec 4<&-
pyin=$(mktemp -u)
pyot=$(mktemp -u)
mkfifo $pyin
mkfifo $pyot
python -u -i <$pyin &>$pyot &
pypid=$!
exec 3> $pyin
exec 4< $pyot
rm $pyin
rm $pyot
echo print >&3
while read -u 4 line && [ "${line}" != '>>>' ]
do
echo "# ${line}"
done
echo 'import sys;sys.ps2=""' >&3
}
function py_read {
local line
while read -u 4 line && [ "${line}" != 'bithon-eos' ]
do
while [ "${line#>>> }" != "${line}" ]
do
line=${line#>>> }
done
[ ${line} == '>>>' ] && continue
echo "${line}"
done
}
function _ppy {
local line
IFS=''
while read line
do
echo "$line" >&3
done
echo >&3
echo 'print "\nbithon-eos"' >&3
}
# Trick is make "$@" work as lhs of comparison is to prefix _
function py {
if [ "_$@" == '_' ]
then
( _ppy <&0 & )
else
( (echo "$@" >&3; echo 'print "\nbithon-eos"' >&3)& )
fi
py_read
}
function kill_python {
exec 4<&-
exec 3>&-
kill -9 $pypid
}
...
for x in $(ppy << EOF
for x in xrange(10):
print x*x / 0.123
EOF
); do echo $x; done
0.0
8.13008130081
32.5203252033
73.1707317073
130.081300813
203.25203252
292.682926829
398.37398374
520.325203252
658.536585366
...
py "import numpy;numpy.median([$counts])"
46.0
exec 3>&-
exec 4<&-
pyin=$(mktemp -u)
pyot=$(mktemp -u)
mkfifo $pyin
mkfifo $pyot
python -u -i <$pyin &>$pyot &
pypid=$!
exec 3> $pyin
exec 4< $pyot
rm $pyin
rm $pyot
echo print >&3
while read -u 4 line && [ "${line}" != '>>>' ]
do
echo "# ${line}"
done
echo 'import sys;sys.ps2=""' >&3
}
function py_read {
local line
while read -u 4 line && [ "${line}" != 'bithon-eos' ]
do
while [ "${line#>>> }" != "${line}" ]
do
line=${line#>>> }
done
[ ${line} == '>>>' ] && continue
echo "${line}"
done
}
function _ppy {
local line
IFS=''
while read line
do
echo "$line" >&3
done
echo >&3
echo 'print "\nbithon-eos"' >&3
}
# Trick is make "$@" work as lhs of comparison is to prefix _
function py {
if [ "_$@" == '_' ]
then
( _ppy <&0 & )
else
( (echo "$@" >&3; echo 'print "\nbithon-eos"' >&3)& )
fi
py_read
}
function kill_python {
exec 4<&-
exec 3>&-
kill -9 $pypid
}
...
for x in $(ppy << EOF
for x in xrange(10):
print x*x / 0.123
EOF
); do echo $x; done
0.0
8.13008130081
32.5203252033
73.1707317073
130.081300813
203.25203252
292.682926829
398.37398374
520.325203252
658.536585366
...
py "import numpy;numpy.median([$counts])"
46.0
Comments
Post a Comment