1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
Installation
------------
::
pip install pythonpy
::
Usage
-----------------------------------------------
Pythonpy will evaluate any python expression from the command line.
Float Arithmetic
~~~~~~~~~~~~~~~~
::
$ py '3 * 1.5'
4.5
::
Import any module automatically
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
$ 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 -x 'foo(x)' will apply foo to each line of input
---------------------------------------------------
Multiply each line of input by 7
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
$ py 'range(3)' | py -x 'int(x)*7'
0
7
14
::
Grab the second column of a csv
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
$ echo $'a1,b1,c1\na2,b2,c2' | py -x 'x.split(",")[1]'
b1
b2
::
Append ".txt" to every file in the directory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
$ ls | py -x '"mv `%s` `%s.txt`" % (x,x)' | sh
# sharp quotes are swapped out for single quotes
# single quotes handle spaces in filenames
::
Remove every file returned by the find command
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
$ find . -type f | py -x '"rm %s" % x' | sh
::
Get only 2 digit numbers
~~~~~~~~~~~~~~~~~~~~~
::
$ py 'range(14)' | py -x 'x if len(x) == 2 else None'
10
11
12
13
::
py -l will set l = list(sys.stdin)
-------------------------------------------
Lists are printed row by row
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
$ py 'range(3)'
0
1
2
$ py '[range(3)]'
[0, 1, 2]
::
Reverse the input
~~~~~~~~~~~~~~~~~
::
$ py 'range(3)' | py -l 'l[::-1]'
2
1
0
::
Sum the input
~~~~~~~~~~~~~
::
$ py 'range(3)' | py -l 'sum(int(x) for x in l)'
3
::
Sort a csv by the second column
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
$ echo $'a,2\nb,1' | py -l 'sorted(l, key=lambda x: x.split(",")[1])'
b,1
a,2
::
Count words beginning with each letter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
$ 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)
::
For more examples, check out the `wiki <http://github.com/Russell91/pythonpy/wiki>`__.
Pythonpy also supports ipython style tab completion, which you can enable as follows
::
$ if command -v find_pycompletion.sh>/dev/null; then source `find_pycompletion.sh`; fi
::
|