Skip to main content

plugable dependency injection and result processing

Project description

in-n-out

License PyPI Python Version CI codecov

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


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

Uploaded Source

Built Distribution

in_n_out-0.1.4-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file in-n-out-0.1.4.tar.gz.

File metadata

  • Download URL: in-n-out-0.1.4.tar.gz
  • Upload date:
  • Size: 33.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for in-n-out-0.1.4.tar.gz
Algorithm Hash digest
SHA256 b75e50947188598fd98f4460ea4b85643420bb69b94f335f2dd66f3beb98c1b2
MD5 e77b29af735d5ffbb17bce07912bed61
BLAKE2b-256 6d47fea2cfb8800ba84677577399abc18c04132f783e2f3502a9f4af5ae44993

See more details on using hashes here.

File details

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

File metadata

  • Download URL: in_n_out-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for in_n_out-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d641d215faa216a5d927549c99b4208d4fd7f4c273edc28a6f81c89f97208727
MD5 b014ac882318c61ae54f6526c075b5c4
BLAKE2b-256 c09a44f8f0813a31e5dc4084b61e3a8b544431f2eacb1c59b68bfa77907e803a

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