Repository navigation
Expand file tree
/
Copy pathpython_rules.txt
More file actions
130 lines (96 loc) · 4.39 KB
/
Copy pathpython_rules.txt
File metadata and controls
130 lines (96 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Python Development Team Rules and Best Practices
## General Guidelines
1. **Embrace Pythonic Principles**
- Write code that is clear, concise, and explicit.
- Follow the Zen of Python (`import this`).
2. **Consistency and Readability**
- Use `PEP 8` as the coding standard.
- Ensure consistent formatting with tools like `black` or `autopep8`.
3. **Continuous Learning**
- Stay updated with Python's ecosystem and libraries.
- Share knowledge and resources with the team.
---
## Folder Architecture
### 1. Core Structure
```
project_root/
├── src/ # Main source code
│ ├── modules/ # Feature-specific modules
│ ├── utils/ # Utility functions
│ ├── __init__.py # Package initializer
│ └── main.py # Application entry point
├── tests/ # Test cases
│ ├── unit/ # Unit tests
│ ├── integration/ # Integration tests
│ └── conftest.py # Test configuration (if using pytest)
├── docs/ # Documentation
├── scripts/ # Automation and build scripts
├── virtualenv/ # Virtual environment (add to `.gitignore`)
├── requirements.txt # Dependency list
├── setup.py # Package configuration
├── .env # Environment variables
└── README.md # Project overview
```
---
## Coding Standards
1. **Code Formatting**
- Use `black` or `autopep8` for consistent formatting.
- Enforce linting with `flake8` or `pylint`.
2. **Error Handling**
- Use `try-except` blocks for predictable errors.
- Avoid catching generic exceptions (`except Exception:`).
- Log meaningful error messages.
3. **Documentation**
- Use docstrings for functions, classes, and modules.
- Follow `PEP 257` for docstring conventions.
- Include examples in documentation where applicable.
4. **Testing**
- Write unit tests for all public functions and classes.
- Use `pytest` for testing with clear and concise test cases.
- Mock external dependencies to isolate tests.
5. **Type Annotations**
- Add type hints to all functions and methods.
- Use tools like `mypy` to verify type correctness.
---
## Best Practices
### 1. Dependency Management
- Use `pip` or `pipenv` for managing dependencies.
- Pin versions in `requirements.txt` to ensure reproducibility.
- Regularly update dependencies and check for vulnerabilities with `pip-audit`.
### 2. Performance Optimization
- Use list comprehensions and generator expressions for efficiency.
- Profile code with `cProfile` or `line_profiler`.
- Optimize critical paths with libraries like `NumPy` or `Cython`.
### 3. Code Organization
- Follow a modular approach for scalability.
- Avoid circular imports by structuring dependencies properly.
- Use `__init__.py` files to define packages.
### 4. Testing and CI/CD
- Achieve at least 80% test coverage.
- Use `pytest-cov` for measuring test coverage.
- Automate testing and deployment with CI/CD pipelines (e.g., GitHub Actions, GitLab CI).
### 5. Security Best Practices
- Sanitize user inputs to prevent injection attacks.
- Avoid hardcoding sensitive data; use environment variables.
- Regularly update dependencies to patch vulnerabilities.
---
## Communication and Collaboration
1. **Code Reviews**
- Focus on logic, clarity, and adherence to standards.
- Suggest improvements and alternatives constructively.
2. **Documentation Sharing**
- Maintain updated `README.md` and API documentation.
- Use tools like `Sphinx` or `mkdocs` for generating documentation.
3. **Knowledge Sharing**
- Host regular Python workshops or code walkthroughs.
- Document lessons learned from solving complex problems.
---
## Continuous Improvement
1. **Retrospectives**
- Reflect on completed projects to identify areas for improvement.
- Share challenges and solutions in team meetings.
2. **Tooling Enhancements**
- Explore and adopt new tools or libraries that improve efficiency.
- Periodically evaluate the team's development setup.
---
By following these Python-specific rules and best practices, the development team can ensure high-quality, maintainable, and efficient software while leveraging Python's strengths and simplicity.