Easy dependency injection in Python.
Project description
#FastInject: easy Python dependency injection
FastInject provides easy dependency injection for Python that makes you code decoupled, testable, uncomplicated and more readable.
Decorate your services with the @injectable
decorator and decorate your function with @inject
. Done!
Your function will now be injected with instances of the required service.
pip install fastinject
Table of Contents
- Main Features
- Usage Example
- Installation
- Dependencies
- License
- Documentation
- Development
- Contributing to Cachr
Main Features
- 🐍 Pure Python
- 🤸 Flexible
- 🎩 Tailor-made for your app
- 👨🎨 Easy to use with decorators
How to
Injecting services
Inject services that depend on one another
- ServiceConfig: Register multiple dependencies for injection.
- ServiceConfig: Register nested dependencies for injection.
Use the service registy imperatively to get and set dependencies on the fly
- Declare service to be injectable imperatively.
- Declare service to be injectable and declare function to inject imperatively.
Use multiple registries?
Usage Example
Below details a
Step 1: Declare service to be injectable
We have a service that we want to inject, so we mark it injectable
with a decorator:
import time, datetime
from fastinject import injectable
@injectable() # <-- Just add this decorator to declare the TimeStamp service to be injectable
class TimeStamp:
ts: float
def __init__(self) -> None:
self.ts = time.time()
@property
def datetime_str(self) -> str:
return datetime.datetime.fromtimestamp(self.ts).strftime("%Y-%m-%d %H:%M:%S")
Step 2: Use the service in a function that is injected in
from fastinject import inject
@inject() # <-- This decorator will inject required services in this function
def function_with_injection(ts: TimeStamp):
print(f"In the injected function, the current time is {ts.datetime_str}.")
if __name__ == "__main__":
function_with_injection()
Installation
pip install fastinject
The source code is currently hosted on GitHub at: https://github.com/mike-huls/fastinject
Binary installers for the latest released version are available at the Python Package Index (PyPI).
Dependencies
FastInject has one major dependency: injector
. FastInject aims to build on injector
by making it easier to use.
License
Documentation
🔨 Under construction
Development
Find the changelog and list of upcoming features here.
Contributions are always welcome; feel free to submit bug reports, bug fixes, feature requests, documentation improvements or enhancements!
Project details
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
File details
Details for the file fastinject-0.0.1.tar.gz
.
File metadata
- Download URL: fastinject-0.0.1.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.4.30
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 940a3a7e8b4b07b919fbebde86cfe1c5a7c8c8848c3fa850aaba86995ab06449 |
|
MD5 | 5e51e287a9963e1d75fcd8c12e6d599a |
|
BLAKE2b-256 | ddb643ebdea521daca5116ef609122a46a2ba289955c68a53f6c8ce3e76e8527 |
File details
Details for the file fastinject-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: fastinject-0.0.1-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.4.30
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f55ce40fee704287342e6b2e36823d113e50ee1c4e00ab99b693cd59cc3c44d |
|
MD5 | 9fb6405684934b67111df378fbca6e8e |
|
BLAKE2b-256 | 11de1cbb5df69b714969a0d5da785a12fc95103318c986d4278e16beee34593b |