forked from lowks/pythonpy
-
Notifications
You must be signed in to change notification settings - Fork 0
More Examples
Russell Stewart edited this page Mar 20, 2015
·
53 revisions
Pythonpy will automatically replace sharp quotes with single quotes before processing an expression:
py '`hello world`' hello world py '"ma`am"' ma'am
Run a statement as you would using python -c with py -c:
py -c 'a = 5; print(`a`)' a
Alternatively:
py -c 'a = 5' 'a' 5
The argument of (-c) will run before the expression. In the rarer case that you need to run a statement after the main expression, you can use the (-C) flag.
Plot data you have access to from the command line using the pyplt alias:
alias pyplt='py -c '"'"'matplotlib.use(`Agg`); from matplotlib import pyplot as plt'"'"' -C '"'"'plt.savefig("output.jpg")'"'"
py '[math.sin(x/10) for x in range(100)]' | pyplt -l 'plt.plot(l)'
Fig1. - output.jpg
Often you only care about one section of a long row. Splitting the input can help you get that part:
echo 'a,b,c' | py --split_input , -x 'x' ['a', 'b', 'c'] echo 'a,b,c' | py --si , -x 'x[1]' b echo 'a b c.' | py --si ' *' -x 'x[-1]' c.
You can split the output when you're done as well:
echo 'a,b,c' | py --si , -x x --so ' ' a b c echo 'a,b,c' | py --si , -x x --so $'\t' a b c echo 'a b c' | py --si ' *' -x x --so $'\n' a b c
Note that the $ in $'\n' is necessary to pass raw strings in bash. See here for discussion.
Pythonpy can also preprocess numeric inputs.
echo '13' | py --ji -x 'x*7' 91