| 1 |
Metadata-Version: 2.3 |
| 2 |
Name: in-n-out |
| 3 |
Version: 0.2.1 |
| 4 |
Summary: plugable dependency injection and result processing |
| 5 |
Project-URL: homepage, https://github.com/pyapp-kit/in-n-out |
| 6 |
Project-URL: repository, https://github.com/pyapp-kit/in-n-out |
| 7 |
Project-URL: documentations, https://ino.rtfd.io |
| 8 |
Author-email: Talley Lambert <[email protected]> |
| 9 |
License: BSD 3-Clause License |
| 10 |
License-File: LICENSE |
| 11 |
Classifier: Development Status :: 3 - Alpha |
| 12 |
Classifier: License :: OSI Approved :: BSD License |
| 13 |
Classifier: Programming Language :: Python :: 3 |
| 14 |
Classifier: Programming Language :: Python :: 3.8 |
| 15 |
Classifier: Programming Language :: Python :: 3.9 |
| 16 |
Classifier: Programming Language :: Python :: 3.10 |
| 17 |
Classifier: Programming Language :: Python :: 3.11 |
| 18 |
Classifier: Programming Language :: Python :: 3.12 |
| 19 |
Classifier: Typing :: Typed |
| 20 |
Requires-Python: >=3.8 |
| 21 |
Provides-Extra: dev |
| 22 |
Requires-Dist: ipython; extra == 'dev' |
| 23 |
Requires-Dist: mypy; extra == 'dev' |
| 24 |
Requires-Dist: pdbpp; extra == 'dev' |
| 25 |
Requires-Dist: pre-commit; extra == 'dev' |
| 26 |
Requires-Dist: pytest-codspeed; extra == 'dev' |
| 27 |
Requires-Dist: pytest-cov; extra == 'dev' |
| 28 |
Requires-Dist: pytest>=6.0; extra == 'dev' |
| 29 |
Requires-Dist: rich; extra == 'dev' |
| 30 |
Requires-Dist: ruff; extra == 'dev' |
| 31 |
Requires-Dist: toolz; extra == 'dev' |
| 32 |
Provides-Extra: docs |
| 33 |
Requires-Dist: mkdocs-gen-files; extra == 'docs' |
| 34 |
Requires-Dist: mkdocs-literate-nav; extra == 'docs' |
| 35 |
Requires-Dist: mkdocs-material==9.4.1; extra == 'docs' |
| 36 |
Requires-Dist: mkdocs==1.5.3; extra == 'docs' |
| 37 |
Requires-Dist: mkdocstrings-python==1.7.3; extra == 'docs' |
| 38 |
Provides-Extra: test |
| 39 |
Requires-Dist: pytest-codspeed; extra == 'test' |
| 40 |
Requires-Dist: pytest-cov; extra == 'test' |
| 41 |
Requires-Dist: pytest>=6.0; extra == 'test' |
| 42 |
Requires-Dist: toolz; extra == 'test' |
| 43 |
Description-Content-Type: text/markdown |
| 44 |
|
| 45 |
# in-n-out |
| 46 |
|
| 47 |
[](https://github.com/pyapp-kit/in-n-out/raw/main/LICENSE) |
| 48 |
[](https://pypi.org/project/in-n-out) |
| 49 |
[](https://python.org) |
| 50 |
[](https://github.com/pyapp-kit/in-n-out/actions/workflows/ci.yml) |
| 51 |
[](https://app.codecov.io/gh/pyapp-kit/in-n-out) |
| 52 |
[](https://codspeed.io/pyapp-kit/in-n-out) |
| 53 |
|
| 54 |
Python dependency injection you can taste. |
| 55 |
|
| 56 |
A lightweight dependency injection and result processing framework |
| 57 |
for Python using type hints. Emphasis is on simplicity, ease of use, |
| 58 |
and minimal impact on source code. |
| 59 |
|
| 60 |
```python |
| 61 |
import in_n_out as ino |
| 62 |
|
| 63 |
|
| 64 |
class Thing: |
| 65 |
def __init__(self, name: str): |
| 66 |
self.name = name |
| 67 |
|
| 68 |
|
| 69 |
# use ino.inject to create a version of the function |
| 70 |
# that will retrieve the required dependencies at call time |
| 71 |
@ino.inject |
| 72 |
def func(thing: Thing): |
| 73 |
return thing.name |
| 74 |
|
| 75 |
|
| 76 |
def give_me_a_thing() -> Thing: |
| 77 |
return Thing("Thing") |
| 78 |
|
| 79 |
|
| 80 |
# register a provider of Thing |
| 81 |
ino.register_provider(give_me_a_thing) |
| 82 |
print(func()) # prints "Thing" |
| 83 |
|
| 84 |
|
| 85 |
def give_me_another_thing() -> Thing: |
| 86 |
return Thing("Another Thing") |
| 87 |
|
| 88 |
|
| 89 |
with ino.register_provider(give_me_another_thing, weight=10): |
| 90 |
print(func()) # prints "Another Thing" |
| 91 |
``` |
| 92 |
|
| 93 |
This also supports processing *return* values as well |
| 94 |
(injection of intentional side effects): |
| 95 |
|
| 96 |
```python |
| 97 |
|
| 98 |
@ino.inject_processors |
| 99 |
def func2(thing: Thing) -> str: |
| 100 |
return thing.name |
| 101 |
|
| 102 |
def greet_name(name: str): |
| 103 |
print(f"Hello, {name}!") |
| 104 |
|
| 105 |
ino.register_processor(greet_name) |
| 106 |
|
| 107 |
func2(Thing('Bob')) # prints "Hello, Bob!" |
| 108 |
``` |
| 109 |
|
| 110 |
### Alternatives |
| 111 |
|
| 112 |
Lots of other python DI frameworks exist, here are a few alternatives to consider: |
| 113 |
|
| 114 |
- <https://github.com/ets-labs/python-dependency-injector> |
| 115 |
- <https://github.com/google/pinject> |
| 116 |
- <https://github.com/ivankorobkov/python-inject> |
| 117 |
- <https://github.com/alecthomas/injector> |
| 118 |
- <https://github.com/Finistere/antidote> |
| 119 |
- <https://github.com/dry-python/returns> |
| 120 |
- <https://github.com/adriangb/di> |