From 5639cb35d4eb25574442bda8ab5e0ff778296b60 Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Sat, 15 Jul 2017 14:48:10 -0400 Subject: [PATCH 1/4] json.tool: improve code readability --- Lib/json/tool.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 4f3182c0c1e7f1..2f78dc5f3b9c04 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -25,22 +25,22 @@ def main(): help='a JSON file to be validated or pretty-printed') parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), help='write the output of infile to outfile') - parser.add_argument('--sort-keys', action='store_true', default=False, - help='sort the output of dictionaries alphabetically by key') + parser.add_argument('--sort-keys', action='store_true', + help='sort dictionary outputs alphabetically by key') options = parser.parse_args() + # Read input JSON infile = options.infile or sys.stdin - outfile = options.outfile or sys.stdout sort_keys = options.sort_keys + hook = collections.OrderedDict if sort_keys else None with infile: try: - if sort_keys: - obj = json.load(infile) - else: - obj = json.load(infile, - object_pairs_hook=collections.OrderedDict) + obj = json.load(infile, object_pairs_hook=hook) except ValueError as e: raise SystemExit(e) + + # Output JSON + outfile = options.outfile or sys.stdout with outfile: json.dump(obj, outfile, sort_keys=sort_keys, indent=4) outfile.write('\n') From ff3b0299fe87c64ae50d9f8d42bd795041c666a8 Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Mon, 17 Jul 2017 15:32:09 -0400 Subject: [PATCH 2/4] Address https://github.com/python/cpython/pull/2720#pullrequestreview-50224377 --- Lib/json/tool.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 2f78dc5f3b9c04..0a3d8ef36c5bb5 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -22,27 +22,26 @@ def main(): 'to validate and pretty-print JSON objects.') parser = argparse.ArgumentParser(prog=prog, description=description) parser.add_argument('infile', nargs='?', type=argparse.FileType(), + default=sys.stdin, help='a JSON file to be validated or pretty-printed') parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), + default=sys.stdout, help='write the output of infile to outfile') parser.add_argument('--sort-keys', action='store_true', - help='sort dictionary outputs alphabetically by key') + help='sort dictionaries output alphabetically by key') options = parser.parse_args() # Read input JSON - infile = options.infile or sys.stdin - sort_keys = options.sort_keys - hook = collections.OrderedDict if sort_keys else None - with infile: + hook = collections.OrderedDict if options.sort_keys else None + with options.infile as infile: try: obj = json.load(infile, object_pairs_hook=hook) except ValueError as e: raise SystemExit(e) # Output JSON - outfile = options.outfile or sys.stdout - with outfile: - json.dump(obj, outfile, sort_keys=sort_keys, indent=4) + with options.outfile as outfile: + json.dump(obj, outfile, sort_keys=options.sort_keys, indent=4) outfile.write('\n') From 61cbd236f1f72aae43de5878a9feef7d823a1616 Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Tue, 18 Jul 2017 11:45:13 -0400 Subject: [PATCH 3/4] Update --sort-keys help message Use --sort-keys help message based on the json.tool docs at https://docs.python.org/3.6/library/json.html#basic-usage: > If sort_keys is true, then the output of dictionaries > will be sorted by key. --- Lib/json/tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index 0a3d8ef36c5bb5..de7196a36a67f4 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -28,7 +28,7 @@ def main(): default=sys.stdout, help='write the output of infile to outfile') parser.add_argument('--sort-keys', action='store_true', - help='sort dictionaries output alphabetically by key') + help='sort the output of dictionaries by key') options = parser.parse_args() # Read input JSON From 143d63bbb5ad7a55c3ee0db4614c4b12a9a6b461 Mon Sep 17 00:00:00 2001 From: Daniel Himmelstein Date: Thu, 20 Jul 2017 11:31:27 -0400 Subject: [PATCH 4/4] Remove comments Refs https://bugs.python.org/msg298692. Code was descriptive without the comments. --- Lib/json/tool.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/json/tool.py b/Lib/json/tool.py index de7196a36a67f4..076d130bad282d 100644 --- a/Lib/json/tool.py +++ b/Lib/json/tool.py @@ -31,7 +31,6 @@ def main(): help='sort the output of dictionaries by key') options = parser.parse_args() - # Read input JSON hook = collections.OrderedDict if options.sort_keys else None with options.infile as infile: try: @@ -39,7 +38,6 @@ def main(): except ValueError as e: raise SystemExit(e) - # Output JSON with options.outfile as outfile: json.dump(obj, outfile, sort_keys=options.sort_keys, indent=4) outfile.write('\n')