-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path.python_rules
More file actions
71 lines (52 loc) · 2.31 KB
/
Copy path.python_rules
File metadata and controls
71 lines (52 loc) · 2.31 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
# Python Development Rules
This file contains coding guidelines and best practices for Python development in this project.
## Code Style
- Follow PEP 8 style guide
- Use type hints for function signatures and class attributes
- Maximum line length: 88 characters (Black formatter default)
- Use meaningful variable and function names (snake_case)
- Use docstrings for modules, classes, and functions
## Type Hints and Annotations
- Use type hints for all function parameters and return values
- Use `Optional[T]` for values that may be None
- Use `Union[T1, T2]` for values that can be multiple types
- Use `List[T]`, `Dict[K, V]`, `Set[T]` for collections
- Consider using `TypedDict` for structured dictionaries
## Error Handling
- Use specific exception types, not bare `except:`
- Raise exceptions for exceptional circumstances
- Use context managers (`with` statement) for resource management
- Document which exceptions a function may raise
## Modern Python Features
- Use f-strings for string formatting: `f"Hello {name}"`
- Use list/dict/set comprehensions when appropriate
- Use `pathlib.Path` instead of `os.path` for file operations
- Use `dataclasses` or `attrs` for data containers
- Use `Enum` for constants and enumerated values
## Functions and Classes
- Keep functions small and focused (single responsibility)
- Use `@property` for computed attributes
- Use `@staticmethod` and `@classmethod` appropriately
- Prefer composition over inheritance
- Use `__slots__` for memory-efficient classes
## Async Programming
- Use `async`/`await` for I/O-bound operations
- Use `asyncio.gather()` for concurrent tasks
- Use `asyncio.create_task()` to run tasks in background
- Avoid blocking calls in async functions
## Testing
- Write unit tests using `pytest`
- Use fixtures for test setup
- Aim for high test coverage (>80%)
- Use `pytest.mark.parametrize` for testing multiple cases
## Dependencies
- Use `requirements.txt` or `pyproject.toml` for dependencies
- Pin dependency versions for reproducibility
- Use virtual environments (`venv` or `conda`)
- Keep dependencies minimal and up-to-date
## Performance
- Profile before optimizing (`cProfile`, `line_profiler`)
- Use generators for large datasets
- Use `__slots__` for memory efficiency
- Consider `numpy` for numerical computations
- Use `lru_cache` for expensive function calls