realpython / realpython/python-guide

Addition of specific example of great code from one of the listed repositories.

Open
#995 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Batchfile
Stars
29.8k
Forks
5.9k
PR merge metrics
No merged PRs in 30d

Description

I absolutely love the Reading Code section, and I've been going through some of the repositories and identifying specific examples of why the code is good.

I was hoping to add one such example to the page. Specifically, from howdoi. It's virtually at the top of the codebase and it deals with standardizing the repository for Python 2 and 3.

https://github.com/gleitz/howdoi/blob/master/howdoi/howdoi.py

# Handle imports for Python 2 and 3
if sys.version < '3':
    import codecs
    from urllib import quote as url_quote
    from urllib import getproxies

    # Handling Unicode: http://stackoverflow.com/a/6633040/305414
    def u(x):
        return codecs.unicode_escape_decode(x)[0]
else:
    from urllib.request import getproxies
    from urllib.parse import quote as url_quote

    def u(x):
        return x

I think that there are two (and a half) key pedagogical takeaways from this.

The half takeaway is that supporting different versions of Python need not be a grueling task.

The remaining two are -

  1. Appropriate use of control flow for module imports and function creation
  2. Standardizing an API so that its source is indistinguishable to the rest of the code and functionality is consistent.

I haven't formally written this up yet, but if you think it would be a useful example, I can do that and submit a pull request.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Reading Code section and the linked howdoi/howdoi.py example. Draft a formal explanation of the Python 2/3 import handling, control flow, and consistent API described in the issue, then add it to the appropriate page. Done means the example and its pedagogical takeaways are included in a pull request.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.