This repository contains the company-wide standard configuration files and project structure for Python projects at DatumBrain. These standards ensure consistency, maintainability, and quality across all Python projects.
This template provides a standardized setup for Python projects including:
- Code quality tools (Ruff, Black, MyPy)
- Testing framework (Pytest with coverage)
- Project structure (src layout)
- Configuration files (pyproject.toml, .gitignore, .editorconfig)
-
Clone this repository as a template for your new project:
git clone https://github.com/datumbrain/python-template your-project-name cd your-project-name
-
Update project-specific details:
- Rename
src/your_package_name
to your actual package name - Update package name references in
pyproject.toml
- Update author information and project metadata
- Rename
-
Install Poetry (if not already installed):
curl -sSL https://install.python-poetry.org | python3 -
-
Install dependencies:
poetry install
-
Activate the virtual environment:
poetry shell
.
├── src/ # Source code directory
│ └── your_package_name/ # Your main package
│ └── __init__.py # Package initialization
├── tests/ # Test directory
│ ├── __init__.py # Test package initialization
│ └── conftest.py # Pytest configuration and fixtures
├── .editorconfig # Editor configuration for consistency
├── .gitignore # Git ignore patterns
├── pyproject.toml # Project configuration and dependencies
└── README.md # Project documentation
src/
: Contains all production code. Using src layout prevents accidentally importing from the repository root.tests/
: Contains all test files. Tests should mirror the structure of the src directory..editorconfig
: Ensures consistent coding styles between different editors and IDEs.pyproject.toml
: Central configuration file for the project, including dependencies, build system, and tool configurations.
The pyproject.toml
file is the central configuration hub containing:
- Project metadata: Name, version, description, authors
- Dependencies: Using Poetry for dependency management
- Tool configurations:
- Ruff: Fast Python linter and formatter
- MyPy: Static type checking
- Pytest: Testing framework configuration
- Coverage: Code coverage settings
- Black: Code formatting (optional, alongside Ruff)
Comprehensive gitignore file covering:
- Python artifacts (
__pycache__
,*.pyc
, etc.) - Virtual environments (
venv
,.venv
, etc.) - IDE files (
.idea
,.vscode
) - Test and coverage reports
- OS-specific files (
.DS_Store
,Thumbs.db
)
Standardizes:
- Character encoding (UTF-8)
- Line endings (LF for Unix-style)
- Indentation (4 spaces for Python)
- Trailing whitespace handling
- File-specific settings for YAML, JSON, Markdown, etc.
# Run linter
poetry run ruff check .
# Fix auto-fixable issues
poetry run ruff check --fix .
# Format code
poetry run ruff format .
poetry run mypy src
poetry run black src tests
# Run all tests
poetry run pytest
# Run with coverage
poetry run pytest --cov
# Run specific test file
poetry run pytest tests/test_module.py
# Run tests in parallel
poetry run pytest -n auto
# Run only unit tests
poetry run pytest -m unit
# Skip slow tests
poetry run pytest -m "not slow"
# Run integration tests
poetry run pytest -m integration
Set up pre-commit hooks to automatically run checks before commits:
# Install pre-commit
poetry add --group dev pre-commit
# Create .pre-commit-config.yaml
cat > .pre-commit-config.yaml << EOF
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.0
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.0
hooks:
- id: mypy
additional_dependencies: [types-all]
EOF
# Install the git hook scripts
poetry run pre-commit install
# Run against all files (first time)
poetry run pre-commit run --all-files
- Line Length: 88 characters (Black/Ruff default)
- Indentation: 4 spaces
- Quotes: Double quotes for strings
- Imports: Sorted and grouped (via Ruff/isort)
- Docstrings: Google style convention
All new code should include type hints:
def calculate_total(items: list[float], tax_rate: float = 0.1) -> float:
"""Calculate total with tax.
Args:
items: List of item prices.
tax_rate: Tax rate to apply.
Returns:
Total amount including tax.
"""
subtotal = sum(items)
return subtotal * (1 + tax_rate)
- Minimum 80% code coverage
- Tests organized in
tests/
mirroringsrc/
structure - Use pytest fixtures for reusable test data
- Descriptive test names:
test_<function>_<scenario>_<expected_outcome>
-
Create a feature branch:
git checkout -b feature/your-feature-name
-
Make your changes in
src/
-
Write/update tests in
tests/
-
Run quality checks:
poetry run ruff check . poetry run ruff format . poetry run mypy src poetry run pytest --cov
-
Commit your changes:
git add . git commit -m "feat: add your feature description"
-
Push and create a pull request
Follow conventional commits:
feat:
New featurefix:
Bug fixdocs:
Documentation changesstyle:
Code style changes (formatting)refactor:
Code refactoringtest:
Test changeschore:
Build process or auxiliary tool changes
poetry build
This creates distribution files in the dist/
directory.
# Configure PyPI token
poetry config pypi-token.pypi your-token
# Publish
poetry publish
# Configure private repository
poetry config repositories.private https://your-repo-url
poetry config http-basic.private username password
# Publish
poetry publish -r private
# Add production dependency
poetry add package-name
# Add development dependency
poetry add --group dev package-name
# Add specific version
poetry add package-name@^2.0.0
# Update all dependencies
poetry update
# Update specific package
poetry update package-name
# Activate virtual environment
poetry shell
# Run command in virtual environment
poetry run python script.py
# Show environment info
poetry env info
- Poetry Documentation
- Ruff Documentation
- Pytest Documentation
- MyPy Documentation
- PEP 8 Style Guide
- Type Hints (PEP 484)
When contributing to projects using this template:
- Follow the established project structure
- Ensure all tests pass
- Maintain or improve code coverage
- Follow the coding standards
- Update documentation as needed
This template is available for all DatumBrain projects. For specific project licenses, check the individual repository.
Maintained by: Engineering Team / Datum Brain
Last updated: 2025-08-28 16:30:05