defremove_pycache(): for app in APP_LIST: dir_path = os.path.join(PROJECT_DIR, app, '__pycache__') if os.path.exists(dir_path): print('rm', dir_path)
defcompile_cython(): # compile file_Set = set() for app in APP_LIST: for filename in COMPILE_FILES: file_Set.add(os.path.join(app, filename))
setup( ext_modules=cythonize(file_Set) )
# clean for app in APP_LIST: for filename in COMPILE_FILES: file_path = os.path.join(app, filename) print('rm', file_path) os.remove(file_path)
defcompile_pyc(): # compile for app in APP_LIST: for filename in os.listdir(app): if filename.endswith('.py'): filepath = os.path.join(app, filename) try: compile(filepath) print("Success compile file: %s" % filepath)
Software engineering principles, from Robert C. Martin”s book Clean Code, adapted for Python. This is not a style guide. It”s a guide to producing readable, reusable, and refactorable software in Python.
Not every principle herein has to be strictly followed, and even fewer will be universally agreed upon. These are guidelines and nothing more, but they are ones codified over many years of collective experience by the authors of Clean Code.
Even better Python is (also) an object oriented programming language. If it makes sense, package the functions together with the concrete implementation of the entity in your code, as instance attributes, property methods, or methods:
We will read more code than we will ever write. It”s important that the code we do write is readable and searchable. By not naming variables that end up being meaningful for understanding our program, we hurt our readers. Make your names searchable.
Bad:
import time
# What is the number 86400 for again? time.sleep(86400)
Good:
import time
# Declare them in the global namespace for the module. SECONDS_IN_A_DAY = 60 * 60 * 24 time.sleep(SECONDS_IN_A_DAY)
Use default arguments instead of short circuiting or conditionals
Tricky
Why write:
import hashlib
defcreate_micro_brewery(name): name = "Hipster Brew Co."if name isNoneelse name slug = hashlib.sha1(name.encode()).hexdigest() # etc.
… when you can specify a default argument instead? This also makes it clear that you are expecting a string as the argument.
Good:
from typing import Text import hashlib
defcreate_micro_brewery(name: Text = "Hipster Brew Co."): slug = hashlib.sha1(name.encode()).hexdigest() # etc.
Functions
Function arguments (2 or fewer ideally)
Limiting the amount of function parameters is incredibly important because it makes testing your function easier. Having more than three leads to a combinatorial explosion where you have to test tons of different cases with each separate argument.
Zero arguments is the ideal case. One or two arguments is ok, and three should be avoided. Anything more than that should be consolidated. Usually, if you have more than two arguments then your function is trying to do too much. In cases where it”s not, most of the time a higher-level object will suffice as an argument.
menu = Menu( { "title": "My Menu", "body": "Something about my menu", "button_text": "OK", "cancellable": False } )
Also good
from typing import Text
classMenuConfig: """A configuration for the Menu. Attributes: title: The title of the Menu. body: The body of the Menu. button_text: The text for the button label. cancellable: Can it be cancelled? """ title: Text body: Text button_text: Text cancellable: bool = False
defcreate_menu(config: MenuConfig) -> None: title = config.title body = config.body # ...
config = MenuConfig() config.title = "My delicious menu" config.body = "A description of the various items on the menu" config.button_text = "Order now!" # The instance attribute overrides the default class attribute. config.cancellable = True
create_menu(config)
Fancy
from typing import NamedTuple
classMenuConfig(NamedTuple): """A configuration for the Menu. Attributes: title: The title of the Menu. body: The body of the Menu. button_text: The text for the button label. cancellable: Can it be cancelled? """ title: str body: str button_text: str cancellable: bool = False
create_menu( MenuConfig( title="My delicious menu", body="A description of the various items on the menu", button_text="Order now!" ) )
Even fancier
from typing import Text from dataclasses import astuple, dataclass
@dataclass classMenuConfig: """A configuration for the Menu. Attributes: title: The title of the Menu. body: The body of the Menu. button_text: The text for the button label. cancellable: Can it be cancelled? """ title: Text body: Text button_text: Text cancellable: bool = False
create_menu( MenuConfig( title="My delicious menu", body="A description of the various items on the menu", button_text="Order now!" ) )
Even fancier, Python3.8+ only
from typing import TypedDict, Text
classMenuConfig(TypedDict): """A configuration for the Menu. Attributes: title: The title of the Menu. body: The body of the Menu. button_text: The text for the button label. cancellable: Can it be cancelled? """ title: Text body: Text button_text: Text cancellable: bool
defcreate_menu(config: MenuConfig): title = config["title"] # ...
create_menu( # You need to supply all the parameters MenuConfig( title="My delicious menu", body="A description of the various items on the menu", button_text="Order now!", cancellable=True ) )
Functions should do one thing
This is by far the most important rule in software engineering. When functions do more than one thing, they are harder to compose, test, and reason about. When you can isolate a function to just one action, they can be refactored easily and your code will read much cleaner. If you take nothing else away from this guide other than this, you”ll be ahead of many developers.
Bad:
from typing import List
classClient: active: bool
defemail(client: Client) -> None: pass
defemail_clients(clients: List[Client]) -> None: """Filter active clients and send them an email. """ for client in clients: if client.active: email(client)
Good:
from typing import List
classClient: active: bool
defemail(client: Client) -> None: pass
defget_active_clients(clients: List[Client]) -> List[Client]: """Filter active clients. """ return [client for client in clients if client.active]
defemail_clients(clients: List[Client]) -> None: """Send an email to a given list of clients. """ for client in get_active_clients(clients): email(client)
Do you see an opportunity for using generators now?
Even better
from typing import Generator, Iterator
classClient: active: bool
defemail(client: Client): pass
defactive_clients(clients: Iterator[Client]) -> Generator[Client, None, None]: """Only active clients""" return (client for client in clients if client.active)
defemail_client(clients: Iterator[Client]) -> None: """Send an email to a given list of clients. """ for client in active_clients(clients): email(client)
Function names should say what they do
Bad:
classEmail: defhandle(self) -> None: pass
message = Email() # What is this supposed to do again? message.handle()
Good:
classEmail: defsend(self) -> None: """Send this message"""
message = Email() message.send()
Functions should only be one level of abstraction
When you have more than one level of abstraction, your function is usually doing too much. Splitting up functions leads to reusability and easier testing.
statements = code.split('\n') tokens = [] for regex in regexes: for statement in statements: pass
ast = [] for token in tokens: pass
for node in ast: pass
Good:
from typing import Tuple, List, Text, Dict
REGEXES: Tuple = ( # ... )
defparse_better_js_alternative(code: Text) -> None: tokens: List = tokenize(code) syntax_tree: List = parse(tokens)
for node in syntax_tree: pass
deftokenize(code: Text) -> List: statements = code.split() tokens: List[Dict] = [] for regex in REGEXES: for statement in statements: pass
return tokens
defparse(tokens: List) -> List: syntax_tree: List[Dict] = [] for token in tokens: pass
return syntax_tree
Don”t use flags as function parameters
Flags tell your user that this function does more than one thing. Functions should do one thing. Split your functions if they are following different code paths based on a boolean.
Bad:
from typing import Text from tempfile import gettempdir from pathlib import Path
A function produces a side effect if it does anything other than take a value in and return another value or values. For example, a side effect could be writing to a file, modifying some global variable, or accidentally wiring all your money to a stranger.
Now, you do need to have side effects in a program on occasion - for example, like in the previous example, you might need to write to a file. In these cases, you should centralize and indicate where you are incorporating side effects. Don”t have several functions and classes that write to a particular file - rather, have one (and only one) service that does it.
The main point is to avoid common pitfalls like sharing state between objects without any structure, using mutable data types that can be written to by anything, or using an instance of a class, and not centralizing where your side effects occur. If you can do this, you will be happier than the vast majority of other programmers.
Bad:
# type: ignore
# This is a module-level name. # It"s good practice to define these as immutable values, such as a string. # However... fullname = "Ryan McDermott"
defsplit_into_first_and_last_name() -> None: # The use of the global keyword here is changing the meaning of the # the following line. This function is now mutating the module-level # state and introducing a side-effect! global fullname fullname = fullname.split()
split_into_first_and_last_name()
# MyPy will spot the problem, complaining about 'Incompatible types in # assignment: (expression has type "List[str]", variable has type "str")' print(fullname) # ["Ryan", "McDermott"]
# OK. It worked the first time, but what will happen if we call the # function again?
# The reason why we create instances of classes is to manage state! person = Person("Ryan McDermott") print(person.name) # => "Ryan McDermott" print(person.name_as_first_and_last) # => ["Ryan", "McDermott"]