UPDATE 20220225: Please consider using https://github.com/edk0/spy/ instead, as it seems much better maintained, organized, and all-around like a better choice than using this probject.
Forked from MatthieuBizien/pythonpy
$ pipx install git+https://github.com/n8henrie/pythonpy
$ echo foo | py -x 'print(x + "bar")'
foobar
$ echo -e '1\n2\n3' | py -l 'sum(map(int, l))'
6Makes for much easier piping. Comparison with python3 -c:
$ echo -n foo | py -x 'x'
foo
$ echo -n foo | python3 -c 'import sys; x=sys.stdin.read(); print(x)'
foo
$ echo -ne '1\n2\n3' | py -l 'sum(map(int, l))'
6
$ echo -ne '1\n2\n3' | python3 -c 'import sys; l=[line.strip() for line in sys.stdin.readlines()]; print(sum(map(int, l)))'
6$ py '3 * 1.5'
4.5$ py 'math.exp(1)'
2.71828182846
$ py 'random.random()'
0.103173957713
$ py 'datetime.datetime.now?'
Help on built-in function now:
now(...)
[tz] -> new datetime with tz's local day and time.$ py 'range(3)'
0
1
2
$ py '[range(3)]'
[0, 1, 2]$ py 'range(3)' | py -x 'int(x)*7'
0
7
14$ py 'range(3)' | py -x 'x + ".txt"'
0.txt
1.txt
2.txt- sharp quotes are swapped out for single quotes
- single quotes handle spaces in filenames
$ ls | py -x '"mv `%s` `%s`" % (x,x)' | sh -x
+ mv MANIFEST.in MANIFEST.in
mv: 'MANIFEST.in' and 'MANIFEST.in' are the same file
+ mv README.md README.md
mv: 'README.md' and 'README.md' are the same file
+ mv build build
mv: cannot move 'build' to a subdirectory of itself, 'build/build'
+ mv pythonpy pythonpy
mv: cannot move 'pythonpy' to a subdirectory of itself, 'pythonpy/pythonpy'
+ mv pythonpy.egg-info pythonpy.egg-info
mv: cannot move 'pythonpy.egg-info' to a subdirectory of itself, 'pythonpy.egg-info/pythonpy.egg-info'
+ mv setup.py setup.py
mv: 'setup.py' and 'setup.py' are the same file
+ mv test test
mv: cannot move 'test' to a subdirectory of itself, 'test/test'
+ mv tox.ini tox.ini
mv: 'tox.ini' and 'tox.ini' are the same file$ py 'range(8)' | py -x 'x if int(x)%2 == 0 else None'
0
2
4
6$ py 'range(8)' | py -fx 'int(x)%2 == 1'
1
3
5
7$ cat /usr/share/dict/words | py -fx 'x.startswith("and")' | head -5
and
andante
andante's
andantes
andiron$ cat /usr/share/dict/words | py -fx 're.match(r"ba.*ing$", x)' | head -5
baaing
babbling
babying
babysitting
backbiting$ cat /usr/share/dict/words | py -fx 'x==x[::-1] and len(x) >= 5' | head -5
civic
deified
kayak
level
ma'am$ py 'range(3)' | py -l 'l[::-1]'
2
1
0$ py 'range(3)' | py -l 'sum(int(x) for x in l)'
3$ py 'range(17)' | py -l 'len(l)'
17$ cat /usr/share/dict/words | py -x 'x[0].lower()' | py -l 'collections.Counter(l).most_common(5)'
('s', 11327)
('c', 9521)
('p', 7659)
('b', 6068)
('m', 5922)- WIP
- I'm not sure whether I'll worry about the bash completion
- Licensing: multiple authors, listed as MIT. https://github.com/Russell91
seems to have disappeared, still owns
https://pypi.org/project/pythonpy/./LICENSErefers to my changes and modifications, with much gratitude to the original authors for all the heavy lifting!