plugable dependency injection and result processing
Project description
in-n-out
Python dependency injection you can taste.
A lightweight dependency injection and result processing framework for Python using type hints. Emphasis is on simplicity, ease of use, and minimal impact on source code.
import in_n_out as ino
class Thing:
def __init__(self, name: str):
self.name = name
# use ino.inject to create a version of the function
# that will retrieve the required dependencies at call time
@ino.inject
def func(thing: Thing):
return thing.name
def give_me_a_thing() -> Thing:
return Thing("Thing")
# register a provider of Thing
ino.register_provider(give_me_a_thing)
print(func()) # prints "Thing"
def give_me_another_thing() -> Thing:
return Thing("Another Thing")
with ino.register_provider(give_me_another_thing, weight=10):
print(func()) # prints "Another Thing"
This also supports processing return values as well (injection of intentional side effects):
@ino.inject_processors
def func2(thing: Thing) -> str:
return thing.name
def greet_name(name: str) -> str:
print(f"Hello, {name}!")
ino.register_processor(greet_name)
func2(Thing('Bob')) # prints "Hello, Bob!"
Alternatives
Lots of other python DI frameworks exist, here are a few alternatives to consider:
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
in-n-out-0.1.2.tar.gz
(30.9 kB
view details)
Built Distribution
in_n_out-0.1.2-py3-none-any.whl
(18.6 kB
view details)
File details
Details for the file in-n-out-0.1.2.tar.gz
.
File metadata
- Download URL: in-n-out-0.1.2.tar.gz
- Upload date:
- Size: 30.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dce3c46af2f76a9d61a1d0185d285c89c7a1fc78641f9303f3a9c9b9718ae120 |
|
MD5 | c6826d684710440d4843a50fb63e820d |
|
BLAKE2b-256 | 21f0b63a3807c63b8b2777d0fc3abecb10d52ec383eaa1e4f74e31a6571f6f10 |
File details
Details for the file in_n_out-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: in_n_out-0.1.2-py3-none-any.whl
- Upload date:
- Size: 18.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3e1f10d769167aa7b7a760497288b6e74fc94f6c17dff710c740c72679adb51 |
|
MD5 | df920a983848e6e5ffeb3e4c7dd0d7a9 |
|
BLAKE2b-256 | 4fd4960678c153eee62542f8c8006bd3a06d86a2ee87f18fd74e6b7517d5d78b |