- Language: Python 3.8+
- Package Manager: pip
- Virtual Environment: venv
- Main Source Directory:
modules/*/src/(Gradle-style multi-module monorepo)
- Follow PEP 8 style guide for Python code
- Use 4 spaces for indentation (no tabs)
- Maximum line length: 79 characters for code, 72 for docstrings
- Use snake_case for functions and variables
- Use PascalCase for class names
- Use UPPER_CASE for constants
- Group imports in the following order:
- Standard library imports
- Related third-party imports
- Local application/library specific imports
- Use absolute imports when possible
- Avoid wildcard imports (
from module import *)
- Variables:
lowercase_with_underscores - Functions:
lowercase_with_underscores() - Classes:
CapitalizedWords - Constants:
UPPER_CASE_WITH_UNDERSCORES - Private methods/attributes:
_leading_underscore
Use type hints for function signatures:
def query_by_city(city: str) -> int:
"""Query DynamoDB table by city name."""
count: int = 0
# implementation
return countUse Python's logging module instead of print statements:
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info("Processing started")
logger.warning("Resource not found")
logger.error("Operation failed", exc_info=True)- Keep related functionality together in modules
- Use
__init__.pyfiles to expose public APIs - Use
if __name__ == '__main__':for script entry points
- Store configuration in separate files (e.g.,
config.pyor.inifiles) - Use environment variables for sensitive data
- Never commit credentials to version control
- Ignore tests since i am only playing around.
# Create
python3 -m venv venv
# Activate
source venv/bin/activate # macOS/Linux
# Install dependencies
pip install -r requirements.txt
# Deactivate
deactivate- Keep
requirements.txtupdated - Pin versions for production:
boto3==1.26.0 - Use ranges for development:
boto3>=1.26.0 - Consider using
requirements-dev.txtfor development dependencies
make formatmake lintmake typecheckConsider using pre-commit hooks:
# .pre-commit-config.yaml
repos:
- repo: https://github.com/psf/black
rev: 23.0.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8Prefer list comprehensions over map/filter when readable:
# Good
squares = [x**2 for x in range(10)]
# Also good for readability
result = [process(item) for item in items if item.is_valid()]Use generators for large datasets:
def read_large_file(file_path):
with open(file_path) as f:
for line in f:
yield line.strip()Use context managers for resource management:
class DynamoDBConnection:
def __enter__(self):
self.client = boto3.client('dynamodb')
return self.client
def __exit__(self, exc_type, exc_val, exc_tb):
# cleanup
passUse dataclasses for simple data structures (Python 3.7+):
from dataclasses import dataclass
from typing import Optional
@dataclass
class CustomerOrder:
order_id: int
customer_id: str
item_name: str
quantity: int = 1
status: Optional[str] = NoneUse pathlib for path operations:
from pathlib import Path
file_path = Path('modules/files/src/pythonbox_files/data.csv')
if file_path.exists():
data = file_path.read_text()Use f-strings (Python 3.6+):
# Good
message = f"Processing {count} items from {city}"
# Avoid
message = "Processing {} items from {}".format(count, city)
message = "Processing " + str(count) + " items from " + cityWhen asked to create a simulation, always:
- Use
matplotlibfor visualization — animate withmatplotlib.animation.FuncAnimationand render withPillowWriter. - Save a GIF output next to the script via
Path(__file__).parent / "<name>.gif". - Show the animation in the UI — after
anim.save(...), always callplt.show()so the live window opens, thenplt.close(fig):anim.save(str(out_path), writer=animation.PillowWriter(fps=FPS)) print(f"Saved → {out_path}") print("Displaying animation in UI window …") plt.show() plt.close(fig)
- Precompute all frames first, then render once — follow the pattern in
physics/qualia_modality.py,physics/free_wave_packet.py, andmath/fractal.py. - Use the macOS-friendly backend fallback at the top of every new simulation file:
import matplotlib for _backend in ("MacOSX", "TkAgg", "Qt5Agg", "Agg"): try: matplotlib.use(_backend) import matplotlib.pyplot as _probe # noqa: F401 break except Exception: continue
- Print progress with
print()so the user can follow along (e.g. "Pre-computing …", "Rendering N frames to GIF …", "Saved → "). - Place all work at module level (not inside
__main__) to match the repo convention; keepif __name__ == "__main__": passat the bottom. - Dependencies: use
numpy+matplotlib+pillow(already inrequirements.txt); add any new dependency torequirements.txt.
- Read existing code style and match it
- Add type hints to new functions
- Handle exceptions appropriately
- Use logging instead of print statements
- Update requirements.txt if adding new dependencies