diff --git a/MANIFEST b/MANIFEST index 69e500c..c217127 100644 --- a/MANIFEST +++ b/MANIFEST @@ -2,4 +2,4 @@ README.txt setup.py bin/pythonpy -pythonpy/__init__.py +test/test_pythonpy.py diff --git a/README.rst b/README.rst index ceab535..07e4751 100644 --- a/README.rst +++ b/README.rst @@ -15,23 +15,23 @@ For a permanent alias (For Bash users): :: -Float arithmetic +Arithmetic ---------------- :: - $ py '3 * 1.5' - 4.5 + $ py '24 * 60 ** 2' + 86400 :: -Exponentiation +Floating point -------------- :: - $ py '7**3' - 343 + $ py '1.0/98' + 0.010204081632 :: diff --git a/README.txt b/README.txt index 27aef0c..0e22ea5 100644 --- a/README.txt +++ b/README.txt @@ -1,13 +1,13 @@ # install sudo pip install pythonpy; alias py='pythonpy' -# float arithmetic -$ py '3 * 1.5' -4.5 +# arithmetic +$ py '24 * 60 ** 2' +86400 -# exponentiation -$ py '7**3' -343 +# floating point numbers +$ py '1.0/98' +0.010204081632 # number sequence $ py 'range(3)' diff --git a/bin/pythonpy b/bin/pythonpy index 4f03da5..5739a03 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -12,35 +12,39 @@ import sys parser = argparse.ArgumentParser() parser.add_argument('evaluation', nargs='?', default='None') -parser.add_argument('-c', '--cmd') -parser.add_argument('--ji' '--json_in', - dest='json_input', action='store_const', - const=True, default=False) -parser.add_argument('--jo' '--json_out', - dest='json_output', action='store_const', - const=True, default=False) -parser.add_argument('-x' '--line_by_line', - dest='line_by_line', action='store_const', +parser.add_argument('-x', dest='lines_of_stdin', action='store_const', + const=True, default=False, + help='treat each row as x') +parser.add_argument('-fx', dest='filter_result', action='store_const', + const=True, default=False, + help='keep rows satisfying condition(x)') +parser.add_argument('-l', dest='list_of_stdin', action='store_const', const=True, default=False, - help='sum the integers (default: find the max)') -parser.add_argument('-sv' '--split_values', - dest='split_values', - default=False, - help='sum the integers (default: find the max)') -parser.add_argument('-l', '--list_of_stdin', - dest='list_of_stdin', action='store_const', - const=True, default=False) -parser.add_argument('-fx', '--filter', - dest='filter_result', action='store_const', - const=True, default=False) + help='treat list of stdin as l') +parser.add_argument('-c', dest='cmd', help='run code before expression') parser.add_argument('--i', '--ignore_exceptions', dest='ignore_exceptions', action='store_const', - const=True, default=False) + const=True, default=False, + help='') +parser.add_argument('--si', '--split_input', dest='split_input', + help='pre-process each row with re.split(delimiter)') +parser.add_argument('--so', '--split_output', dest='split_output', + help='post-process each row with delimiter.join(row)') +parser.add_argument('--ji' '--json_input', + dest='json_input', action='store_const', + const=True, default=False, + help='pre-process each row with json.loads(row)') +parser.add_argument('--jo' '--json_output', + dest='json_output', action='store_const', + const=True, default=False, + help='post-process each row with json.dumps(row)') args = parser.parse_args() if args.json_input: stdin = (json.loads(x.rstrip()) for x in sys.stdin) +elif args.split_input: + stdin = (re.split(args.split_input, x.rstrip()) for x in sys.stdin) else: stdin = (x.rstrip() for x in sys.stdin) @@ -52,7 +56,7 @@ if args.cmd: if args.cmd: exec(args.cmd) -if args.line_by_line: +if args.lines_of_stdin: if args.ignore_exceptions: def safe_eval(text, x): try: @@ -67,21 +71,26 @@ elif args.list_of_stdin: result = eval(args.evaluation) elif args.filter_result: result = (x for x in stdin if eval(args.evaluation)) -elif args.split_values: - result = (eval(args.evaluation) for sv in - (re.split(args.split_values, x) for x in stdin)) else: result = eval(args.evaluation) +def format(output): + if output == None: + return None + elif args.json_output: + return json.dumps(output) + elif args.split_output: + return args.split_output.join(output) + else: + return output + + if hasattr(result, '__iter__'): for x in result: - if x is not None: - if args.json_output: - print json.dumps(x) - else: - print x -elif result is not None: - if args.json_output: - print json.dumps(result) - else: - print result + formatted = format(x) + if formatted: + print formatted +else: + formatted = format(result) + if formatted: + print formatted diff --git a/setup.py b/setup.py index 82f664a..182634a 100644 --- a/setup.py +++ b/setup.py @@ -3,9 +3,9 @@ setup( name='pythonpy', - version='0.1.5', + version='0.1.8', description='Command line utility for Python', scripts=[os.path.join('bin', 'pythonpy')], - license='Creative Commons Attribution-Noncommercial-Share Alike license', - long_description=open('README.rst').read(), + license='MIT', + long_description=open('README.txt').read(), )