Skip to content

Latest commit

 

History

History
116 lines (76 loc) · 4.17 KB

CODE_STYLE.md

File metadata and controls

116 lines (76 loc) · 4.17 KB

Code of Style

Table of Contents

1. Introduction

This Code of Style provides guidelines for writing Python code for a project hosted on GitHub. Adhering to these guidelines helps ensure that code is consistent, readable, and maintainable.

2. Naming Conventions

The following naming conventions should be used in Python code:

  • Module names should be in lowercase and use underscores as necessary to improve readability (e.g., my_module.py).
  • Class names should use CamelCase (e.g., MyClass).
  • Function and method names should be in lowercase with words separated by underscores (e.g., my_function()).
  • Variable names should be in lowercase with words separated by underscores (e.g., my_variable).
  • Constant names should be in all uppercase with words separated by underscores (e.g., MY_CONSTANT).

3. Code Formatting

Code should be formatted according to the PEP 8 style guide. The following guidelines should be followed:

  • Use 4 spaces for indentation.
  • Limit lines to a maximum of 79 characters.
  • Use parentheses to group multiple lines of an expression or function call.
  • Put import statements at the top of the file, after any module comments but before global variables or constants.
  • Put a blank line between each function or class definition.

4. Comments

Comments should be used to explain code where necessary. The following guidelines should be followed:

  • Use complete sentences for comments.
  • Capitalize the first word of each sentence.
  • Use inline comments sparingly.
  • Use descriptive variable and function names instead of comments where possible.

5. Documentation

Documentation should be provided for each module, class, and function using docstrings. The following guidelines should be followed:

  • Use triple quotes for docstrings.
  • Include a one-line summary at the beginning of the docstring.
  • Include parameter descriptions, return value descriptions, and any exceptions raised by the function or method.

6. Imports

Imports should be used at the top of the file. The following guidelines should be followed:

  • Use absolute imports.
  • Put each import on a separate line.
  • Use import statements, not from ... import ... statements.

7. Error Handling

Errors should be handled gracefully using try-except blocks where necessary. The following guidelines should be followed:

  • Catch specific exceptions instead of using a bare except: statement.
  • Use meaningful error messages.
  • Use the logging module for error messages instead of print statements.

8. Testing

Unit tests should be provided for each function or method. The following guidelines should be followed:

  • Tests should be placed in a separate tests/ directory.
  • Use the unittest module for testing.
  • Use descriptive test names that explain the purpose of the test.
  • Test for both expected results and edge cases.
  • Include test coverage reports with your code.

9. Version Control

Version control should be used to track changes to your code. The following guidelines should be followed:

  • Use Git as the version control system.
  • Use meaningful commit messages that explain the purpose of the changes.
  • Use feature branches for developing new features.
  • Use pull requests for code review and merging changes into the main branch.

10. Conclusion

Adhering to this Code of Style will help ensure that your Python code is consistent, readable, and maintainable. By following these guidelines, you can make your code more accessible to others and improve your productivity as a developer.