Last active
February 11, 2022 19:00
-
-
Save Diapolo10/6e601ec0ed32b51ddcf8d51723bf5bf3 to your computer and use it in GitHub Desktop.
Python style guide example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# The above two lines specify two things. The order must always be the same if both are used. | |
# The first line is a shebang, used by Unix-like systems to determine how to run a file. | |
# The second is a way to specify the encoding used by the file. | |
# Neither are necessary, especially the second one, since Python 3 is UTF-8 by default. | |
""" | |
Module-level docstring | |
This describes what the module is about. | |
It must be placed before any valid Python code, | |
otherwise it will be ignored. | |
See PEP-257 for the official docstring conventions. | |
""" | |
# Imports from the standard library. Note the alphabetical order. | |
import math | |
import operator | |
from pathlib import Path | |
# Imports from third-party libraries. | |
import numpy as np | |
import requests | |
from escapyde import FRED, FGREEN, FBLUE | |
from iplib3 import IPAddress | |
# Imports from modules defined in the project. | |
import my_module | |
from project_constants import MAX_POWER_LIMIT | |
# Constants used in the project. Order either alphabetical or otherwise logical. | |
DESKTOP = Path.home() / 'Desktop' | |
ROOT_DIR = Path(__file__).parent | |
DATA_DIR = ROOT_DIR / 'data' | |
MAX_CHEESE_WEIGHT = 420 # kg | |
# Classes and functions; either can be first, there's no clear consensus. | |
# Just don't mix them. Order logically, then alphabetically. | |
def age_total(people): | |
"""Calculate and return the total age of given people.""" | |
total = 0 | |
for person in people: | |
total = my_module.add(total, person.age) | |
return total | |
class Person: | |
def __init__(self, name, age=0): | |
self.name = name | |
self.age = age | |
# If you want to run the module as a script, add the following to the bottom. | |
# Ideally you'd also have a main function and just call it there. That way you | |
# can run everything even if you import the module, should you wish. | |
if __name__ == '__main__': | |
people = [Person('test') for _ in range(10)] | |
print(age_total(people)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Lorem Ipsum | |
Dolor sit amet. | |
""" | |
def add(a, b): | |
return a + b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Lorem Ipsum | |
Dolor sit amet. | |
""" | |
MAX_POWER_LIMIT = 255 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment