python-ppa
python-ppa is a lightweight, Spring Data ppa-inspired persistence framework for MongoDB and PostgreSQL built on top of Python 3.13+, Pydantic v2, PyMongo, and psycopg.
It uses advanced Python metaprogramming (metaclass) to dynamically generate MongoDB queries at runtime based on method naming conventions or explicit query declarations, drastically reducing boilerplate code.
🚀 Features
- ppa-Like Repository Pattern: Declare an interface, and let the metaclass handle query assembly dynamically.
- Query Naming Conventions: Instantly supports
find_by_*,find_all_by_*,exists_by_*, andcount_by_*pattern resolutions. - Custom Query Annotations: Bind complex MongoDB or PostgreSQL query templates with custom parameter injections using the
@querydecorator. - Data Validation: Fully powered by Pydantic v2 for robust runtime type checking and parsing.
- Zero-Boilerplate ID Mapping: Automatically handles conversions between stringified hexadecimal keys and native BSON
ObjectIdfootprints. - Environment Profiles: Flexible configurations handling multi-stage deployment environments (
dev,prod, etc.) via YAML interpolation.
📁 Directory Structure
.
├── README.md
├── ppa
│ ├── __init__.py
│ ├── config.py # App bootstrapping, YAML parsing & environment mapping
│ ├── mongo
│ ├── __init__.py
│ └── interface.py # Repository core interface, custom metadata, and queries
│ └── postgres
│ ├── __init__.py
│ └── interface.py # PostgreSQL repository interface and SQL query conventions
├── pyproject.toml
└── requirements.txt
🛠️ Getting Started
Prerequisites
- Python 3.13 or higher
Installation & Virtual Env Setup
- Clone the repository and navigate to its root:
cd python-ppa - Spin up a virtual environment and update your pip core dependencies:
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
- Install the project library dependencies:
pip install -r requirements.txt
⚙️ Configuration Management
The framework supports multi-profile YAML configurations with real-time environment substitution syntax (e.g., ${ENV_VAR:default_value}).
Create a resources/ directory at the root of your execution workspace and include your application sheets.
- Master Configuration (
resources/settings.yml)
app:
profile: ${APP_PROFILE:dev} # Swaps profile to target environment configuration
- Environment Profile Configuration (
resources/settings-dev.yml)
mongodb:
uri: ${MONGO_URI:mongodb://localhost:27017}
database: ${MONGO_DB_NAME:ppa_database}
# Or configure PostgreSQL instead.
postgresql:
dsn: ${POSTGRES_DSN:postgresql://postgres:postgres@localhost:5432/ppa_database}
# Equivalent field-based PostgreSQL configuration:
# postgresql:
# host: ${POSTGRES_HOST:localhost}
# port: ${POSTGRES_PORT:5432}
# database: ${POSTGRES_DB_NAME:ppa_database}
# user: ${POSTGRES_USER:postgres}
# password: ${POSTGRES_PASSWORD:postgres}
framework:
logging:
enabled: true
level: "DEBUG"
📖 Usage Example
Here is a quick overview of how you can configure a domain entity and generate auto-implemented interface pipelines:
- Define your Document Model
from pydantic import Field
from ppa.mongo.interface import DocumentModel, document
@document(name="users")
class User(DocumentModel):
id: str = Field(alias="_id", default=None)
username: str
email: str
age: int
- Declare your Interface Repository
By extending IRepository[T], method naming patterns are captured and converted into database interactions seamlessly.
from ppa.mongo.interface import IRepository, query
from typing import List, Optional
class UserRepository(IRepository[User]):
# 1. Query generation by structural method naming convention
def find_by_username(self, username: str) -> Optional[User]: ...
def count_by_age(self, age: int) -> int: ...
# 2. Templated declaration using placeholder substitutions
@query(definition={"email": "?0", "age": {"$gte": "?1"}})
def find_by_email_and_min_age(self, email: str, min_age: int) -> List[User]: ...
- Execute CRUD Actions
from ppa.config import close_db_connection
# Initialize repository instance
user_repo = UserRepository()
# Save a document
new_user = User(username="pratyush", email="pratyush@example.com", age=25)
user_id = user_repo.save(new_user)
# Fetch using automatic query generation
user = user_repo.find_by_username("pratyush")
print(f"Found User: {user.email if user else 'Not Found'}")
# Clean up connections on process termination
close_db_connection()
PostgreSQL repositories follow the same naming conventions with SQL-backed models:
from pydantic import EmailStr
from ppa.postgres.interface import EntityModel, IRepository, query, table
@table(name="users")
class User(EntityModel):
username: str
email: EmailStr
age: int
class UserRepository(IRepository[User]):
def find_by_username(self, username: str) -> User | None: ...
def count_by_age(self, age: int) -> int: ...
@query("SELECT * FROM users WHERE email = ?0 AND age >= ?1")
def find_by_email_and_min_age(self, email: str, min_age: int) -> list[User]: ...
📜 License
This project is open-source software licensed under the MIT License.
Metadata
Release files for python-ppa 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| python_ppa-0.1.7.tar.gz | 15.6 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_ppa-0.1.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.8 kB
Release files / python_ppa-0.1.7.tar.gz
| Download URL | python_ppa-0.1.7.tar.gz |
|---|---|
| Size | 15.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
27a48ebb3522bacde2e264cb3e86e8ab1f98aff36fe20d7d0ac4d5cd82b05656
|
|
BLAKE2b-256 checksum How to use checksums |
660ea026501000e7f344f70d3d703ed25f2a1c133e3b9fa3779c70b86c51839c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 26, 2026.
Transparency logRelease files / python_ppa-0.1.7-py3-none-any.whl
| Download URL | python_ppa-0.1.7-py3-none-any.whl |
|---|---|
| Size | 16.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a10f917d2146a776dd34cf89d3907e5b4b1c573e4af9099cc185bd1ee2fe64cd
|
|
BLAKE2b-256 checksum How to use checksums |
5061ec86758f2aa841ea81832b0c648700866a256f1caa1e217536a5094172bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 26, 2026.
Transparency logSecurity for python-ppa 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for python_ppa-0.1.7.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 26, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.