Skip to content

Instantly share code, notes, and snippets.

@ruimaranhao
Last active February 7, 2021 03:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruimaranhao/4e18cbe3dad6f68040c32ed6709090a3 to your computer and use it in GitHub Desktop.
Save ruimaranhao/4e18cbe3dad6f68040c32ed6709090a3 to your computer and use it in GitHub Desktop.
A guide of best practices to developing in Python (meant for the first year students of the Instituto Superior Técnico, U.Lisboa).

A Guide of Best Practices for Python

A guide of best practices to developing in Python (meant for the first year students of the Instituto Superior Técnico, University of Lisbon). Do you want to have a hard-cover book on this, check this list out.

Inspired in Steve Sloria's gist.

In General

"Beautiful is better than ugly." - PEP 20

General Development Guidelines

  • "Explicit is better than implicit" - PEP 20
  • "Readability counts." - PEP 20
  • "Anybody can fix anything." - Khan Academy Development Docs
  • Fix each issue (bad design, wrong decision, or poor code) as soon as it is discovered.
  • "Now is better than never." - PEP 20
  • Test ruthlessly. Write docs for new features.
  • Even more important that Test-Driven Development--Human-Driven Development
  • These guidelines may--and probably will--change.

In Particular

Style

Follow PEP 8, when sensible.

Naming

  • Variables, functions, methods, packages, modules
    • lower_case_with_underscores
  • Classes and Exceptions
    • CapWords
  • Protected methods and internal functions
    • _single_leading_underscore(self, ...)
  • Private methods
    • __double_leading_underscore(self, ...)
  • Constants
    • ALL_CAPS_WITH_UNDERSCORES
General Naming Guidelines

Avoid one-letter variables (esp. l, O, I).

Exception: In very short blocks, when the meaning is clearly visible from the immediate context

Fine

for e in elements:
    e.mutate()

Avoid redundant labeling.

Yes

import audio

core = audio.Core()
controller = audio.Controller()

No

import audio
from audio import *

core = audio.AudioCore()
controller = audio.AudioController()

Prefer "reverse notation".

Yes

elements = ...
elements_active = ...
elements_defunct = ...

No

elements = ...
active_elements = ...
defunct_elements ...

Indentation

Up to you, but be consistent. Enough said.

Alt Text

However, note that: A tab could be a different number of columns depending on your environment, but a space is always one column. In terms of how many spaces (or tabs) constitutes indentation, it's more important to be consistent throughout your code than to use any specific tab stop value.

Equality checking

Avoid comparing to True, False or None.

Yes

if attr:
    print('True!')
    
if attr is True:
    print('True!')   
    
if not attr:
    print('False!')
    
if attr is None:
    print('None')

No

if attr == True:
    print('True!')
    
if attr == False:
    print('False!')
    
if attr == None:
    print('None')

List comprehensions

Use list comprehension when possible.

Yes

a = [3, 4, 5]
b = [i for i  in a  if i > 4]

#Or (filter is this case; map could also be more appropriate in other cases)
b = filter(lambda x: x > 4, a)

No

a = [3, 4, 5]
b = []
for i in a:
    if  i > 4:
        b.append(i)

Keyword with and files

The with statement ensures that clean-up code is executed. When opening a file, with will make sure that the file is closed after the with block.

Yes

with open('file.txt') as f:
    do_something_with_f

Imports

Import entire modules instead of individual symbols within a module. For example, for a top-level module canteen that has a file canteen/sessions.py,

Yes

import canteen
import canteen.sessions
from canteen import sessions

No

from canteen import get_user  # Symbol from canteen/__init__.py
from canteen.sessions import get_session  # Symbol from canteen/sessions.py

Exception: For third-party code where documentation explicitly says to import individual symbols.

Rationale: Avoids circular imports. See here.

Put all imports at the top of the page with three sections, each separated by a blank line, in this order:

  1. System imports
  2. Third-party imports
  3. Local source tree imports

Rationale: Makes it clear where each module is coming from.

Documentation

Follow PEP 257's docstring guidelines. reStructured Text and Sphinx can help to enforce these standards.

When possible, use one-line docstrings for obvious functions.

"""Return the pathname of ``foo``."""

Multiline docstrings should include

  • Summary line
  • Use case, if appropriate
  • Args
  • Return type and semantics, unless None is returned
"""Train a model to classify Foos and Bars.

Usage::

    >>> import klassify
    >>> data = [("green", "foo"), ("orange", "bar")]
    >>> classifier = klassify.train(data)

:param train_data: A list of tuples of the form ``(color, label)``.
:rtype: A :class:`Classifier <Classifier>`
"""

Notes

  • Use action words ("Return") rather than descriptions ("Returns").
  • Document __init__ methods in the docstring for the class.
class Person(object):
    """A simple representation of a human being.

    :param name: A string, the person's name.
    :param age: An int, the person's age.
    """
    def __init__(self, name, age):
        self.name = name
        self.age = age
On comments

Use them sparingly. Prefer code readability to writing a lot of comments. Often, small methods are more effective than comments.

No

# If the sign is a stop sign
if sign.color == 'red' and sign.sides == 8:
    stop()

Yes

def is_stop_sign(sign):
    return sign.color == 'red' and sign.sides == 8

if is_stop_sign(sign):
    stop()

When you do write comments, remember: "Strunk and White apply." - PEP 8

Line lengths

Don't stress over it. 80-100 characters is fine.

Use parentheses for line continuations.

wiki = (
    "The Colt Python is a .357 Magnum caliber revolver formerly manufactured "
    "by Colt's Manufacturing Company of Hartford, Connecticut. It is sometimes "
    'referred to as a "Combat Magnum". It was first introduced in 1955, the '
    "same year as Smith & Wesson's M29 .44 Magnum."
)
Advanced Material

Advanced Material: Testing

Strive for 100% code coverage, but don't get obsess over the coverage score.

General testing guidelines

  • Use long, descriptive names. This often obviates the need for doctrings in test methods.
  • Tests should be isolated. Don't interact with a real database or network. Use a separate test database that gets torn down or use mock objects.
  • Prefer factories to fixtures.
  • Never let incomplete tests pass, else you run the risk of forgetting about them. Instead, add a placeholder like assert False, "TODO: finish me".

Unit Tests

  • Focus on one tiny bit of functionality.
  • Should be fast, but a slow test is better than no test.
  • It often makes sense to have one testcase class for a single class or model.
import unittest
import factories

class PersonTest(unittest.TestCase):
    def setUp(self):
        self.person = factories.PersonFactory()

    def test_has_age_in_dog_years(self):
        self.assertEqual(self.person.dog_years, self.person.age / 7)

Functional Tests

Functional tests are higher level tests that are closer to how an end-user would interact with your application. They are typically used for web and GUI applications.

  • Write tests as scenarios. Testcase and test method names should read like a scenario description.
  • Use comments to write out stories, before writing the test code.
import unittest

class TestAUser(unittest.TestCase):

    def test_can_write_a_blog_post(self):
        # Goes to the her dashboard
        ...
        # Clicks "New Post"
        ...
        # Fills out the post form
        ...
        # Clicks "Submit"
        ...
        # Can see the new post
        ...

Notice how the testcase and test method read together like "Test A User can write a blog post".

Inspired by...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment