-
Notifications
You must be signed in to change notification settings - Fork 9
pyDAmonitor Code Norms
Guoqing Ge edited this page Mar 25, 2025
·
2 revisions
pyDAmonitor follows the PEP 8 Python code style and a few code norms specific to this repository.
- Indentation: Use 4 spaces per indentation level (no tabs).
- Line Length: Keep lines ≤ 79 characters (72 for docstrings/comments).
- Blank Lines:
- Use two blank lines before top-level functions and classes.
- Use one blank line between class methods or functions.
- Import should be in separate lines
- Order imports as:
- Standard library imports
- Third-party imports
- Local application/library imports
- Variables & functions: lower_case_with_underscores
- Constants: UPPER_CASE_WITH_UNDERSCORES
- Classes: CamelCase
- Private variables/functions: _single_leading_underscore
- Avoid names like l, O, I if possible (confusing with 1 and 0).
- No spaces inside brackets
- One space around operators
- No trailing whitespace
- Use # for inline comments and explanations.
- Use docstrings ("""Triple quotes""") for functions, classes, and modules.
- Functions should have one blank line before them inside classes.
- Use self as the first argument in instance methods.
- (1). Use
isforNone,True,Falsechecks.- Use
if x is Noneinstead ofif x == None
- Use
- (2) Use
withstatement for file handling
with open("file.txt") as f:
content = f.read()
- (3) Use list comprehensions for readability
new_list = [expression for item in iterable if condition]
squares = [x**2 for x in range(5)]
numbers = [x if x % 2 == 0 else -x for x in range(5)]
Advantages of List Comprehensions: 1) Concise and readable – Reduces multiple lines of code into one; 2) Faster execution – More efficient than traditional loops in many cases; 3). Easy to implement filtering and transformations in a single expression
- (4) Wrap common or reusable code blocks into functions so that they can be re-used by others, and by both the batch scripts and the Jupyter Notebook.