Skip to main content

plugable dependency injection and result processing

Project description

in-n-out

License PyPI Python Version CI codecov Benchmarks

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


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.2.0.tar.gz (26.0 kB view details)

Uploaded Source

Built Distribution

in_n_out-0.2.0-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file in_n_out-0.2.0.tar.gz.

File metadata

  • Download URL: in_n_out-0.2.0.tar.gz
  • Upload date:
  • Size: 26.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for in_n_out-0.2.0.tar.gz
Algorithm Hash digest
SHA256 84a25bdbf0ca9760ddfab82776e8cd25675dae21ca0e956e20ac0e7302fca19f
MD5 949d5b98e5d3d0342bb05d26b779d829
BLAKE2b-256 bcbb83fac31dd7812528e3038bd324d08991c64ea3d502e8dba0932359167a04

See more details on using hashes here.

File details

Details for the file in_n_out-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: in_n_out-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for in_n_out-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0a09570a94be958712904786a69e200faa4169af692938ebb71813d3a960ddb0
MD5 87b44988237fb732f96c50e0b384d32d
BLAKE2b-256 9c69d20403145d559410f475172348b48ca1c5f2da9947cf78eab391a8e78316

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page