Skip to main content

Nanoinject is a terrifically small and simple dependency injection container

Project description

Nanoinject

Nanoinject is a terrifically small and simple dependency injection container for Python 3.6+. Its' key features:

  • Easy enough to use in five minutes.
  • Configurable in code or config files.
  • Pretty feature complete.

Installation

pip install nanoinject

Use guide

Dependency injection is a way to achieve Inversion of Control.

Use in code

class A:
    value = 42

class B:
    def __init__(self, a):
        self.a = a


c = Container()
c.add('a', lambda c: A())
c.add('b', lambda c: B(c.get('a')))

assert 42 == c.get('b').a.value

Using configuration

It starts with declaring your services in YAML format:

values:
  scalar_value: 42

services:
  a:
    class: A
    module: example.a
    dependencies:
      - value:scalar_value

  b:
    class: B
    module: example.b
    dependencies:
      - a

And then letting the config object configure the container based on that YAML file:

import os
from nanoinject import Container, Config

class A:
    def __init__(self, value):
        self.value = value

class B:
    def __init__(self, a):
        self.a = a


c = Container()
config = Config.from_yaml_file(os.path.dirname(__file__) + '/services.yaml')
config.config(c)

assert 42 == c.get('b').a.value

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

nanoinject-1.0.0.tar.gz (3.3 kB view details)

Uploaded Source

File details

Details for the file nanoinject-1.0.0.tar.gz.

File metadata

  • Download URL: nanoinject-1.0.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for nanoinject-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b275d2c8c751593733aa4b5fa18c33e4b994c576c73fcddb732303f987e129f6
MD5 f456adc578b6f47ceb6c3193c60bd23c
BLAKE2b-256 e80da6c3ce2963f690f4845a6ec85ba80a93c81bf553a40757aedd045263d767

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