File tree Expand file tree Collapse file tree 1 file changed +104
-1
lines changed Expand file tree Collapse file tree 1 file changed +104
-1
lines changed Original file line number Diff line number Diff line change 1- README!
1+ # install
2+ sudo pip install pythonpy; alias py='pythonpy'
3+
4+ # float arithmetic
5+ $ py '3 * 1.5'
6+ 4.5
7+
8+ # exponentiation
9+ $ py '7**3'
10+ 343
11+
12+ # number sequence
13+ $ py 'range(3)'
14+ 0
15+ 1
16+ 2
17+
18+ # list comprehensions
19+ $ py '[x**2 for x in range(1 ,5)]'
20+ 1
21+ 4
22+ 9
23+ 16
24+
25+ # math library usage
26+ $ py 'math.exp(1)'
27+ 2.71828182846
28+
29+ # random library usage
30+ $ py 'random.random()'
31+ 0.103173957713
32+
33+ # multiply each line of input by 7.
34+ $ py 'range(3) | py -x 'int(x)*7'
35+ 0
36+ 7
37+ 14
38+
39+ # append ".txt" to each line of input
40+ $ py 'range(3)' | py -x 'x + ".txt"'
41+ 0.txt
42+ 1.txt
43+ 2.txt
44+
45+ # Sometimes you want to treat the input as a python list.
46+ # reverse a list
47+ $ py 'range(4)' | py -l 'sorted(l , reverse=True)'
48+ 3
49+ 2
50+ 1
51+ 0
52+
53+ # sum a list of numbers
54+ $ py 'range(4)' | py -l 'sum(int(x) for x in l)'
55+ 6
56+
57+ # count the lines of input
58+ $ py 'range(17)' | py -l 'len(l)'
59+ 17
60+
61+ # Other times you just want to filter out lines from the input.
62+ # get only even numbers
63+ $ py 'range(8)' | py -x 'x if int(x)%2 == 0 else None'
64+ 0
65+ 2
66+ 4
67+ 6
68+
69+ # The shorthand -fx (filter on x) is also available.
70+ # get only odd numbers
71+ $ py 'range(8) | py -fx 'int(x)%2 == 1'
72+ 1
73+ 3
74+ 5
75+ 7
76+
77+ # get words starting with "and"
78+ $ cat /usr/share/dict/words | py -fx 're.match(r"and" , x)' | head -5
79+ and
80+ andante
81+ andante's
82+ andantes
83+ andiron
84+
85+ # get verbs starting with ba
86+ $ cat /usr/share/dict/words | py -fx 're.match(r"ba.*ing$" , x)' | head -5
87+ baaing
88+ babbling
89+ babying
90+ babysitting
91+ backbiting
92+
93+ # get long palindromes
94+ $ cat /usr/share/dict/words | py -fx 'x==x[::-1] and len(x) >= 5' | head -5
95+ civic
96+ deified
97+ kayak
98+ level
99+ ma'am
100+
101+ # ignore AttributeErrors if they pop up with (--i).
102+ # get the local network ip
103+ $ ifconfig | py -x --i 're.search(r"192\.168[\d\.]+" , x).group()'
104+ 192.168.1.41
You can’t perform that action at this time.
0 commit comments