Python - Table of contents

Python

PEP 8

Unless otherwise specified PEP 8 standards should be written for all Python code. The easiest way to get up to speed with PEP 8 standards is using the flake8 python linter.

For more info on PEP 8 standards check out the official guide: https://www.python.org/dev/peps/pep-0008/. The Lightning Kite style guide will only outline a few of the most common PEP 8 standards.

$ pip install flake8
$ flake8 coolproject
coolproject/mod.py:97:1: F401 'shutil' imported but unused
coolproject/mod.py:625:17: E225 missing whitespace around operato
coolproject/mod.py:729:1: F811 redefinition of function 'readlines' from line 723
coolproject/mod.py:1028:1: F841 local variable 'errors' is assigned to but never used

Naming Conventions

  • Use underscores, not camelCase, for variable, function and method names (i.e. poll.get_unique_voters(), not poll.getUniqueVoters).
  • Use InitialCaps for class names (or for factory functions that return classes).
example_variable = "Variables use underscores"

def example_function():
    """
    Function names also use underscores
    """
    pass


class ClassName():
    """
    Classes use InitialCaps
    """

Spacing and Indentation

  • Class definitions should be separated by two line breaks returns.
  • Function and method definitions should be separated by only one line break.
  • A good rule of thumb is to limit line length to no more than 79 characters, however if limiting a certain line to 79 characters makes the code significantly uglier or harder to read don't worry about it. See the django docs style guide.
def some_function():
    pass

def another_function():
    """
    Functions and methods only need one space
    """
    pass


class SomeClass():
    """
    Class definitions should have two spaces
    """
    pass


class AnotherClass():
    pass

Django

Models

  • Field names should be all lowercase, using underscores instead of camelCase.
  • The class Meta should appear after the fields are defined, with a single blank line separating the fields and the class definition.
  • The order of model inner classes and standard methods should be as follows (noting that these are not all required):
    • All database fields
    • Custom manager attributes
    • class Meta
    • def __str__()
    • def save()
    • def get_absolute_url()
    • Any custom methods
  • If choices is defined for a given model field, define each choice as a tuple of tuples, with an all-uppercase name as a class attribute on the model. See example to the right:

# Correct field naming and class Meta placement
class Person(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=40)

    class Meta:
        verbose_name_plural = 'people'


# Model with choices field
class MyModel(models.Model):
    DIRECTION_UP = 'U'
    DIRECTION_DOWN = 'D'
    DIRECTION_CHOICES = (
        (DIRECTION_UP, 'Up'),
        (DIRECTION_DOWN, 'Down'),
    )

Views

In Django views, the first parameter in a view function should be called request.

def my_view(request, foo):
    # ...

Templates

In Django template code, put one (and only one) space between the curly brackets and the tag contents.

<div>
    <span>{{ variable }}</span>
</div>

Imports

We like to alphabetize our imports and split them up into 4 main sections: Python libraries, django code, third party libraries, and our application code.

import os    # Python libraries
import sys

from django import forms    # Django code
from django.core.conf import settings

from rest_framework import serializers  # Other third party libraries

from .models import User    # Our application code