ποΈ Python Persistence API: A JPA (Java Persistence API) and Spring Data inspired abstraction layer.
This framework unifies database access (SQL & NoSQL) via a robust implementation of the Repository Pattern and the Entity Manager. The current implementation focuses on Apache TinkerPop (Gremlin) for Graph Databases.
- Architecture & Concepts
- Installation
- Configuration Guide
- Usage
- Running the Demos (Paris Metro)
- Project Structure
- Tests
This project ports solid concepts from the Java ecosystem to Python:
- Domain Entities (
@Entity): Uses Pydantic to define strongly typed data models with runtime validation. - Entity Manager (
EntityManager): An agnostic interface managing the object lifecycle (persist, find, remove) and the underlying driver connection. - Repositories (
Repository): A business abstraction layer hiding query complexity (Gremlin, SQL) behind simple methods (save,find_by_id,find_fastest_path).
classDiagram
class BaseEntity {
+ID id
+datetime created_at
+datetime updated_at
}
class Station {
+str name
+int zone
}
BaseEntity <|-- Station
class EntityManager {
<<Interface>>
+persist(entity)
+find_by_property(key, value)
+create_relationship(from, to, rel)
}
class GremlinEntityManager {
+DriverRemoteConnection connection
}
EntityManager <|-- GremlinEntityManager
class MetroRepository {
+save_station(station)
+find_fastest_path(start, end)
}
MetroRepository --> EntityManager : uses
##π InstallationThis project uses uv for lightning-fast dependency management.
- Clone the repository:
git clone [https://github.com/your-user/soltania-python-persistence-api.git](https://github.com/your-user/soltania-python-persistence-api.git)
cd soltania-python-persistence-api
- Install dependencies and environment:
uv sync
This command automatically creates the .venv folder and installs everything needed.
##βοΈ Configuration GuideThis project uses a hierarchical configuration system inspired by Spring Boot. Variables are defined in src/soltania_persistence/config.py.
###π Available Variables| Variable | Description | Default Value |
| --- | --- | --- |
| GREMLIN_HOST | IP Address of the Tinkerpop/Gremlin server | localhost |
| GREMLIN_PORT | Server Port | 8182 |
| GREMLIN_PROTOCOL | ws (WebSocket) or wss (Secure) | ws |
###π Source Priority1. CLI Arguments (e.g. --gremlin_host=10.0.0.1)
2. Environment Variables (export GREMLIN_HOST=...)
3. Internal .env File (Project root)
4. Default Values (Code)
##π Running the Demos (Paris Metro)The project includes a complete example modeling the Paris Metro Network located in src/soltania_persistence/examples/metro_network.
###1. Initialize the DatabaseYou must first clean and populate the database with the provided line data (JSON).
# Clear database (Drop)
uv run src/soltania_persistence/examples/metro_network/main.py drop
# Import data (Load)
uv run src/soltania_persistence/examples/metro_network/main.py load
###2. Calculate an ItineraryRun the pathfinding algorithm between any two stations.
Example 1: A simple trip
uv run src/soltania_persistence/examples/metro_network/main.py "Mairie des Lilas" "ChΓ’telet"
Example 2: A complex trip (with transfer)
uv run src/soltania_persistence/examples/metro_network/main.py "Mairie des Lilas" "Chelles - Gournay"
###πΈ Real-world OutputHere is an actual execution trace. Notice how the engine intelligently detects transfers:
π FASTEST ROUTE (43 min 30 sec)
==================================================
π START : Mairie des Lilas
β¬οΈ TAKE π METRO 11
βͺοΈ Porte des Lilas
βͺοΈ TΓ©lΓ©graphe
βͺοΈ Place des FΓͺtes
βͺοΈ Jourdain
βͺοΈ PyrΓ©nΓ©es
βͺοΈ Belleville
βͺοΈ Goncourt
βͺοΈ RΓ©publique
βͺοΈ Arts et MΓ©tiers
βͺοΈ Rambuteau
βͺοΈ HΓ΄tel de Ville
βͺοΈ ChΓ’telet
π TRANSFER : Take π RER A
βͺοΈ Gare de Lyon
βͺοΈ Nation
βͺοΈ Vincennes
βͺοΈ Val de Fontenay
βͺοΈ Neuilly-Plaisance
βͺοΈ Bry-sur-Marne
βͺοΈ Noisy-le-Grand - Mont d'Est
βͺοΈ Noisy - Champs
π TRANSFER : Take π METRO 16
βͺοΈ Chelles - Gournay
==================================================
π ARRIVAL : Chelles - Gournay
##π Project StructureThe project follows a modular "Domain-Driven" structure.
soltania-python-persistence-api/
βββ pyproject.toml # Dependencies
βββ .env # Local config
βββ src/
β βββ soltania_persistence/
β βββ config.py # βοΈ Configuration Engine
β βββ core/ # π§± Framework Core (Entities, Interfaces)
β βββ provider/ # π Drivers (Tinkerpop/Gremlin)
β βββ examples/
β βββ metro_network/ # π Domain Example: Transport
β βββ data/ # JSON Data (lines.json)
β βββ models/ # Nodes (Station) & Edges (Connection)
β βββ repositories/# Gremlin Logic (Pathfinding)
β βββ services/ # ETL/Importer Logic
β βββ main.py # Entry Point (CLI)
βββ tests/ # π§ͺ Unit & Integration Tests
##π» Usage (Code Snippet)Here is how you would use the framework in your own code:
from soltania_persistence.provider.tinkerpop.manager import GremlinEntityManager
from soltania_persistence.config import settings
from my_app.repositories import UserRepository
# 1. Initialize Manager
em = GremlinEntityManager(settings.gremlin_url)
repo = UserRepository(em)
# 2. Persist Data
user = User(username="admin", email="[email protected]")
repo.save(user)
# 3. Query Data
found_user = repo.find_by_username("admin")##π§ͺ Tests```bash
uv run pytest -m "not integration"
uv run pytest