A simple dependency injection library for Python
Project description
gladi
Overview
Import the injector. This is the only import needed.
from gladi import inject
The library provides 4 decorators:
@inject.singleton: creates a singleton from a callable that instantiates the class in its return type@inject.scoped: same as singleton, but the callable gets called once per request@inject.transient: same as singleton, but it gets called on every request@inject.resolve: signals to the library that dependencies in parameters need to be resolved- using
@inject.singleton,@inject.scopedor@inject.transientimplies this
- using
Simple Usage
Assuming we have a class to inject,
class Database: ...
create a function (or callable) with no user-provided arguments that returns a Database instance.
def get_database() -> Database:
return Database(
host=...,
port=...,
)
Mark the callable with a decorator. In this case, a singleton would be appropriate.
@inject.singleton
def get_database() -> Database: ...
Database class is now an injectable (as a singleton).
async retrieve_user(
user_id: int,
database = inject(Database),
): ...
Lastly, signal that the retrieve_user function needs its dependencies to be resolved with the @inject.resolve decorator.
@inject.resolve
async retrieve_user(
user_id: int,
database = inject(Database),
): ...
await retrieve_user(33)
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 gladi-1.0.0.tar.gz.
File metadata
- Download URL: gladi-1.0.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b59ea298c61a04b3094910d6c117d3c9ac27ca947257c673877e77ca23949321
|
|
| MD5 |
b21acbe464f964618d273be2e62d7721
|
|
| BLAKE2b-256 |
718d75a5ff9744cce9618d7a1a76897a2208875d47961f8b0ae6bf09f7c42311
|
File details
Details for the file gladi-1.0.0-py3-none-any.whl.
File metadata
- Download URL: gladi-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
719e47a87178250d40cfaf857748c8225620f490e6d2eb6c654c68457823a1e2
|
|
| MD5 |
b850d79bde091d5a2a05e0c872cf3da7
|
|
| BLAKE2b-256 |
fbc2c48e97efb45e4c93e38365e2e9ad9c51c309a59bd7d0393cd4f538c2836b
|