-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyline.py
39 lines (33 loc) · 1.07 KB
/
pyline.py
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
#!/usr/bin/env python
## {{{ http://code.activestate.com/recipes/437932/ (r3)
# updated 2005.07.21, thanks to Jacob Oscarson
# updated 2006.03.30, thanks to Mark Eichin
import sys
import re
import getopt
# parse options for module imports
opts, args = getopt.getopt(sys.argv[1:], 'm:')
opts = dict(opts)
if '-m' in opts:
for imp in opts['-m'].split(','):
locals()[imp] = __import__(imp.strip())
cmd = ' '.join(args)
if not cmd.strip():
cmd = 'line' # no-op
codeobj = compile(cmd, 'command', 'eval')
write = sys.stdout.write
for numz, line in enumerate(sys.stdin):
line = line[:-1]
num = numz + 1
words = [w for w in line.strip().split(' ') if len(w)]
result = eval(codeobj, globals(), locals())
if result is None or result is False:
continue
elif isinstance(result, list) or isinstance(result, tuple):
result = ' '.join(map(str, result))
else:
result = str(result)
write(result)
if not result.endswith('\n'):
write('\n')
## end of http://code.activestate.com/recipes/437932/ }}}