Skip to content

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.

1. Code Layout

  • 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.

2. Imports

  • Import should be in separate lines
  • Order imports as:
    1. Standard library imports
    2. Third-party imports
    3. Local application/library imports

3. Naming Conventions

  • 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).

4. Whitespace Usage

  • No spaces inside brackets
  • One space around operators
  • No trailing whitespace

5. Comments & Docstrings

  • Use # for inline comments and explanations.
  • Use docstrings ("""Triple quotes""") for functions, classes, and modules.

6. Function & Class Definitions

  • Functions should have one blank line before them inside classes.
  • Use self as the first argument in instance methods.

7. Better Practices

  • (1). Use is for None, True, False checks.
    • Use if x is None instead of if x == None
  • (2) Use with statement 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.

Clone this wiki locally