Last active
October 9, 2015 23:28
-
-
Save aphlysia/3595816 to your computer and use it in GitHub Desktop.
pyone: python one liner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
help = ''' | |
python one liner | |
Usage: pyone [OPTIONS] code [OPTIONS] [--] [file1, file2, ...] | |
-e code | |
-E code | |
-l | |
-Mmodule[,module,...] | |
-n | |
-p | |
--pre=code | |
--post=code | |
--ic=encoding | |
--oc=encoding | |
--eerror=strict, ignore, or replace, ... | |
e.g. | |
$ pyone "print(sys.platform)" | |
$ pyone "0 and print('a')" | |
$ pyone "1 and print('a')" | |
$ pyone "0 or print('a')" | |
$ pyone "1 or print('a')" | |
$ pyone -nle "print(_ * 2)" < hoge.txt | |
$ pyone -ple "_ = _ * 2" < hoge.txt | |
$ pyone -ple "_ = _ * 2" *.txt | |
$ pyone -Mimp -e "print(imp.__name__)" | |
$ pyone --pre="print(0)" --post="print(2)" "print(1)" | |
$ pyone --ic=euc-jp --oc=utf-8 --error=replace -ple 0 hoge.txt | |
$ pyone -ple "_ = _ if int(_) % 2 == 0 else _noprint" | |
If -n or -p option is specified, code is executed for each line, and variable _ refers each line. | |
If -p option is specified, _ is printed after execution. | |
_argv variable has arguments. | |
$ pyone -e "print(_argv)" a b | |
['a', 'b'] | |
''' | |
import sys | |
import codecs | |
from collections import defaultdict | |
def readArgv(): | |
options = defaultdict(lambda: None) | |
options['M'] = set() | |
options['eerror'] = 'strict' | |
isProgram = False | |
isFile = False | |
_argv = [] | |
for item in sys.argv[1:]: | |
if isProgram: | |
options['e'] = item | |
isProgram = False | |
continue | |
if isFile: | |
_argv.append(item) | |
elif item == '--': | |
isFile = True | |
continue | |
elif item.startswith('--pre='): | |
options['pre'] = item[6:] | |
elif item.startswith('--post='): | |
options['post'] = item[7:] | |
elif item.startswith('--ic='): | |
options['ic'] = item[5:] | |
elif item.startswith('--oc='): | |
options['oc'] = item[5:] | |
elif item.startswith('--eerror='): | |
options['eerror'] = item[9:] | |
elif item[0] == '-': | |
if 'M' == item[1]: | |
options['M'] |= set(item[2:].split(',')) | |
continue | |
if 'p' in item[1:]: | |
options['p'] = True | |
if 'n' in item[1:]: | |
options['n'] = True | |
if 'l' in item[1:]: | |
options['l'] = True | |
if item[-1] in ('e', 'E'): | |
isProgram = True | |
elif not options['e']: | |
options['e'] = item | |
else: | |
isFile =True | |
_argv.append(item) | |
return options, _argv | |
class NoPrint: pass | |
if __name__ == '__main__': | |
options, argv = readArgv() | |
for module in options['M']: | |
exec('import ' + module, globals()) | |
_noprint = NoPrint() | |
context = globals().copy() | |
if not options['e']: | |
print(help) | |
exit() | |
if options['oc']: | |
if sys.version_info[0] >= 3: | |
sys.stdout = codecs.getwriter(options['oc'])(sys.stdout.detach()) | |
else: | |
sys.stdout = codecs.getwriter(options['oc'])(sys.stdout) | |
if options['pre']: | |
exec(options['pre'], context) | |
if options['p'] or options['n']: | |
if len(argv) > 0: | |
for filename in argv: | |
try: | |
file = codecs.open(filename, 'r', encoding=options['ic'], errors=options['eerror']) | |
for _ in file: | |
if options['l']: | |
_ = _.rstrip('\r\n') | |
context['_'] = _ | |
exec(options['e'], context) | |
if options['p'] and context['_'] is not _noprint: | |
print(context['_']) | |
finally: | |
file.close | |
else: | |
if options['ic']: | |
if sys.version_info[0] >= 3: | |
sys.stdin = codecs.getreader(options['ic'])(sys.stdin.detach()) | |
else: | |
sys.stdin = codecs.getreader(options['ic'])(sys.stdin) | |
for _ in sys.stdin: | |
if options['l']: | |
_ = _.rstrip('\r\n') | |
context['_'] = _ | |
exec(options['e'], context) | |
if options['p'] and context['_'] is not _noprint: | |
print(context['_']) | |
else: | |
context['_argv'] = argv | |
exec(options['e'], context) | |
if options['post']: | |
exec(options['post'], context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment