A Spring Boot-inspired Dependency Injection and Configuration Framework for Python.
Project description
Patronum: A Dependency Injection and Configuration Framework for Python
Patronum is a modern, robust, and lightweight Python library designed to provide a Dependency Injection (DI) / Inversion of Control (IoC) container and a flexible configuration framework.
Spring-like experience
Patronum is an independent project, partially inspired by the conventions and ease-of-use found in Spring Boot®. If you're a Python developer who appreciates Spring's way of managing components and configurations, Patronum is built for you.
Spring®, Spring Framework®, and Spring Boot® are trademarks of Broadcom Inc. and/or its subsidiaries. Patronum is not affiliated with, endorsed, or sponsored by Broadcom.
License
Patronum is licensed under the MIT License.
Quick-Start
Installation
pip install patronum
Basic usage
# my_app/loggers.py
import abc
from patronum import Service, Qualifier
class Logger(abc.ABC):
@abc.abstractmethod
def log(self, message: str):
pass
@Service
class ApplicationLogger(Logger):
def log(self, message: str):
print(f"[APPLICATION LOGGER]: {message}")
# my_app/main.py
from patronum import PatronumApplication, Annotated
from .loggers import Logger
def log_message(logger: Logger):
logger.log("Hello world!")
if __name__ == "__main__":
PatronumApplication.run(log_message)
Key Features
Patronum aims to simplify your application structure and development process with the following core features:
1. Intuitive Dependency Injection:
PatronumApplication.run(callable): The heart of the DI container. Pass a callable, and Patronum will automatically find, instantiate, and inject its dependencies.- Component Scanning:
@Component&@Service: Decorators to mark classes as managed beans. Optionally accepts a bean name.@Qualifier("name"): Decorate components or bean methods to assign one or more unique names, allowing for precise dependency resolution.
- Programmatic Bean Definition:
@Configuration: Mark classes that define beans programmatically.@Bean: Decorate methods within@Configurationclasses. The return value of these methods are registered as beans.@Qualifiercan also be used here.
- Disambiguation & Control:
@Primary: Mark a bean as the default candidate when multiple implementations of the same type exist.- Strict Dependency Resolution: Throws an error if multiple candidates are found for a dependency and none are marked
@Primary.
- Conditional Bean Creation:
@ConditionalOnClass(name, matchIfMissing=False): Bean is created only if the specified class (by fully qualified name string) is present (or missing, ifmatchIfMissing=True).@ConditionalOnProperty(propertyName, havingValue=None, matchIfMissing=False): Bean is created based on the presence or value of a configuration property.
- Modern Python Support:
- Works seamlessly with plain Python classes, Pydantic models, and dataclasses.
- Utilizes
typing.Annotatedfor specifying injection criteria (e.g.,Annotated[MyType, "bean_name"]).
2. Powerful Configuration Framework:
@ActiveProfiles(["dev", "prod"]): Decorator to easily set active configuration profiles.- Flexible YAML Configuration:
- Loads and merges configurations from
application.yamlfiles. - Supports profile-specific variants (e.g.,
application-dev.yaml,application-prod.yaml) which override base configurations. - Discovers configurations in the root module/directory and sub-modules/nested directories.
- Merging Strategy: For YAML files, root-level configurations are loaded last and override properties from nested directories if conflicts occur. Profile-specific files override their non-profiled counterparts. OS environment variables always take precedence over YAML files.
- Loads and merges configurations from
- Typed Configuration Properties with
@ConfigurationProperties:@ConfigurationProperties(prefix: str): Decorate a class (plain Python, Pydantic model, or dataclass) to bind properties from the merged configuration store directly to its fields.- The prefix argument specifies the namespace in the configuration (e.g., myapp.database).
- Field names in the class are mapped to property keys (e.g.,
user_name: strwithinmyapp.database-prefixed class, maps tomyapp.database.user_name).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file patronum-0.0.0.dev1.tar.gz.
File metadata
- Download URL: patronum-0.0.0.dev1.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
142e9f99d6849e72db43dbec97f37955efc83e316b9b25750f3a0aa65bd5a510
|
|
| MD5 |
8e2b7cc28df11aa1ad805fd3230b6948
|
|
| BLAKE2b-256 |
cf89946d8fbc5d874fa4f960656ccf55887d58f2539f664b235b60a869f88859
|
File details
Details for the file patronum-0.0.0.dev1-py3-none-any.whl.
File metadata
- Download URL: patronum-0.0.0.dev1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
172802b3cc558f1e59561eccdc87bd60289c2944015155699cbcce007fbabedc
|
|
| MD5 |
3dc3cd6d46f2fa7a0574ea7163116ac6
|
|
| BLAKE2b-256 |
095c34c0bc6804445b2e9f068cf370f988ba4f1377e0f79f6838d86438dda196
|