Skip to content

Latest commit

 

History

History
81 lines (41 loc) · 5.23 KB

File metadata and controls

81 lines (41 loc) · 5.23 KB

Python Coding

  1. PEP 8 Compliance: Follow the Python Enhancement Proposal 8 (PEP 8) style guide for Python code to ensure readability and consistency.

  2. Docstrings: Provide clear docstrings for each class and method, using a consistent format, such as Google style or NumPy/SciPy style.

  3. Type Annotations: Use type hints (PEP 484) to improve code readability and help static type checkers.

  4. Modular Design: Write modular code, breaking down functionalities into methods and classes with single responsibilities.

  5. Test Cases (TDD): Follow Test-Driven Development (TDD) by writing tests before actual code and striving for high test coverage.

  6. Error Handling: Implement robust error handling with custom exceptions and ensure no bare except: clauses that might catch unexpected exceptions.

  7. Code Patterns: Use design patterns appropriately where they fit, for example, Singleton for a single user instance or Factory pattern for creation logic.

  8. Consistent Naming Conventions: Use clear and descriptive variable, method, and class names.

  9. Efficient Algorithms: Choose or develop algorithms that optimize for time and space complexity suitable for the problem.

  10. Security Practices: Write code that follows security best practices, such as sanitizing inputs to prevent injection attacks.

  11. Logging: Include logging with appropriate levels (debug, info, warning, error, critical) and meaningful messages.

  12. Configurations: Externalize configurations and secrets, do not hard-code paths, URLs, or credentials.

  13. Code Reviews: Utilize code reviews and pull requests to maintain quality and share knowledge within the team.

  14. Dependencies Management: Use virtual environments and manage dependencies via tools like pip with requirements.txt or Pipenv.

  15. Version Control: Make atomic and meaningful commits with clear messages in version control systems like git.

  16. Documentation: Maintain a README.md or equivalent that gives an overview, setup, and usage instructions.

  17. Performance Profiling: Profile the code to find bottlenecks and optimize performance.

  18. Refactoring: Regularly revisit and refactor the code to improve its structure and readability.

  19. Asynchronous Programming: Use async/await patterns when dealing with I/O-bound operations to improve efficiency.

  20. Scalability Considerations: Design the code to be scalable, both in terms of data and traffic, ensuring that the system can handle growth.

  21. Decorator Use: Utilize decorators for cross-cutting concerns (like caching, timing, and logging) to keep method logic clean.

  22. Context Managers: Implement custom context managers if the class deals with external resources or needs to have enter and exit states.

  23. Metaclasses: Use metaclasses where appropriate for creating classes in a dynamic fashion or enforcing certain patterns.

  24. Descriptive Names: Employ descriptive, unambiguous names for methods and attributes.

  25. Duck Typing: Design methods to rely on duck typing and not be overly restrictive, embracing Python’s “ask forgiveness not permission” philosophy.

  26. Single Responsibility Principle: Ensure the class adheres strictly to the single responsibility principle.

  27. Magic Methods: Properly implement magic methods to integrate with Python’s built-in features and syntax (like __len__, __repr__, __str__, and __iter__).

  28. Dataclass Decorator: For simple data holding classes, use the @dataclass decorator to reduce boilerplate.

  29. Generators: Use generators (yield) for methods that can produce a sequence of values over time, optimizing memory usage.

  30. Property Decorators: Use @property decorators to create managed attributes, incorporating getter, setter, and deleter functionalities.

  31. Static and Class Methods: Appropriately use @staticmethod and @classmethod when methods do not need to interact with class instance state.

  32. Annotations and Type Checking: Include type hints and use tools like mypy to perform static type checking.

  33. Custom Exceptions: Define custom exception classes for error handling that are specific to the class's domain.

  34. Optimization with Cython: If performance is crucial, use Cython extensions for computationally intensive methods.

  35. Dynamic Attribute Access: Implement custom __getattr__, __getattribute__, and __setattr__ methods to handle dynamic attribute access if required.

  36. Efficient Attribute Storage: Use __slots__ to make classes more memory efficient by preventing the creation of instance dictionaries.

  37. Module-Level Abstraction: Ensure the class interacts with the rest of the module in a way that promotes loose coupling and high cohesion.

  38. Rich Comparison Methods: Implement the rich comparison methods (__eq__, __ne__, __lt__, __le__, __gt__, __ge__) to allow object comparison.

  39. Immutable Data Handling: For immutable classes, ensure all attributes are read-only, possibly using @property with a setter that raises an exception.

  40. Lazy Properties: Implement properties that do expensive computations lazily, meaning the computation is only done when the property is accessed for the first time.