Skip to content

bpo-30971: Improve code readability of json.tool #2720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

dhimmel
Copy link
Contributor

@dhimmel dhimmel commented Jul 15, 2017

Several months ago I worked on pull requests to add new options to the json.tool command line utility. These pull requests are still open, pending further design discussion and consensus on whether to add them:

Related, #346 fixed a small testing bug in json.tool and was merged.

Since #345 and #201 have been inactive for some time, I wanted to take the non-controversial code changes from those PRs. This PR contains readability improvements and minor refactoring to json.tool. The goal is to get some of the previous work merged and make the json.tool source easier to maintain and enhance going forward. Admittedly, the changes are quite minor.

@mention-bot
Copy link

@dhimmel, thanks for your PR! By analyzing the history of the files in this pull request, we identified @benjaminp, @tiran and @berkerpeksag to be potential reviewers.

Copy link
Contributor Author

@dhimmel dhimmel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left inline comments for the motivation behind each change.

Lib/json/tool.py Outdated
@@ -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')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line exceeded the PEP8 line length, hence the rephrasing.

Lib/json/tool.py Outdated
except ValueError as e:
raise SystemExit(e)

# Output JSON
outfile = options.outfile or sys.stdout
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move variable definition close to where it's used.

Lib/json/tool.py Outdated
sort_keys = options.sort_keys
hook = collections.OrderedDict if sort_keys else None
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the heavily indented if-else for readability

Lib/json/tool.py Outdated
@@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary argument.

Lib/json/tool.py Outdated
options = parser.parse_args()

# Read input JSON
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment for readability.

@bitdancer
Copy link
Member

We generally don't make changes like these unless we are modifying the code for other reasons.

Lib/json/tool.py Outdated
except ValueError as e:
raise SystemExit(e)

# Output JSON
outfile = options.outfile or sys.stdout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this change helps, it is easier to read when we process all options in one place, and then use the process options.

It would be nice if we could integrate this in options:

outfile = options.outfile or sys.stdout

Does this work?

 parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
                      default=sys.stdout,
                      help='write the output of infile to outfile')

Lib/json/tool.py Outdated
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... dictionaries output ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nirs I changed it to:

sort dictionaries output alphabetically by key

as per your suggestion. But would it make more sense as possessive:

sort dictionaries' output alphabetically by key

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would check other modules and use the same style.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would phrase this as "sort alphabetically by key when generating dictionary output".

Copy link
Contributor

@nirs nirs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

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.
@dhimmel
Copy link
Contributor Author

dhimmel commented Jul 18, 2017

If this issue is so simple that there’s no need for an issue to track any discussion of what the pull request is trying to solve (e.g. fixing a spelling mistake), then the pull request needs to have the “skip issue” label added to it.

bedevere/issue-number is failing. I assumed this PR doesn't require an issue, but let me know if otherwise.

@bitdancer
Copy link
Member

This absolutely needs an issue to discuss whether or not to apply it. We probably won't, as we don't normally accept "prettify" pull requests.

@dhimmel dhimmel changed the title Improve code readability of json.tool bpo-30971: Improve code readability of json.tool Jul 19, 2017
@dhimmel
Copy link
Contributor Author

dhimmel commented Jul 19, 2017

This absolutely needs an issue to discuss whether or not to apply it.

Thanks @bitdancer for the guidance. I opened bpo-30971.

Refs https://bugs.python.org/msg298692.
Code was descriptive without the comments.
@dhimmel
Copy link
Contributor Author

dhimmel commented Jul 20, 2017

I'm closing the pull request in response to the advice received in bpo-30971.

Specifically from @bitdancer:

I think using this PR as a base for your feature PRs, and then committing everything together if they are accepted, would be the way to go.

And from @berkerpeksag:

Everyone has their own preferences and it's usually not enough to justify code churn.

Will rebase my other PRs with json.tool enhancements on top of these changes.

@dhimmel dhimmel closed this Jul 20, 2017
dhimmel added a commit to dhimmel/cpython that referenced this pull request Jul 20, 2017
Originall from python#2720

Set default option for infile and outfile as per
python#2720 (review)

Use --sort-keys help message based on the json.tool docs at
https://docs.python.org/3.6/library/json.html#basic-usage:

Remove commens as per https://bugs.python.org/msg298692.
Code was descriptive without the comments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants