From 16f16b9c6a06a39af02d282d46e12edbcfac9cf9 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 00:41:27 -0700 Subject: [PATCH 01/61] Changed license to MIT. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 82f664a..5fbedfb 100644 --- a/setup.py +++ b/setup.py @@ -6,6 +6,6 @@ version='0.1.5', description='Command line utility for Python', scripts=[os.path.join('bin', 'pythonpy')], - license='Creative Commons Attribution-Noncommercial-Share Alike license', + license='MIT', long_description=open('README.rst').read(), ) From 7ee3e62bd6d82476e3f76f596694d2a7059c3c97 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 11:30:24 -0700 Subject: [PATCH 02/61] 0.1.6 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 5fbedfb..2bd295f 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.1.5', + version='0.1.6', description='Command line utility for Python', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From 148b562b9107bd4a4ab801405dab62794e0c6033 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 11:37:39 -0700 Subject: [PATCH 03/61] Fixed readme issue for 0.1.7 --- MANIFEST | 2 +- setup.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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/setup.py b/setup.py index 2bd295f..2ecc3c4 100644 --- a/setup.py +++ b/setup.py @@ -3,9 +3,9 @@ setup( name='pythonpy', - version='0.1.6', + version='0.1.7', description='Command line utility for Python', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', - long_description=open('README.rst').read(), + #long_description=open('README.rst').read(), ) From 8c0f6d95a1cd20600f96bd05e3ba6787f1d05538 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 11:42:03 -0700 Subject: [PATCH 04/61] Added readme back to setup.py --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 2ecc3c4..0be56af 100644 --- a/setup.py +++ b/setup.py @@ -3,9 +3,9 @@ setup( name='pythonpy', - version='0.1.7', + version='0.1.8dev', description='Command line utility for Python', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', - #long_description=open('README.rst').read(), + long_description=open('README.txt').read(), ) From 031dc71ea697d3b10c666e1920ba324158e891d7 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 11:50:56 -0700 Subject: [PATCH 05/61] Added README.txt back to setup.py for 0.1.8 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 0be56af..182634a 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.1.8dev', + version='0.1.8', description='Command line utility for Python', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From eda9fc713b8a29d0e3d754a7885273be30907428 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 16:22:48 -0700 Subject: [PATCH 06/61] Changed initial example in README to be more interesting. --- README.txt | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/README.txt b/README.txt index 27aef0c..80d6a2f 100644 --- a/README.txt +++ b/README.txt @@ -1,13 +1,9 @@ # install sudo pip install pythonpy; alias py='pythonpy' -# float arithmetic -$ py '3 * 1.5' -4.5 - -# exponentiation -$ py '7**3' -343 +# arithmetic +$ py '24 * 60 ** 2' +86400 # number sequence $ py 'range(3)' From f94a5e33cfaf73934819704cd5833af1d8666982 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 16:25:02 -0700 Subject: [PATCH 07/61] Added floating point example back to readme. --- README.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.txt b/README.txt index 80d6a2f..0e22ea5 100644 --- a/README.txt +++ b/README.txt @@ -5,6 +5,10 @@ sudo pip install pythonpy; alias py='pythonpy' $ py '24 * 60 ** 2' 86400 +# floating point numbers +$ py '1.0/98' +0.010204081632 + # number sequence $ py 'range(3)' 0 From 2012ebdfb2c3ced75c09d528e2f50b97f36a3832 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 16:29:33 -0700 Subject: [PATCH 08/61] Updated README.rst to match README.txt --- README.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 :: From 9307b2f80b9c7383f65b5912904227a6f7f42e33 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 19:27:00 -0700 Subject: [PATCH 09/61] Changed -sv system to --si and --so system This will make it easier to understand --so and --si as they behave analogously to --jo and --ji --- bin/pythonpy | 57 ++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index 4f03da5..896572a 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -13,34 +13,34 @@ 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', 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) +parser.add_argument('-l', '--list_of_stdin', + dest='list_of_stdin', action='store_const', + const=True, default=False) parser.add_argument('--i', '--ignore_exceptions', dest='ignore_exceptions', action='store_const', const=True, default=False) +parser.add_argument('--si', '--split_input', dest='split_input') +parser.add_argument('--so', '--split_output', dest='split_output') +parser.add_argument('--ji' '--json_input', + dest='json_input', action='store_const', + const=True, default=False) +parser.add_argument('--jo' '--json_output', + dest='json_output', action='store_const', + const=True, default=False) 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) @@ -67,21 +67,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 From 185c0a2c6264e449086f0bc649c6ef941cbf213a Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 19:45:54 -0700 Subject: [PATCH 10/61] Improved the help descriptions --- bin/pythonpy | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index 896572a..5739a03 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -12,28 +12,32 @@ import sys parser = argparse.ArgumentParser() parser.add_argument('evaluation', nargs='?', default='None') -parser.add_argument('-c', '--cmd') -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='sum the integers (default: find the max)') -parser.add_argument('-fx', '--filter', - dest='filter_result', action='store_const', - const=True, default=False) -parser.add_argument('-l', '--list_of_stdin', - dest='list_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='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) -parser.add_argument('--si', '--split_input', dest='split_input') -parser.add_argument('--so', '--split_output', dest='split_output') + 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) + 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) + const=True, default=False, + help='post-process each row with json.dumps(row)') args = parser.parse_args() @@ -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: From b072a33b22de4c0b348c5550288de005c796cacb Mon Sep 17 00:00:00 2001 From: Low Kian Seong Date: Thu, 12 Jun 2014 15:48:27 +0800 Subject: [PATCH 11/61] Update setup.py I forgot to add this the other day for you, the url that links from pypi to github. --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 182634a..9aea9f0 100644 --- a/setup.py +++ b/setup.py @@ -8,4 +8,5 @@ scripts=[os.path.join('bin', 'pythonpy')], license='MIT', long_description=open('README.txt').read(), + url='https://github.com/Russell91/pythonpy', ) From 3c4a5f155ad5ad18a25aafee38dc8125e919477f Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 19:39:06 -0700 Subject: [PATCH 12/61] Changed back first to readme examples and changed rst formatting. --- README.rst | 33 +++++++++++++++++---------------- README.txt | 12 ++++++------ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/README.rst b/README.rst index 07e4751..9805214 100644 --- a/README.rst +++ b/README.rst @@ -15,28 +15,29 @@ For a permanent alias (For Bash users): :: -Arithmetic ----------------- +Float Arithmetic +~~~~~~~~~~~~~~~~ :: - $ py '24 * 60 ** 2' - 86400 + $ py '3 * 1.5' + 4.5 + :: -Floating point --------------- +Exponentiation +~~~~~~~~~~~~~~ :: - $ py '1.0/98' - 0.010204081632 + $ py '7**3' + 343 :: Number sequence ---------------- +~~~~~~~~~~~~~~~ :: @@ -48,7 +49,7 @@ Number sequence :: List comprehensions -------------------- +~~~~~~~~~~~~~~~~~~~ :: @@ -61,7 +62,7 @@ List comprehensions :: Math library usage ------------------- +~~~~~~~~~~~~~~~~~~ :: @@ -71,7 +72,7 @@ Math library usage :: Random library usage --------------------- +~~~~~~~~~~~~~~~~~~~~ :: @@ -81,7 +82,7 @@ Random library usage :: Multiply each line of input by 7. ---------------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: @@ -93,7 +94,7 @@ Multiply each line of input by 7. :: Append ".txt" to each line of input ------------------------------------ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :: @@ -121,7 +122,7 @@ Reverse a list :: Sum a list of numbers ---------------------- +~~~~~~~~~~~~~~~~~~~~~ :: @@ -131,7 +132,7 @@ Sum a list of numbers :: Count the lines of input ------------------------- +~~~~~~~~~~~~~~~~~~~~~~~~ :: diff --git a/README.txt b/README.txt index 0e22ea5..27aef0c 100644 --- a/README.txt +++ b/README.txt @@ -1,13 +1,13 @@ # install sudo pip install pythonpy; alias py='pythonpy' -# arithmetic -$ py '24 * 60 ** 2' -86400 +# float arithmetic +$ py '3 * 1.5' +4.5 -# floating point numbers -$ py '1.0/98' -0.010204081632 +# exponentiation +$ py '7**3' +343 # number sequence $ py 'range(3)' From e5f67714946c01b6afc3d1fd80bfe097ba08b639 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 20:49:09 -0700 Subject: [PATCH 13/61] Changed AttributeError to Error in README. --- README.rst | 2 +- README.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 9805214..a6b01bb 100644 --- a/README.rst +++ b/README.rst @@ -215,7 +215,7 @@ Get long palindromes :: -Ignore AttributeErrors if they pop up with (--i) +Ignore Errors if they pop up with (--i) ------------------------------------------------ Get the local network ip diff --git a/README.txt b/README.txt index 27aef0c..e98b613 100644 --- a/README.txt +++ b/README.txt @@ -98,7 +98,7 @@ kayak level ma'am -# ignore AttributeErrors if they pop up with (--i). +# ignore Errors if they pop up with (--i). # get the local network ip $ ifconfig | py -x --i 're.search(r"192\.168[\d\.]+", x).group()' 192.168.1.41 From 352785d7540ea8ddaf9f537341c59ce6bae08a54 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 20:51:24 -0700 Subject: [PATCH 14/61] Changed reverse a list example in readme. --- README.rst | 2 +- README.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index a6b01bb..b29d258 100644 --- a/README.rst +++ b/README.rst @@ -113,7 +113,7 @@ Reverse a list :: - $ py 'range(4)' | py -l 'sorted(l, reverse=True)' + $ py 'range(4)' | py -l 'l[::-1]' 3 2 1 diff --git a/README.txt b/README.txt index e98b613..7a4a606 100644 --- a/README.txt +++ b/README.txt @@ -44,7 +44,7 @@ $ py 'range(3)' | py -x 'x + ".txt"' # Sometimes you want to treat the input as a python list. # reverse a list -$ py 'range(4)' | py -l 'sorted(l, reverse=True)' +$ py 'range(4)' | py -l 'l[::-1]' 3 2 1 From 95dbdb1dd0034f6957c04484834eeebd60aa0d18 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 23:30:18 -0700 Subject: [PATCH 15/61] 0.1.9dev --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 9aea9f0..42650a0 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.1.8', + version='0.1.9dev', description='Command line utility for Python', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From 4dcc2e26394c13ce6efc8099e264a4d28e15fda6 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 23:41:57 -0700 Subject: [PATCH 16/61] Relasing 0.2 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 42650a0..5294fa6 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.1.9dev', + version='0.2.0', description='Command line utility for Python', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From 68a350bc348e57e6d65fab0a7daa4d1be86ef778 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Tue, 10 Jun 2014 23:45:34 -0700 Subject: [PATCH 17/61] Bugfix for not displaying values evaluating to False --- bin/pythonpy | 4 ++-- setup.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index 5739a03..357a95d 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -88,9 +88,9 @@ def format(output): if hasattr(result, '__iter__'): for x in result: formatted = format(x) - if formatted: + if formatted is not None: print formatted else: formatted = format(result) - if formatted: + if formatted is not None: print formatted diff --git a/setup.py b/setup.py index 5294fa6..28be197 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.2.0', + version='0.2.1', description='Command line utility for Python', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From 8832b72673add3d855006c37a2256169f5e25a30 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 22:44:13 -0700 Subject: [PATCH 18/61] Changed Error message in tutorial again. --- README.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.txt b/README.txt index 7a4a606..e7e09f3 100644 --- a/README.txt +++ b/README.txt @@ -98,7 +98,7 @@ kayak level ma'am -# ignore Errors if they pop up with (--i). +# keep going if row raises Error with (--i). # get the local network ip $ ifconfig | py -x --i 're.search(r"192\.168[\d\.]+", x).group()' 192.168.1.41 From 339a9d156927449983fd43f7eab88825ed00198c Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 22:48:57 -0700 Subject: [PATCH 19/61] 0.2.2 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 28be197..46ef54c 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.2.1', + version='0.2.2dev', description='Command line utility for Python', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From 360a0b655729d8e3a34cbf00c7b0afe5306d0e84 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 22:52:52 -0700 Subject: [PATCH 20/61] 0.2.2dev2 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 46ef54c..0f4acbd 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.2.2dev', + version='0.2.2dev2', description='Command line utility for Python', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From 8fff08222fe275c725c7841feba5e73e4927ae34 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 22:57:01 -0700 Subject: [PATCH 21/61] Removed long_description from pip in favor of url only. --- setup.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 0f4acbd..d621fa2 100644 --- a/setup.py +++ b/setup.py @@ -4,9 +4,8 @@ setup( name='pythonpy', version='0.2.2dev2', - description='Command line utility for Python', + description='Take advantage of your python skills from the command line', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', - long_description=open('README.txt').read(), url='https://github.com/Russell91/pythonpy', ) From a01778e8cc00f0b79c4ac2d001cdb1fbd2252e3b Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 22:57:20 -0700 Subject: [PATCH 22/61] 0.2.2dev3 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index d621fa2..b072c9d 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.2.2dev2', + version='0.2.2dev3', description='Take advantage of your python skills from the command line', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From 1c9e18af75aa090716afb17bc75601e21e8aad0f Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 22:59:03 -0700 Subject: [PATCH 23/61] Added blank long desciption to force pypi. --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index b072c9d..6539fe9 100644 --- a/setup.py +++ b/setup.py @@ -8,4 +8,5 @@ scripts=[os.path.join('bin', 'pythonpy')], license='MIT', url='https://github.com/Russell91/pythonpy', + long_description='', ) From 7c5f6ce0f627bad8f88c251c563012193f40a246 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 22:59:21 -0700 Subject: [PATCH 24/61] 0.2.2dev4 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 6539fe9..e7c4a88 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.2.2dev3', + version='0.2.2dev4', description='Take advantage of your python skills from the command line', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From 0c6d2dc402978549beec9bc130bd2e66589f04ba Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 23:10:57 -0700 Subject: [PATCH 25/61] Brought rst and txt readmes into equivalence. --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index b29d258..1402504 100644 --- a/README.rst +++ b/README.rst @@ -215,7 +215,7 @@ Get long palindromes :: -Ignore Errors if they pop up with (--i) +Keep going if some rows raise Errors with (--i). ------------------------------------------------ Get the local network ip From c24982983d4ef088b52fa07e59b8ff6fa2934ab9 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 23:33:49 -0700 Subject: [PATCH 26/61] Added safe_eval --i option to work for -fx in addition to -x mode. --- bin/pythonpy | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index 357a95d..bfc7671 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -56,21 +56,25 @@ if args.cmd: if args.cmd: exec(args.cmd) +def safe_eval(text, x): + try: + return eval(text) + except: + return None + if args.lines_of_stdin: if args.ignore_exceptions: - def safe_eval(text, x): - try: - return eval(text) - except: - return None result = (safe_eval(args.evaluation, x) for x in stdin) else: result = (eval(args.evaluation) for x in stdin) +elif args.filter_result: + if args.ignore_exceptions: + result = (x for x in stdin if safe_eval(args.evaluation, x)) + else: + result = (x for x in stdin if eval(args.evaluation)) elif args.list_of_stdin: l = list(stdin) result = eval(args.evaluation) -elif args.filter_result: - result = (x for x in stdin if eval(args.evaluation)) else: result = eval(args.evaluation) From 02b2c22a132c3d86756e22494e7ed53b4922b88b Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 23:34:25 -0700 Subject: [PATCH 27/61] Updated .gitignore to ignore .swp files. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5c1d549..1bcc247 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dist *.pyc +*swp From 6b7c9312f3e07d05b7a64c07ad417de5b24b7972 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Thu, 12 Jun 2014 23:47:00 -0700 Subject: [PATCH 28/61] Added some good __future__ statements to pythonpy. --- bin/pythonpy | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index bfc7671..aab0141 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -1,4 +1,6 @@ #!/usr/bin/env python +from __future__ import (unicode_literals, absolute_import, + print_function, division) import argparse import glob import itertools @@ -93,8 +95,8 @@ if hasattr(result, '__iter__'): for x in result: formatted = format(x) if formatted is not None: - print formatted + print(formatted) else: formatted = format(result) if formatted is not None: - print formatted + print(formatted) From fef752e0825f1587eb707548a95f506b62b5307b Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 00:28:40 -0700 Subject: [PATCH 29/61] Added a bunch more imports and a lazy import strategy for 25% perf boost. --- bin/pythonpy | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index aab0141..b468ce4 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -2,15 +2,30 @@ from __future__ import (unicode_literals, absolute_import, print_function, division) import argparse -import glob -import itertools +import sys import json -import math -import os -import random import re -import shutil -import sys + +def lazy_imports(evaluation, cmd): + query = (evaluation if evaluation else '' + + cmd if cmd else '') + if 'glob' in query: global glob; import glob + if 'itertools' in query: global itertools; import itertools + if 'json' in query: global json; import json + if 'math' in query: global math; import math + if 'os' in query: global os; import os + if 'random' in query: global random; import random + if 're' in query: global re; import re + if 'shutil' in query: global shutil; import shutil + if 'tempfile' in query: global tempfile; import tempfile + if 'datetime' in query: global datetime; import datetime + if 'hashlib' in query: global hashlib; import hashlib + if 'csv' in query: global csv; import csv + if 'base64' in query: global base64; import base64 + if 'calendar' in query: global calendar; import calendar + if 'Counter' in query: global Counter; from collections import Counter + if 'OrderedDict' in query: global OrderedDict; from collections import OrderedDict + if 'uuid4' in query: global uuid4; from uuid import uuid4 parser = argparse.ArgumentParser() parser.add_argument('evaluation', nargs='?', default='None') @@ -55,6 +70,8 @@ if args.evaluation: if args.cmd: args.cmd = args.cmd.replace("`", "'") +lazy_imports(args.evaluation, args.cmd) + if args.cmd: exec(args.cmd) From 7fcd56664d721c32fd6a1efedaea14200e596be6 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 00:29:02 -0700 Subject: [PATCH 30/61] Added a few more unit tests. --- test/test_pythonpy.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/test_pythonpy.py b/test/test_pythonpy.py index a9aa9d1..ad7043d 100644 --- a/test/test_pythonpy.py +++ b/test/test_pythonpy.py @@ -8,6 +8,17 @@ def test_empty(self): def test_numbers(self): self.assertEqual(check_output(['pythonpy', '3 * 4.5']),'13.5\n') + def test_range(self): + self.assertEqual(check_output(['pythonpy', 'range(3)']), '\n'.join(map(str, range(3))) + '\n') + + def test_range(self): + self.assertEqual(check_output(["""echo a,b | pythonpy -x 'x[1]' --si ,"""], shell=True), 'b\n') + + def test_ignore_errors(self): + self.assertEqual(check_output("""echo a | pythonpy -x --i 'None.None'""", shell=True), '') + self.assertEqual(check_output("""echo a | pythonpy -fx --i 'None.None'""", shell=True), '') + + if __name__ == '__main__': unittest.main() From 220e5ef05dbc703f02e94534119e161890368a2f Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 00:54:58 -0700 Subject: [PATCH 31/61] Changed implementation to use OOP :(. --- bin/pythonpy | 223 ++++++++++++++++++++++++++++----------------------- 1 file changed, 122 insertions(+), 101 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index b468ce4..b2595e5 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -6,114 +6,135 @@ import sys import json import re -def lazy_imports(evaluation, cmd): - query = (evaluation if evaluation else '' + - cmd if cmd else '') - if 'glob' in query: global glob; import glob - if 'itertools' in query: global itertools; import itertools - if 'json' in query: global json; import json - if 'math' in query: global math; import math - if 'os' in query: global os; import os - if 'random' in query: global random; import random - if 're' in query: global re; import re - if 'shutil' in query: global shutil; import shutil - if 'tempfile' in query: global tempfile; import tempfile - if 'datetime' in query: global datetime; import datetime - if 'hashlib' in query: global hashlib; import hashlib - if 'csv' in query: global csv; import csv - if 'base64' in query: global base64; import base64 - if 'calendar' in query: global calendar; import calendar - if 'Counter' in query: global Counter; from collections import Counter - if 'OrderedDict' in query: global OrderedDict; from collections import OrderedDict - if 'uuid4' in query: global uuid4; from uuid import uuid4 +class PythonPy(object): + def __init__(self): + self.parser = self.get_parser() -parser = argparse.ArgumentParser() -parser.add_argument('evaluation', nargs='?', default='None') -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='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, - 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)') + def run(self): + self.args = self.parser.parse_args() + stdin = self.preprocess() + self.lazy_imports() + if self.args.cmd: + exec(self.args.cmd) + results = self.process(stdin) + self.post_process(results) -args = parser.parse_args() + def get_parser(self): + parser = argparse.ArgumentParser() + parser.add_argument('evaluation', nargs='?', default='None') + 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='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, + 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)') + return parser -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) + def lazy_imports(self): + query = (self.args.evaluation if self.args.evaluation else '' + + self.args.cmd if self.args.cmd else '') + if 'glob' in query: global glob; import glob + if 'itertools' in query: global itertools; import itertools + if 'json' in query: global json; import json + if 'math' in query: global math; import math + if 'os' in query: global os; import os + if 'random' in query: global random; import random + if 're' in query: global re; import re + if 'shutil' in query: global shutil; import shutil + if 'tempfile' in query: global tempfile; import tempfile + if 'datetime' in query: global datetime; import datetime + if 'hashlib' in query: global hashlib; import hashlib + if 'csv' in query: global csv; import csv + if 'base64' in query: global base64; import base64 + if 'calendar' in query: global calendar; import calendar + if 'Counter' in query: global Counter; from collections import Counter + if 'OrderedDict' in query: global OrderedDict; from collections import OrderedDict + if 'uuid4' in query: global uuid4; from uuid import uuid4 -if args.evaluation: - args.evaluation = args.evaluation.replace("`", "'") -if args.cmd: - args.cmd = args.cmd.replace("`", "'") + def preprocess(self): + if self.args.json_input: + stdin = (json.loads(x.rstrip()) for x in sys.stdin) + elif self.args.split_input: + stdin = (re.split(self.args.split_input, x.rstrip()) for x in sys.stdin) + else: + stdin = (x.rstrip() for x in sys.stdin) -lazy_imports(args.evaluation, args.cmd) + if self.args.evaluation: + self.args.evaluation = self.args.evaluation.replace("`", "'") + if self.args.cmd: + self.args.cmd = self.args.cmd.replace("`", "'") + return stdin -if args.cmd: - exec(args.cmd) + def process(self, stdin): + def safe_eval(text, x): + try: + return eval(text) + except: + return None -def safe_eval(text, x): - try: - return eval(text) - except: - return None + if self.args.lines_of_stdin: + if self.args.ignore_exceptions: + result = (safe_eval(self.args.evaluation, x) for x in stdin) + else: + result = (eval(self.args.evaluation) for x in stdin) + elif self.args.filter_result: + if self.args.ignore_exceptions: + result = (x for x in stdin if safe_eval(self.args.evaluation, x)) + else: + result = (x for x in stdin if eval(self.args.evaluation)) + elif self.args.list_of_stdin: + l = list(stdin) + result = eval(self.args.evaluation) + else: + result = eval(self.args.evaluation) + return result -if args.lines_of_stdin: - if args.ignore_exceptions: - result = (safe_eval(args.evaluation, x) for x in stdin) - else: - result = (eval(args.evaluation) for x in stdin) -elif args.filter_result: - if args.ignore_exceptions: - result = (x for x in stdin if safe_eval(args.evaluation, x)) - else: - result = (x for x in stdin if eval(args.evaluation)) -elif args.list_of_stdin: - l = list(stdin) - result = eval(args.evaluation) -else: - result = eval(args.evaluation) + def post_process(self, results): + def format(output): + if output == None: + return None + elif self.args.json_output: + return json.dumps(output) + elif self.args.split_output: + return self.args.split_output.join(output) + else: + return output -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(results, '__iter__'): + for x in results: + formatted = format(x) + if formatted is not None: + print(formatted) + else: + formatted = format(results) + if formatted is not None: + print(formatted) -if hasattr(result, '__iter__'): - for x in result: - formatted = format(x) - if formatted is not None: - print(formatted) -else: - formatted = format(result) - if formatted is not None: - print(formatted) + +def main(): + pythonpy = PythonPy() + pythonpy.run() + + +if __name__ == '__main__': + main() From f0786f825b1ac144ddf7dd67fb7fcadc17c25679 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 00:56:43 -0700 Subject: [PATCH 32/61] Simplified main() function. --- bin/pythonpy | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index b2595e5..502f0ba 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -132,8 +132,7 @@ class PythonPy(object): def main(): - pythonpy = PythonPy() - pythonpy.run() + PythonPy().run() if __name__ == '__main__': From e4e16b17ba9098c2455ab0b20b12a03e4fdbe22e Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 01:01:51 -0700 Subject: [PATCH 33/61] 0.2.2dev5 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e7c4a88..7f73e93 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.2.2dev4', + version='0.2.2dev5', description='Take advantage of your python skills from the command line', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From fa9733726d65fdfdf40119b156c83aac55e0bff0 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 01:07:09 -0700 Subject: [PATCH 34/61] 0.2.2 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 7f73e93..0adc04b 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.2.2dev5', + version='0.2.2', description='Take advantage of your python skills from the command line', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From f68e6949c2fcb12571c8e90d72b540cbc912278b Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 01:51:54 -0700 Subject: [PATCH 35/61] Rearranged imports in alphabetical order --- bin/pythonpy | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index 502f0ba..5adf9a7 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -53,7 +53,12 @@ class PythonPy(object): def lazy_imports(self): query = (self.args.evaluation if self.args.evaluation else '' + self.args.cmd if self.args.cmd else '') + if 'base64' in query: global base64; import base64 + if 'calendar' in query: global calendar; import calendar + if 'csv' in query: global csv; import csv + if 'datetime' in query: global datetime; import datetime if 'glob' in query: global glob; import glob + if 'hashlib' in query: global hashlib; import hashlib if 'itertools' in query: global itertools; import itertools if 'json' in query: global json; import json if 'math' in query: global math; import math @@ -62,11 +67,6 @@ class PythonPy(object): if 're' in query: global re; import re if 'shutil' in query: global shutil; import shutil if 'tempfile' in query: global tempfile; import tempfile - if 'datetime' in query: global datetime; import datetime - if 'hashlib' in query: global hashlib; import hashlib - if 'csv' in query: global csv; import csv - if 'base64' in query: global base64; import base64 - if 'calendar' in query: global calendar; import calendar if 'Counter' in query: global Counter; from collections import Counter if 'OrderedDict' in query: global OrderedDict; from collections import OrderedDict if 'uuid4' in query: global uuid4; from uuid import uuid4 From 22bd488995026e4c79290f815bcb9f1afa579c86 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 01:52:06 -0700 Subject: [PATCH 36/61] Rearranged imports in alphabetical order --- bin/pythonpy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/pythonpy b/bin/pythonpy index 5adf9a7..abcbdf0 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -2,9 +2,9 @@ from __future__ import (unicode_literals, absolute_import, print_function, division) import argparse -import sys import json import re +import sys class PythonPy(object): def __init__(self): From a605a600c515466aa3e37b732486a531967cf7d4 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 01:54:13 -0700 Subject: [PATCH 37/61] Removed redundant re and json lazy import. --- bin/pythonpy | 2 -- 1 file changed, 2 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index abcbdf0..0fd2364 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -60,11 +60,9 @@ class PythonPy(object): if 'glob' in query: global glob; import glob if 'hashlib' in query: global hashlib; import hashlib if 'itertools' in query: global itertools; import itertools - if 'json' in query: global json; import json if 'math' in query: global math; import math if 'os' in query: global os; import os if 'random' in query: global random; import random - if 're' in query: global re; import re if 'shutil' in query: global shutil; import shutil if 'tempfile' in query: global tempfile; import tempfile if 'Counter' in query: global Counter; from collections import Counter From ea1adfa8423d019db7b69ea96145eb7ceab9da1a Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 02:01:47 -0700 Subject: [PATCH 38/61] Mentioned the wiki at the end of the README. --- README.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.rst b/README.rst index 1402504..a8ed179 100644 --- a/README.rst +++ b/README.rst @@ -227,3 +227,7 @@ Get the local network ip 192.168.1.41 :: + + +Check out the wiki at github.com/Russell91/pythonpy/wiki for more advanced features +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From f41ef15851f6a768aa0db12edbd4a83d4aa6b30d Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 02:02:42 -0700 Subject: [PATCH 39/61] Made wiki mention more optional. --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index a8ed179..a077e1c 100644 --- a/README.rst +++ b/README.rst @@ -229,5 +229,5 @@ Get the local network ip :: -Check out the wiki at github.com/Russell91/pythonpy/wiki for more advanced features +If you haven't had enough yet, check out the wiki at github.com/Russell91/pythonpy/wiki for more advanced features ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From e7a554233b6f5d5fb8fdf3a389c7d7467d93fe86 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 02:42:03 -0700 Subject: [PATCH 40/61] Fixed bug neutralizing -c argument. --- bin/pythonpy | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index 0fd2364..de468f8 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -12,11 +12,9 @@ class PythonPy(object): def run(self): self.args = self.parser.parse_args() - stdin = self.preprocess() + self.stdin = self.preprocess() self.lazy_imports() - if self.args.cmd: - exec(self.args.cmd) - results = self.process(stdin) + results = self.process() self.post_process(results) def get_parser(self): @@ -70,6 +68,7 @@ class PythonPy(object): if 'uuid4' in query: global uuid4; from uuid import uuid4 def preprocess(self): + if self.args.json_input: stdin = (json.loads(x.rstrip()) for x in sys.stdin) elif self.args.split_input: @@ -83,25 +82,28 @@ class PythonPy(object): self.args.cmd = self.args.cmd.replace("`", "'") return stdin - def process(self, stdin): - def safe_eval(text, x): - try: - return eval(text) - except: - return None + def safe_eval(self, text, x): + try: + return eval(text) + except: + return None + + def process(self): + if self.args.cmd: + exec self.args.cmd in globals(), locals() if self.args.lines_of_stdin: if self.args.ignore_exceptions: - result = (safe_eval(self.args.evaluation, x) for x in stdin) + result = (self.safe_eval(self.args.evaluation, x) for x in self.stdin) else: - result = (eval(self.args.evaluation) for x in stdin) + result = (eval(self.args.evaluation) for x in self.stdin) elif self.args.filter_result: if self.args.ignore_exceptions: - result = (x for x in stdin if safe_eval(self.args.evaluation, x)) + result = (x for x in self.stdin if self.safe_eval(self.args.evaluation, x)) else: - result = (x for x in stdin if eval(self.args.evaluation)) + result = (x for x in self.stdin if eval(self.args.evaluation)) elif self.args.list_of_stdin: - l = list(stdin) + l = list(self.stdin) result = eval(self.args.evaluation) else: result = eval(self.args.evaluation) From 945e7d3c04e35da2febf627349d63bc685a72b99 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 02:42:23 -0700 Subject: [PATCH 41/61] 0.2.3dev --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 0adc04b..3f6475b 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.2.2', + version='0.2.3dev', description='Take advantage of your python skills from the command line', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From 4628b8878e669076d9e2d127c0eb151c34e60f40 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 11:42:35 -0700 Subject: [PATCH 42/61] Changed help description for --si. --- bin/pythonpy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/pythonpy b/bin/pythonpy index de468f8..42b07f6 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -35,7 +35,7 @@ class PythonPy(object): const=True, default=False, help='') parser.add_argument('--si', '--split_input', dest='split_input', - help='pre-process each row with re.split(delimiter)') + help='pre-process each row with re.split(delimiter, row)') parser.add_argument('--so', '--split_output', dest='split_output', help='post-process each row with delimiter.join(row)') parser.add_argument('--ji' '--json_input', From fc6f6f276d968c55899e651e96ab36bdf30dda5d Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 15:53:15 -0700 Subject: [PATCH 43/61] Got rid of OOP as it was messing up exec function. --- bin/pythonpy | 246 +++++++++++++++++++++++---------------------------- setup.py | 2 +- 2 files changed, 114 insertions(+), 134 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index 42b07f6..a1826b1 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -1,139 +1,119 @@ -#!/usr/bin/env python +#!/usr/bin/python from __future__ import (unicode_literals, absolute_import, print_function, division) import argparse +import sys import json import re -import sys - -class PythonPy(object): - def __init__(self): - self.parser = self.get_parser() - - def run(self): - self.args = self.parser.parse_args() - self.stdin = self.preprocess() - self.lazy_imports() - results = self.process() - self.post_process(results) - - def get_parser(self): - parser = argparse.ArgumentParser() - parser.add_argument('evaluation', nargs='?', default='None') - 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='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, - help='') - parser.add_argument('--si', '--split_input', dest='split_input', - help='pre-process each row with re.split(delimiter, row)') - 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)') - return parser - - def lazy_imports(self): - query = (self.args.evaluation if self.args.evaluation else '' + - self.args.cmd if self.args.cmd else '') - if 'base64' in query: global base64; import base64 - if 'calendar' in query: global calendar; import calendar - if 'csv' in query: global csv; import csv - if 'datetime' in query: global datetime; import datetime - if 'glob' in query: global glob; import glob - if 'hashlib' in query: global hashlib; import hashlib - if 'itertools' in query: global itertools; import itertools - if 'math' in query: global math; import math - if 'os' in query: global os; import os - if 'random' in query: global random; import random - if 'shutil' in query: global shutil; import shutil - if 'tempfile' in query: global tempfile; import tempfile - if 'Counter' in query: global Counter; from collections import Counter - if 'OrderedDict' in query: global OrderedDict; from collections import OrderedDict - if 'uuid4' in query: global uuid4; from uuid import uuid4 - - def preprocess(self): - - if self.args.json_input: - stdin = (json.loads(x.rstrip()) for x in sys.stdin) - elif self.args.split_input: - stdin = (re.split(self.args.split_input, x.rstrip()) for x in sys.stdin) - else: - stdin = (x.rstrip() for x in sys.stdin) - - if self.args.evaluation: - self.args.evaluation = self.args.evaluation.replace("`", "'") - if self.args.cmd: - self.args.cmd = self.args.cmd.replace("`", "'") - return stdin - - def safe_eval(self, text, x): - try: - return eval(text) - except: - return None - - def process(self): - if self.args.cmd: - exec self.args.cmd in globals(), locals() - - if self.args.lines_of_stdin: - if self.args.ignore_exceptions: - result = (self.safe_eval(self.args.evaluation, x) for x in self.stdin) - else: - result = (eval(self.args.evaluation) for x in self.stdin) - elif self.args.filter_result: - if self.args.ignore_exceptions: - result = (x for x in self.stdin if self.safe_eval(self.args.evaluation, x)) - else: - result = (x for x in self.stdin if eval(self.args.evaluation)) - elif self.args.list_of_stdin: - l = list(self.stdin) - result = eval(self.args.evaluation) - else: - result = eval(self.args.evaluation) - return result - - def post_process(self, results): - def format(output): - if output == None: - return None - elif self.args.json_output: - return json.dumps(output) - elif self.args.split_output: - return self.args.split_output.join(output) - else: - return output - - if hasattr(results, '__iter__'): - for x in results: - formatted = format(x) - if formatted is not None: - print(formatted) - else: - formatted = format(results) - if formatted is not None: - print(formatted) - - -def main(): - PythonPy().run() - -if __name__ == '__main__': - main() +def lazy_imports(evaluation, cmd): + query = (evaluation if evaluation else '' + + cmd if cmd else '') + if 'glob' in query: global glob; import glob + if 'itertools' in query: global itertools; import itertools + if 'json' in query: global json; import json + if 'math' in query: global math; import math + if 'os' in query: global os; import os + if 'random' in query: global random; import random + if 're' in query: global re; import re + if 'shutil' in query: global shutil; import shutil + if 'tempfile' in query: global tempfile; import tempfile + if 'datetime' in query: global datetime; import datetime + if 'hashlib' in query: global hashlib; import hashlib + if 'csv' in query: global csv; import csv + if 'base64' in query: global base64; import base64 + if 'calendar' in query: global calendar; import calendar + if 'Counter' in query: global Counter; from collections import Counter + if 'OrderedDict' in query: global OrderedDict; from collections import OrderedDict + if 'uuid4' in query: global uuid4; from uuid import uuid4 + +parser = argparse.ArgumentParser() +parser.add_argument('evaluation', nargs='?', default='None') +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='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, + 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) + +if args.evaluation: + args.evaluation = args.evaluation.replace("`", "'") +if args.cmd: + args.cmd = args.cmd.replace("`", "'") + +lazy_imports(args.evaluation, args.cmd) + +if args.cmd: + exec(args.cmd) + +def safe_eval(text, x): + try: + return eval(text) + except: + return None + +if args.lines_of_stdin: + if args.ignore_exceptions: + result = (safe_eval(args.evaluation, x) for x in stdin) + else: + result = (eval(args.evaluation) for x in stdin) +elif args.filter_result: + if args.ignore_exceptions: + result = (x for x in stdin if safe_eval(args.evaluation, x)) + else: + result = (x for x in stdin if eval(args.evaluation)) +elif args.list_of_stdin: + l = list(stdin) + result = eval(args.evaluation) +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: + formatted = format(x) + if formatted is not None: + print(formatted) +else: + formatted = format(result) + if formatted is not None: + print(formatted) diff --git a/setup.py b/setup.py index 3f6475b..408921d 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.2.3dev', + version='0.2.4dev', description='Take advantage of your python skills from the command line', scripts=[os.path.join('bin', 'pythonpy')], license='MIT', From e74a2fc728f9379ef3ba7fadf9e5e4356470ae5d Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 15:58:00 -0700 Subject: [PATCH 44/61] Rearranged imports and added from itertools import groupby. --- bin/pythonpy | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index a1826b1..c23f9f4 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -9,6 +9,11 @@ import re def lazy_imports(evaluation, cmd): query = (evaluation if evaluation else '' + cmd if cmd else '') + if 'base64' in query: global base64; import base64 + if 'calendar' in query: global calendar; import calendar + if 'csv' in query: global csv; import csv + if 'datetime' in query: global datetime; import datetime + if 'hashlib' in query: global hashlib; import hashlib if 'glob' in query: global glob; import glob if 'itertools' in query: global itertools; import itertools if 'json' in query: global json; import json @@ -18,13 +23,9 @@ def lazy_imports(evaluation, cmd): if 're' in query: global re; import re if 'shutil' in query: global shutil; import shutil if 'tempfile' in query: global tempfile; import tempfile - if 'datetime' in query: global datetime; import datetime - if 'hashlib' in query: global hashlib; import hashlib - if 'csv' in query: global csv; import csv - if 'base64' in query: global base64; import base64 - if 'calendar' in query: global calendar; import calendar if 'Counter' in query: global Counter; from collections import Counter if 'OrderedDict' in query: global OrderedDict; from collections import OrderedDict + if 'groupby' in query: global OrderedDict; from collections import OrderedDict if 'uuid4' in query: global uuid4; from uuid import uuid4 parser = argparse.ArgumentParser() From 7bd2f6403ff55832cf9fb85494f2b2d7bca1634c Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 16:01:28 -0700 Subject: [PATCH 45/61] Added -C option to run statement after expression. --- bin/pythonpy | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index c23f9f4..7cb3000 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -6,9 +6,10 @@ import sys import json import re -def lazy_imports(evaluation, cmd): +def lazy_imports(evaluation, pre_cmd, post_cmd): query = (evaluation if evaluation else '' + - cmd if cmd else '') + pre_cmd if pre_cmd else '' + + post_cmd if post_cmd else '') if 'base64' in query: global base64; import base64 if 'calendar' in query: global calendar; import calendar if 'csv' in query: global csv; import csv @@ -39,7 +40,8 @@ parser.add_argument('-fx', dest='filter_result', action='store_const', parser.add_argument('-l', dest='list_of_stdin', 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('-c', dest='pre_cmd', help='run code before expression') +parser.add_argument('-C', dest='post_cmd', help='run code after expression') parser.add_argument('--i', '--ignore_exceptions', dest='ignore_exceptions', action='store_const', const=True, default=False, @@ -68,13 +70,13 @@ else: if args.evaluation: args.evaluation = args.evaluation.replace("`", "'") -if args.cmd: - args.cmd = args.cmd.replace("`", "'") +if args.pre_cmd: + args.pre_cmd = args.pre_cmd.replace("`", "'") -lazy_imports(args.evaluation, args.cmd) +lazy_imports(args.evaluation, args.pre_cmd, args.post_cmd) -if args.cmd: - exec(args.cmd) +if args.pre_cmd: + exec(args.pre_cmd) def safe_eval(text, x): try: @@ -118,3 +120,6 @@ else: formatted = format(result) if formatted is not None: print(formatted) + +if args.post_cmd: + exec(args.post_cmd) From e9d683f4136f46950da939e467cc65f9ece84969 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 16:02:47 -0700 Subject: [PATCH 46/61] Removed README.txt, as it has been superceded by README.rst. --- README.txt | 104 ----------------------------------------------------- 1 file changed, 104 deletions(-) delete mode 100644 README.txt diff --git a/README.txt b/README.txt deleted file mode 100644 index e7e09f3..0000000 --- a/README.txt +++ /dev/null @@ -1,104 +0,0 @@ -# install -sudo pip install pythonpy; alias py='pythonpy' - -# float arithmetic -$ py '3 * 1.5' -4.5 - -# exponentiation -$ py '7**3' -343 - -# number sequence -$ py 'range(3)' -0 -1 -2 - -# list comprehensions -$ py '[x**2 for x in range(1,5)]' -1 -4 -9 -16 - -# math library usage -$ py 'math.exp(1)' -2.71828182846 - -# random library usage -$ py 'random.random()' -0.103173957713 - -# multiply each line of input by 7. -$ py 'range(3)' | py -x 'int(x)*7' -0 -7 -14 - -# append ".txt" to each line of input -$ py 'range(3)' | py -x 'x + ".txt"' -0.txt -1.txt -2.txt - -# Sometimes you want to treat the input as a python list. -# reverse a list -$ py 'range(4)' | py -l 'l[::-1]' -3 -2 -1 -0 - -# sum a list of numbers -$ py 'range(4)' | py -l 'sum(int(x) for x in l)' -6 - -# count the lines of input -$ py 'range(17)' | py -l 'len(l)' -17 - -# Other times you just want to filter out lines from the input. -# get only even numbers -$ py 'range(8)' | py -x 'x if int(x)%2 == 0 else None' -0 -2 -4 -6 - -# The shorthand -fx (filter on x) is also available. -# get only odd numbers -$ py 'range(8)' | py -fx 'int(x)%2 == 1' -1 -3 -5 -7 - -# get words starting with "and" -$ cat /usr/share/dict/words | py -fx 're.match(r"and", x)' | head -5 -and -andante -andante's -andantes -andiron - -#get verbs starting with ba -$ cat /usr/share/dict/words | py -fx 're.match(r"ba.*ing$", x)' | head -5 -baaing -babbling -babying -babysitting -backbiting - -# get long palindromes -$ cat /usr/share/dict/words | py -fx 'x==x[::-1] and len(x) >= 5' | head -5 -civic -deified -kayak -level -ma'am - -# keep going if row raises Error with (--i). -# get the local network ip -$ ifconfig | py -x --i 're.search(r"192\.168[\d\.]+", x).group()' -192.168.1.41 From fc6b53f17a0cb31017f1cfd603ada50034e79555 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 16:07:17 -0700 Subject: [PATCH 47/61] Fixed groupby import typo. --- bin/pythonpy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/pythonpy b/bin/pythonpy index 7cb3000..67facf3 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -26,7 +26,7 @@ def lazy_imports(evaluation, pre_cmd, post_cmd): if 'tempfile' in query: global tempfile; import tempfile if 'Counter' in query: global Counter; from collections import Counter if 'OrderedDict' in query: global OrderedDict; from collections import OrderedDict - if 'groupby' in query: global OrderedDict; from collections import OrderedDict + if 'groupby' in query: global groupby; from itertools import groupby if 'uuid4' in query: global uuid4; from uuid import uuid4 parser = argparse.ArgumentParser() From 93e8911272d25af47c91b035d87af84d5e2a8e2b Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 16:21:00 -0700 Subject: [PATCH 48/61] Fixed bug failing to import for -c/-C statements. --- bin/pythonpy | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/pythonpy b/bin/pythonpy index 67facf3..86430d3 100755 --- a/bin/pythonpy +++ b/bin/pythonpy @@ -7,9 +7,9 @@ import json import re def lazy_imports(evaluation, pre_cmd, post_cmd): - query = (evaluation if evaluation else '' + - pre_cmd if pre_cmd else '' + - post_cmd if post_cmd else '') + query = ((evaluation if evaluation else '') + + (pre_cmd if pre_cmd else '') + + (post_cmd if post_cmd else '')) if 'base64' in query: global base64; import base64 if 'calendar' in query: global calendar; import calendar if 'csv' in query: global csv; import csv From bfd99a2590e0f3676bb51d3f988d75d27a869a99 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 16:21:14 -0700 Subject: [PATCH 49/61] Added a few more unit tests!. --- test/test_pythonpy.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/test/test_pythonpy.py b/test/test_pythonpy.py index ad7043d..a1768e7 100644 --- a/test/test_pythonpy.py +++ b/test/test_pythonpy.py @@ -11,13 +11,40 @@ def test_numbers(self): def test_range(self): self.assertEqual(check_output(['pythonpy', 'range(3)']), '\n'.join(map(str, range(3))) + '\n') - def test_range(self): + def test_split_input(self): self.assertEqual(check_output(["""echo a,b | pythonpy -x 'x[1]' --si ,"""], shell=True), 'b\n') + def test_split_output(self): + self.assertEqual(check_output(["""echo abc | pythonpy -x x --si '' --so ','"""], shell=True), 'a,b,c\n') + def test_ignore_errors(self): self.assertEqual(check_output("""echo a | pythonpy -x --i 'None.None'""", shell=True), '') self.assertEqual(check_output("""echo a | pythonpy -fx --i 'None.None'""", shell=True), '') + def test_statements(self): + self.assertEqual(check_output("""pythonpy -c 'a=5' -C 'print(a)'""", shell=True), '5\n') + self.assertEqual(check_output("""echo 3 | pythonpy -c 'a=5' -x x -C 'print(a)'""", shell=True), '3\n5\n') + + def test_imports(self): + check_output("pythonpy 'math'", shell=True) + check_output("pythonpy 'base64'", shell=True) + check_output("pythonpy 'calendar'", shell=True) + check_output("pythonpy 'csv'", shell=True) + check_output("pythonpy 'datetime'", shell=True) + check_output("pythonpy 'hashlib'", shell=True) + check_output("pythonpy 'glob'", shell=True) + check_output("pythonpy 'itertools'", shell=True) + check_output("pythonpy 'json'", shell=True) + check_output("pythonpy 'math'", shell=True) + check_output("pythonpy 'os'", shell=True) + check_output("pythonpy 'random'", shell=True) + check_output("pythonpy 're'", shell=True) + check_output("pythonpy 'shutil'", shell=True) + check_output("pythonpy 'tempfile'", shell=True) + check_output("pythonpy -c 'Counter'", shell=True) + check_output("pythonpy -c 'OrderedDict'", shell=True) + check_output("pythonpy -c 'groupby'", shell=True) + check_output("pythonpy 'uuid4'", shell=True) if __name__ == '__main__': From 724a6a16e48d22dd7d50d337118ffbd6f6222eac Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 16:39:17 -0700 Subject: [PATCH 50/61] Moved pythonpy out of bin into primary directory. --- bin/pythonpy => pythonpy | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename bin/pythonpy => pythonpy (99%) diff --git a/bin/pythonpy b/pythonpy similarity index 99% rename from bin/pythonpy rename to pythonpy index 86430d3..55b801b 100755 --- a/bin/pythonpy +++ b/pythonpy @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python from __future__ import (unicode_literals, absolute_import, print_function, division) import argparse diff --git a/setup.py b/setup.py index 408921d..3bc5b39 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ name='pythonpy', version='0.2.4dev', description='Take advantage of your python skills from the command line', - scripts=[os.path.join('bin', 'pythonpy')], + scripts=['pythonpy'], license='MIT', url='https://github.com/Russell91/pythonpy', long_description='', From 9cc9f85c1bf51f3016cd486afff9f8e3251bd68d Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 16:58:48 -0700 Subject: [PATCH 51/61] Changed "evaluation" to "expression" in source code. --- pythonpy | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/pythonpy b/pythonpy index 55b801b..0968d53 100755 --- a/pythonpy +++ b/pythonpy @@ -6,8 +6,8 @@ import sys import json import re -def lazy_imports(evaluation, pre_cmd, post_cmd): - query = ((evaluation if evaluation else '') + +def lazy_imports(expression, pre_cmd, post_cmd): + query = ((expression if expression else '') + (pre_cmd if pre_cmd else '') + (post_cmd if post_cmd else '')) if 'base64' in query: global base64; import base64 @@ -29,8 +29,10 @@ def lazy_imports(evaluation, pre_cmd, post_cmd): if 'groupby' in query: global groupby; from itertools import groupby if 'uuid4' in query: global uuid4; from uuid import uuid4 -parser = argparse.ArgumentParser() -parser.add_argument('evaluation', nargs='?', default='None') +parser = argparse.ArgumentParser( + formatter_class=argparse.RawDescriptionHelpFormatter) + +parser.add_argument('expression', nargs='?', default='None') parser.add_argument('-x', dest='lines_of_stdin', action='store_const', const=True, default=False, help='treat each row as x') @@ -68,12 +70,12 @@ elif args.split_input: else: stdin = (x.rstrip() for x in sys.stdin) -if args.evaluation: - args.evaluation = args.evaluation.replace("`", "'") +if args.expression: + args.expression = args.expression.replace("`", "'") if args.pre_cmd: args.pre_cmd = args.pre_cmd.replace("`", "'") -lazy_imports(args.evaluation, args.pre_cmd, args.post_cmd) +lazy_imports(args.expression, args.pre_cmd, args.post_cmd) if args.pre_cmd: exec(args.pre_cmd) @@ -86,19 +88,19 @@ def safe_eval(text, x): if args.lines_of_stdin: if args.ignore_exceptions: - result = (safe_eval(args.evaluation, x) for x in stdin) + result = (safe_eval(args.expression, x) for x in stdin) else: - result = (eval(args.evaluation) for x in stdin) + result = (eval(args.expression) for x in stdin) elif args.filter_result: if args.ignore_exceptions: - result = (x for x in stdin if safe_eval(args.evaluation, x)) + result = (x for x in stdin if safe_eval(args.expression, x)) else: - result = (x for x in stdin if eval(args.evaluation)) + result = (x for x in stdin if eval(args.expression)) elif args.list_of_stdin: l = list(stdin) - result = eval(args.evaluation) + result = eval(args.expression) else: - result = eval(args.evaluation) + result = eval(args.expression) def format(output): if output == None: From e8e80e200ed6e3e6868aed7a9863f914ac26f6de Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 16:59:58 -0700 Subject: [PATCH 52/61] Changed help for --si. --- pythonpy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonpy b/pythonpy index 0968d53..bbd25c9 100755 --- a/pythonpy +++ b/pythonpy @@ -49,7 +49,7 @@ parser.add_argument('--i', '--ignore_exceptions', const=True, default=False, help='') parser.add_argument('--si', '--split_input', dest='split_input', - help='pre-process each row with re.split(delimiter)') + help='pre-process each row with re.split(delimiter, row)') parser.add_argument('--so', '--split_output', dest='split_output', help='post-process each row with delimiter.join(row)') parser.add_argument('--ji' '--json_input', From 5483b405524d7ad49508a87f1682e6ad6f9b3f86 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 17:01:52 -0700 Subject: [PATCH 53/61] 0.2.4dev2 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 3bc5b39..b36866f 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.2.4dev', + version='0.2.4dev2', description='Take advantage of your python skills from the command line', scripts=['pythonpy'], license='MIT', From 27d5adb13d374a072614abc230241006434495af Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 17:35:35 -0700 Subject: [PATCH 54/61] Updated README.rst. --- README.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index a077e1c..b7d71e2 100644 --- a/README.rst +++ b/README.rst @@ -229,5 +229,5 @@ Get the local network ip :: -If you haven't had enough yet, check out the wiki at github.com/Russell91/pythonpy/wiki for more advanced features -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +If you haven't had enough yet, check out the wiki at github.com/Russell91/pythonpy/wiki +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From b2089bcd759ca641cc109dca4b6afc0df863ab0f Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 18:37:09 -0700 Subject: [PATCH 55/61] 0.2.4 --- MANIFEST | 3 +-- setup.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/MANIFEST b/MANIFEST index c217127..acd664e 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,5 +1,4 @@ # file GENERATED by distutils, do NOT edit -README.txt +pythonpy setup.py -bin/pythonpy test/test_pythonpy.py diff --git a/setup.py b/setup.py index b36866f..3b1bd9e 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='pythonpy', - version='0.2.4dev2', + version='0.2.4', description='Take advantage of your python skills from the command line', scripts=['pythonpy'], license='MIT', From 5315dbaee5b7df0ccc762309a8654393402745d7 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 20:20:40 -0700 Subject: [PATCH 56/61] Stopped tracking auto-generated MANIFEST with git. --- .gitignore | 1 + MANIFEST | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 MANIFEST diff --git a/.gitignore b/.gitignore index 1bcc247..35379c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dist *.pyc *swp +MANIFEST diff --git a/MANIFEST b/MANIFEST deleted file mode 100644 index acd664e..0000000 --- a/MANIFEST +++ /dev/null @@ -1,4 +0,0 @@ -# file GENERATED by distutils, do NOT edit -pythonpy -setup.py -test/test_pythonpy.py From f870ef1b89ee7ec4eb055fa94766535bf0ec7639 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 20:25:10 -0700 Subject: [PATCH 57/61] Changed wiki to link. --- README.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index b7d71e2..024b11f 100644 --- a/README.rst +++ b/README.rst @@ -229,5 +229,6 @@ Get the local network ip :: -If you haven't had enough yet, check out the wiki at github.com/Russell91/pythonpy/wiki +If you haven't had enough yet, check out the `wiki `__ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + From d9e60f684a9607ccfcf422c01d23a540e0f9b563 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 20:26:52 -0700 Subject: [PATCH 58/61] Changed wiki link again --- README.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 024b11f..c2ec0ac 100644 --- a/README.rst +++ b/README.rst @@ -229,6 +229,5 @@ Get the local network ip :: -If you haven't had enough yet, check out the `wiki `__ -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - +If you haven't had enough yet, check out the `wiki `__ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 445b193fccbe99e23db1b81dbcbb5e1631f671b0 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 20:39:54 -0700 Subject: [PATCH 59/61] Added letter count example to README. --- README.rst | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/README.rst b/README.rst index c2ec0ac..06ad488 100644 --- a/README.rst +++ b/README.rst @@ -215,6 +215,42 @@ Get long palindromes :: +Count words beginning with each letter +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +:: + + $ cat /usr/share/dict/words | py -x 'x[0].lower()' | py -l 'Counter(l).items()' + ('\xc3', 17) + ('a', 5895) + ('c', 9521) + ('b', 6068) + ('e', 3851) + ('d', 5823) + ('g', 3456) + ('f', 4127) + ('i', 3667) + ('h', 3804) + ('k', 1163) + ('j', 1262) + ('m', 5922) + ('l', 3400) + ('o', 2270) + ('n', 2018) + ('q', 467) + ('p', 7659) + ('s', 11327) + ('r', 5356) + ('u', 1943) + ('t', 5085) + ('w', 2739) + ('v', 1576) + ('y', 421) + ('x', 53) + ('z', 281) + +:: + Keep going if some rows raise Errors with (--i). ------------------------------------------------ From 60b084ccf4506e1a95afa975bf77a6cc9af512b8 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 20:41:32 -0700 Subject: [PATCH 60/61] Shortened letter count example in README. --- README.rst | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/README.rst b/README.rst index 06ad488..c662756 100644 --- a/README.rst +++ b/README.rst @@ -220,34 +220,12 @@ Count words beginning with each letter :: - $ cat /usr/share/dict/words | py -x 'x[0].lower()' | py -l 'Counter(l).items()' - ('\xc3', 17) - ('a', 5895) + $ cat /usr/share/dict/words | py -x 'x[0].lower()' | py -l 'Counter(l).most_common(5)' + ('s', 11327) ('c', 9521) + ('p', 7659) ('b', 6068) - ('e', 3851) - ('d', 5823) - ('g', 3456) - ('f', 4127) - ('i', 3667) - ('h', 3804) - ('k', 1163) - ('j', 1262) ('m', 5922) - ('l', 3400) - ('o', 2270) - ('n', 2018) - ('q', 467) - ('p', 7659) - ('s', 11327) - ('r', 5356) - ('u', 1943) - ('t', 5085) - ('w', 2739) - ('v', 1576) - ('y', 421) - ('x', 53) - ('z', 281) :: From 77f425371851afad731009fc50dae9b847d46899 Mon Sep 17 00:00:00 2001 From: Russell Stewart Date: Fri, 13 Jun 2014 20:48:20 -0700 Subject: [PATCH 61/61] Fixed bug not replacing tics inside post_cmd. --- pythonpy | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pythonpy b/pythonpy index bbd25c9..6923642 100755 --- a/pythonpy +++ b/pythonpy @@ -74,6 +74,8 @@ if args.expression: args.expression = args.expression.replace("`", "'") if args.pre_cmd: args.pre_cmd = args.pre_cmd.replace("`", "'") +if args.post_cmd: + args.post_cmd = args.post_cmd.replace("`", "'") lazy_imports(args.expression, args.pre_cmd, args.post_cmd)