Skip to main content

diay - a dependency injection library

Project description

Diay - a dependency injection library

Build Status Latest version on PyPI Licence

Diay is a dependency injection/inversion of control library for Python 3.5 and higher. It leverages the type hints introduced in 3.5 to allow you to easily call functions or construct classes that require specific types of objects to function.

As an extremely basic and somewhat contrived example - the value of the library doesn't really come into play until you have fairly deeply nested dependencies - let's say you want to render a jinja2 template using some data from an API.

injector = diay.Injector()
injector.set_instance(SomeConfigClass, my_config_object)

@injector.provider
def make_jinja_environment() -> jinja2.Environment:
    return jinja2.Environment()

@injector.provider
def make_api_client(config: SomeConfigClass) -> some.APIclient:
    return some.APIclient(config.api_token)

def render(j2env: jinja2.Environment, apiclient: some.APIclient):
    data = apiclient.get('/some/data')
    template = j2env.get_template('some-template.html.j2', data=data)
    return template.render()

html = injector.call(render)

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

diay-0.1.1.tar.gz (1.8 kB view details)

Uploaded Source

File details

Details for the file diay-0.1.1.tar.gz.

File metadata

  • Download URL: diay-0.1.1.tar.gz
  • Upload date:
  • Size: 1.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for diay-0.1.1.tar.gz
Algorithm Hash digest
SHA256 215f929a764da7726144e37798c5c734b0c5ea5cfd17b86d8cf96eb497c7afe1
MD5 c73e8dd31e1b5554312c4986f1d76ae5
BLAKE2b-256 75994f0a653c5b70daa6bbc4a51793c83723d9d5be22d8d17987b3bd2dc407d3

See more details on using hashes here.

Supported by

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