Another Python Dependency injector framework.
Project description
Python Injection Framework (PIF)
A simple Python dependency injection framework.
Usage
You can install this project from pypi.
pip install python-injection-framework
Dependency Injection
The injection framework is configured to inject any default values for method arguments that are instances
of providers.Provider.
This implementation works by wrapping decorators around methods any patching any unfilled providers.Provider default
arguments at runtime.
All dependency injection is lazily evaluated so providers are only evaluated when a method is called. This approach is optimal as it reduces necessary computation for expensive services and reduces
Decorator Injection
With this approach you can automatically inject functions at load time using the @wiring.inject decorator.
from scottzach1.pif import providers
from scottzach1.pif import wiring
@wiring.inject # <- automatically injects providers.Provider default arguments!
def my_function(a: str = providers.ExistingSingleton("hello world")):
return a
if __name__ == "__main__":
assert "hello world" == my_function()
Module Injection
With this approach you can wire all methods in the specified modules.
from scottzach1.pif import providers
from scottzach1.pif import wiring
def my_function(a: str = providers.ExistingSingleton("hello world")):
return a
if __name__ == "__main__":
wiring.wire([__name__]) # <- dynamically inject methods with providers.Provider default arguments!
assert "hello world" == my_function()
Overriding
This package provides a simple mechanism to override providers. This can be very useful when it comes to mocking services for testing or dynamically patching application behavior based on application configuration.
Standard Overriding
If you want to patch a value all you need to do is call .override() on the provider in question. If you are wanting to
override an existing singleton you may call the convenience method .override_existing().
from scottzach1.pif import providers
from scottzach1.pif import wiring
StringProvider = providers.ExistingSingleton("hello world")
@wiring.inject
def my_function(a: str = StringProvider):
return a
if __name__ == "__main__":
assert "hello world" == my_function()
override = StringProvider.override_existing("overridden_1")
assert "overridden_1"
Context Managers
If you want more control around the override lifecycles then you may use the Override context manager.
from scottzach1.pif import providers
from scottzach1.pif import wiring
StringProvider = providers.ExistingSingleton("hello world")
@wiring.inject
def my_function(a: str = StringProvider):
return a
if __name__ == "__main__":
assert "hello world" == my_function()
OverrideProvider = providers.ExistingSingleton("overridden_1")
with StringProvider.override(OverrideProvider):
assert "overridden_1" == my_function()
with OverrideProvider.override_existing("overridden_2"):
assert "overridden_2" == my_function() # You can even stack overrides!!
assert "overridden_1" == my_function()
assert "hello world" == my_function()
Examples
If you would like to see more examples, feel free to check out examples/.
Contributing
-
Clone the repository and setup with uv 🪄
git clone git@github.com:scottzach1/Python-Injection-Framework.git cd Python-Injection-Framework uv sync --dev
-
Configure pre-commit hooks 🪝
pre-commit install -
Write your changes! 💻️
-
Run test cases 🧪
pytest
-
Submit a Pull Request ↖️
Authors
| Zac Scott (scottzach1) |
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
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 python_injection_framework-1.0.0.tar.gz.
File metadata
- Download URL: python_injection_framework-1.0.0.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a21acf707a9aa3ec21f7ec9af55b2f9b70e53bd5bfe8bc8e4f86df582c9b8f82
|
|
| MD5 |
872e2962abc0f83059ba9e5e5af89f1e
|
|
| BLAKE2b-256 |
ed5b637787e6e2753eac31a99604da0c20cc42bb704a33d243e3edf0d5bdc6d9
|
Provenance
The following attestation bundles were made for python_injection_framework-1.0.0.tar.gz:
Publisher:
pipeline.yml on scottzach1/Python-Injection-Framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_injection_framework-1.0.0.tar.gz -
Subject digest:
a21acf707a9aa3ec21f7ec9af55b2f9b70e53bd5bfe8bc8e4f86df582c9b8f82 - Sigstore transparency entry: 171434013
- Sigstore integration time:
-
Permalink:
scottzach1/Python-Injection-Framework@85cc8be01db494654c9d2e24e2f2254d75e416e0 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/scottzach1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pipeline.yml@85cc8be01db494654c9d2e24e2f2254d75e416e0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file python_injection_framework-1.0.0-py3-none-any.whl.
File metadata
- Download URL: python_injection_framework-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
833b989eb66947619c69b06f714859ee00df0fd924f2c37a5865a5b395409ae2
|
|
| MD5 |
ccb1f688d6d6f3b7ead980523cde0b5e
|
|
| BLAKE2b-256 |
c721f51482d35524a83aa2a2f476d3644e6c480451e08267b5a8c9d57eca054c
|
Provenance
The following attestation bundles were made for python_injection_framework-1.0.0-py3-none-any.whl:
Publisher:
pipeline.yml on scottzach1/Python-Injection-Framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
python_injection_framework-1.0.0-py3-none-any.whl -
Subject digest:
833b989eb66947619c69b06f714859ee00df0fd924f2c37a5865a5b395409ae2 - Sigstore transparency entry: 171434014
- Sigstore integration time:
-
Permalink:
scottzach1/Python-Injection-Framework@85cc8be01db494654c9d2e24e2f2254d75e416e0 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/scottzach1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pipeline.yml@85cc8be01db494654c9d2e24e2f2254d75e416e0 -
Trigger Event:
push
-
Statement type: