intro
as i start to take on larger software projects that require continuous revisiting and refactoring, i want to create a style guide that i can go back to in order to create consistent and readable code.
the pep style guide is great, but i have a few preferences that differ with its conventions and also some preferences where the style guide does not specify a best practice.
although this post is dated for 11/27/24, i want to continue to build refine this guide as i code more in python. hopefully nothing that i put changes too much.
overall structure
the overall structure of a python script should be:
- file declaration: file name, short description of purpose.
- imports
- built-in libraries
- third party packages
- local imports from other project files
- if load_dotenv() is required, put it at the bottom...
- constants: organized by function
- functions: organized and grouped by function
- helper functions
- core functions
- main() function
- --> functions should call those above them. most abstracted function at the bottom
- execution: if __name__ == main...
whitespace
- two lines of whitespace after a function or code section (e.g. constants)
- one line of whitespace between logic code blocks within a funciton
- one line of whitespace after docstring
- one line of whitespace after comment identifier (e.g. # CONSTANTS)