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?
Register similar services?
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.4.tar.gz
.
File metadata
- Download URL: fastinject-0.0.4.tar.gz
- Upload date:
- Size: 14.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e041fc9304c1fd10344a7afd351d6af8cc117193c46102b75184d0d7f454ea49 |
|
MD5 | 7822f82359a9b1027fac1ebde0308fbb |
|
BLAKE2b-256 | 1fdab4a2c0e1be8694ee9cf418e69780c809f3d8352ccff064725cfb2a74ad53 |
File details
Details for the file fastinject-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: fastinject-0.0.4-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c54b25acd3407bb1dd548ee275f3ded6cbee2698e5078e0e0a483f61524d936 |
|
MD5 | 8801578ab02d401a421b623626bec7c6 |
|
BLAKE2b-256 | 3f5d300de350886b6bdcba0f9baee4efe61e65febb48f648c238f1e1762f89ca |