forked from pixelb/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncpy
More file actions
executable file
·340 lines (295 loc) · 9.68 KB
/
funcpy
File metadata and controls
executable file
·340 lines (295 loc) · 9.68 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/python
# Process lines from stdin with python functions
# Functions are either predefined or user supplied,
# the latter being similar to perl -n -e 'expression'.
# Update June 9 2009, I noticed a similar script called pyline:
# http://code.activestate.com/recipes/437932/
# Update March 23 2012, Another similar tool called pyp
# http://code.google.com/p/pyp/
# Update May 24 2012, Another similar tool called osh
# http://geophile.com/osh/
# Update Sep 2015, Another similar tool called red (regexp)
# https://bitbucket.org/johannestaas/red
# License: LGPLv2
# Author:
# http://www.pixelbeat.org/
# Notes:
# This util uses python's functional attributes to be:
# 1. scalable (don't build lists in mem so arbitrary length input supported)
# 2. efficient (as many operations done in compiled code as possible)
# Changes:
# V1.0, 01 Nov 2006, Initial release
# V1.1, 30 Nov 2007, Added 'uniq' (example of many to many)
# V1.6, 11 Jul 2011, http://github.com/pixelb/scripts/commits/master/scripts/funcpy
# Examples:
#
# add numbers
# echo -e "1234\n433" | ./funcpy add
#
# The rest of the examples use user supplied expressions
# (where the input line is represented by x)
#
# transform numbers
# echo -e "1234\n433" | ./funcpy "hex(int(x))[2:]"
#
# operate on single column
# echo "1,2,3" | ./funcpy "int(x.split(',')[1])**2"
#
# reiterate over sub items (only reducable functions)
# echo "1,2,3" | ./funcpy "add(int(i) for i in x.split(','))"
#
# swartzian transform (to sort by line len) (note sort not as efficient or scalable as max)
# ls | ./funcpy "str(len(x))+'\t'+x" | sort -k1,1n | cut -f2
#
# filtering input (odd numbers) (note "or None" at end)
# echo -e "1\n2\n3" | ./funcpy "int(x)%2 and x or None"
#
# formating with printf
# echo "12341234.432142" | ./funcpy '"%g" % float(x)'
#
# arbitrary modules
# ls | ./funcpy --import=base64 "base64.encodestring(x)[:-1]"
#
# similar to perl
# echo -e "1\n5" | perl -n -e 'print $_**2,"\n"'
import itertools
import operator
import math
import sys
used_compiled=True #Turn off for comparison
#is swartzian transform functional?
#can we add reducable decorator to functions?
#TODO: allow option=expression to filter input if true rather than and or hack?
#TODO: put human() in here?
def reduce(func, items, start=None): #foldl
try:
items=iter(items)
except:
raise TypeError("reduce() arg 2 must support iteration")
if start==None:
try:
state=items.next()
except StopIteration:
raise TypeError("reduce() of empty sequence with no initial value")
else:
state=start
for i in items:
state = func(state,i)
return state
if used_compiled:
try: #builtin removed in python 3.0
reduce=__builtins__.reduce
except: #here in python 2.6, only here in python 3.0
import functools
reduce=functools.reduce
def imap(func, iter):
for i in iter:
yield func(i)
if used_compiled:
if sys.version_info < (3,):
imap=itertools.imap
else:
imap=map
def izip(iter1, iter2):
for i in iter1:
yield (i, iter2.next()) #exception raised if either exhausted as required
if used_compiled:
if sys.version_info < (3,):
izip=itertools.izip
else:
izip=zip
def irange(*args):
start=0
step=1
if len(args) == 0:
raise TypeError("irange expected at least 1 argument, got 0")
elif len(args) == 1:
(stop,) = args
elif len(args) == 2:
(start,stop) = args
elif len(args) == 3:
(start,stop,step) = args
else:
raise TypeError("irange expected at most 3 arguments, got %d" % len(args))
while start < stop:
yield start
start += step
if used_compiled:
if sys.version_info < (3,):
irange=xrange
else:
irange=range
def all(iter): #every?
return False not in imap(bool,iter)
def any(iter): #some?
return True in imap(bool,iter)
if used_compiled:
try: # any() and all() are available since python 2.5
all=__builtins__.all
any=__builtins__.any
except:
pass
def factorial(x):
return reduce(operator.mul, irange(1,int(x)+1))
def add(iter):
return reduce(operator.add, iter)
if used_compiled:
add=__builtins__.sum #should be slightly more specific (faster)
def head(iterator, n=10):
return (x for x,i in izip(iterator, irange(n)))
# Note the following more general inspector
# return itertools.islice(iterator,n)
#def tail(iterator, n=10):
# import collections
# return collections.deque(iterator, maxlen=n)
def count(iterator):
return add(1 for x in iterator)
def uniq(iterator):
state=None
for line in iterator:
if line != state:
yield line
state=line
# Using the groupby equivalent is slightly slower:
# return (k for k, g in itertools.groupby(iterator))
def uniq_c(iterator):
return ("%7d %s" % (count(g), k)
for k, g in itertools.groupby(iterator))
def uniq_d(iterator):
return (k for k, g in itertools.groupby(iterator) if count(head(g,2)) > 1)
def min(iter):
state=None
for i in iter:
if state > i: state=i
return state
if used_compiled:
min=__builtins__.min
def max(iter):
state=None
for i in iter:
if i > state: state=i
return state
if used_compiled:
max=__builtins__.max
def avg(iterator):
try:
# Note math.fsum (available since 2.6) doesn't lose precision
# while adding lots of small numbers
iter1, iter2 = itertools.tee(iterator)
return math.fsum(iter1)/count(iter2)
except:
total=0
lenth=0
for i in iterator:
total+=i
length+=1
return total/length
def dec2hex(i):
return __builtins__.hex(int(i))[2:]
def hex2dec(i):
return int(i,16)
def bits(i):
if i<0: raise ValueError("negative number [%d]" % i)
if i==0: return 1
#shift operator slower
return int(math.log(i,2))+1
#=============================================================
import os, errno
# The following exits cleanly on Ctrl-C or EPIPE
# while treating other exceptions as before.
def std_exceptions(etype, value, tb):
sys.excepthook=sys.__excepthook__
if issubclass(etype, KeyboardInterrupt):
pass
elif issubclass(etype, IOError) and value.errno == errno.EPIPE:
pass
else:
sys.__excepthook__(etype, value, tb)
sys.excepthook=std_exceptions
functions={
#name: (func, input, type)
'add': (add, float, 'mto1'),
'max': (max, float, 'mto1'),
'min': (min, float, 'mto1'),
'avg': (avg, float, 'mto1'),
'all': (all, float, 'mto1'),
'any': (any, float, 'mto1'),
'count': (count, str, 'mto1'),
'head': (head, str, 'mtom'),
'uniq': (uniq, str, 'mtom'),
'uniq_c': (uniq_c, str, 'mtom'),
'uniq_d': (uniq_d, str, 'mtom'),
#Following really of minimal use
#'dec2hex': (dec2hex, str, '1to1'),
#'hex2dec': (hex2dec, str, '1to1'),
#'factorial': (factorial, int, '1to1'),
#'log10': (math.log10, float, '1to1'),
#'bits': (bits, long, '1to1'),
}
def Usage():
sys.stderr.write("Usage: " + os.path.split(sys.argv[0])[1] + " [OPTIONS] <" + '|'.join(sorted(functions.keys())) + ">\n")
sys.stderr.write(" " + os.path.split(sys.argv[0])[1] + " [OPTIONS] <" + "python expression where input is x>\n")
sys.stderr.write("Input is taken from stdin, and is processed per line\n")
sys.stderr.write("Note python expressions must be stateless (no ifs or assigments)\n")
sys.stderr.write(" --imports=mod1,mod2,... modules to import for use in python expression\n")
sys.stderr.write(" --help display this help\n")
sys.stderr.close()
sys.exit(1)
import getopt
try:
lOpts, lArgs = getopt.getopt(sys.argv[1:], "", ["help","imports="])
if len(lArgs) == 1:
mode=lArgs[0]
else:
Usage()
sys.exit(1)
if ("--help","") in lOpts:
Usage()
sys.exit(None)
for opt in lOpts:
if opt[0] == "--imports":
for mod in opt[1].split(","):
globals()[mod]=__import__(mod)
except getopt.error:
msg = sys.exc_info()[1]
sys.stdout.write("%s\n" % msg)
Usage()
sys.exit(2)
#=============================================================
try:
func=functions[mode][0]
conv=functions[mode][1]
ftype=functions[mode][2]
except:
#shows the flexibility of interpretation over compilation
code=compile(mode,"<expression>","eval")
func=lambda x: eval(code)
conv=lambda x: x[-1]=='\n' and x[:-1] or x #strip EOL
ftype='1to1'
def convert(iterator):
if conv==str: #shortcut for speed
return iterator
else:
return (conv(line) for line in iterator)
try:
if ftype=='mto1': #many to one
result = func(convert(sys.stdin))
if type(result)==bool:
sys.exit(not result)
else:
sys.stdout.write("%s\n" % result)
elif ftype=='mtom': #many to many
#for result in func(convert(sys.stdin)):
# sys.stdout.write(result)
#note the following is faster but it buffers output
#even if stdout is a terminal. writelines really shouldn't do that.
sys.stdout.writelines(func(convert(sys.stdin)))
else: #one to one
for num in convert(sys.stdin):
result = func(num)
if result!=None:
sys.stdout.write("%s\n" % result)
except ValueError:
value = sys.exc_info()[1]
sys.stderr.write(str(value)+"\n")
sys.exit(1)