Skip to content

bpo-35075: Fix broken url in the pprint documentation #10201

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

Merged
merged 7 commits into from
Nov 1, 2018

Conversation

pablogsal
Copy link
Member

@pablogsal pablogsal commented Oct 28, 2018

@bedevere-bot bedevere-bot added docs Documentation in the Doc dir awaiting merge labels Oct 28, 2018
@pablogsal pablogsal requested a review from methane October 28, 2018 21:17
@pablogsal pablogsal self-assigned this Oct 28, 2018
@lysnikolaou
Copy link
Member

This doesn't work on my local Python build with a TypeError: decode() argument 1 must be str, not None. Apparently http_info.get_content_charset() returns None.

@pablogsal
Copy link
Member Author

pablogsal commented Oct 28, 2018

@lysnikolaou Yup, the http_info charset may be missing, so let's default to utf-8 if there is no charset in the headers.

Thanks for the catch!

@@ -217,9 +217,10 @@ let's fetch information about a project from `PyPI <https://pypi.org>`_::
>>> import json
>>> import pprint
>>> from urllib.request import urlopen
>>> with urlopen('http://pypi.org/project/Twisted/json') as url:
>>> with urlopen('https://pypi.org/pypi/Twisted/json') as url:
Copy link
Member

Choose a reason for hiding this comment

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

Output of this URL is changed, and it's too large for example. (Output is in below).
I think we should find better json example.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can add a new JSON file in https://github.com/python/pythontestdotnet which is used for testing purposes? Is it necessary to make a request to pypi since we can define a minimal custom dict based on the current example to show the output and usage of other parameters like depth and width

Copy link
Member

Choose a reason for hiding this comment

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

By using PyPI, it looks more "pragmatic" rather than "artificial" example.

I found nice example: https://pypi.org/pypi/sampleproject/json
This project is maintained by PyPA.

... http_info = url.info()
... raw_data = url.read().decode(http_info.get_content_charset())
... charset = http_info.get_content_charset(failobj="utf-8")
... raw_data = url.read().decode(charset)
Copy link
Member

Choose a reason for hiding this comment

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

This is example of pprint. So this part should be simple as possible:

with urlopen('https://pypi.org/pypi/Twisted/json') as resp:
    project_info = json.load(resp)

Note that json.load and json.loads now support loading bytes encoded with UTF-8.
So no need to manual decode.

@bedevere-bot
Copy link

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@pablogsal
Copy link
Member Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@methane: please review the changes made to this pull request.

Co-Authored-By: pablogsal <[email protected]>
... raw_data = url.read().decode(http_info.get_content_charset())
>>> project_info = json.loads(raw_data)
>>> with urlopen('https://pypi.org/pypi/sampleproject/json') as resp:
... project_info = json.load(resp)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
... project_info = json.load(resp)
... project_info = json.load(resp)

@methane
Copy link
Member

methane commented Oct 31, 2018

Hm, output seems long yet. Please try only 'info' part:

project_info = json.load(resp)['info']

And depth=1, width=60, instead of depth=2 and width=50.

Co-Authored-By: pablogsal <[email protected]>
@miss-islington
Copy link
Contributor

Thanks @pablogsal for the PR 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 1, 2018
@bedevere-bot
Copy link

GH-10282 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 1, 2018
@bedevere-bot
Copy link

GH-10283 is a backport of this pull request to the 3.6 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants