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):
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.7.tar.gz
(35.5 kB
view details)
Built Distribution
in_n_out-0.1.7-py3-none-any.whl
(19.5 kB
view details)
File details
Details for the file in-n-out-0.1.7.tar.gz
.
File metadata
- Download URL: in-n-out-0.1.7.tar.gz
- Upload date:
- Size: 35.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8380f0d1e8c1e81c5c3294591a0c4d0de032b98f77511395b08ac0c2feaf4aa6 |
|
MD5 | 107a7571a6630960ccdb74f5e11a8564 |
|
BLAKE2b-256 | 53ff0e973bd253edba867d4c56a3def9a33acf00969290fb419c5b1fc80e78f1 |
File details
Details for the file in_n_out-0.1.7-py3-none-any.whl
.
File metadata
- Download URL: in_n_out-0.1.7-py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b980b368c4ca48252b831970de18c9d37c218b69cafac1da30229fd830cb2e2 |
|
MD5 | 626cadd980f5841b2609376d27616834 |
|
BLAKE2b-256 | 4ce8704bc9554c5ae9869ccad4dda654907b2659cf040d4fa6e30b0ec2bf7e2f |