Skip to main content

Simple Dependency Injector for python

Project description

A simple async-friendly dependency injection framework for Python 3.

Supports:
  • Async methods

  • Parametrized dependencies

  • Type annotations

Simple Example

import diana

class MyThing(object):
    def __init__(self, prefix):
        self.prefix = prefix

    def get(self, suffix):
        return self.prefix + suffix

class MyModule(diana.Module):
    @diana.provider
    def provide_thing(self) -> MyThing:
        return MyThing("a_prefix_")


@diana.injector
def requires_a_thing(*, thing: MyThing):
    return thing.get('suffix')

if __name__ == '__main__':
    diana.injector.load_module(MyModule())

    requires_a_thing()  # returns "a_prefix_suffix"

Parametrized Dependencies

import diana
import typing

Snake = typing.NewType('Snake', str)

class SnakeModule(diana.Module):
    @diana.provider
    def provide_snake(self, length: int) -> Snake:
        return Snake('-' + ('=' * length) + e)


@diana.inject(a_snake=Snake)
@diana.param('a_snake', length=5)
def snake_printer(a_snake):
    print(a_snake, "Hissss")

if __name__ == '__main__':
    diana.injector.load_module(SnakeModule())

    snake_printer() # Prints: -=====e Hissss

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

diana-3.0.0b1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file diana-3.0.0b1-py3-none-any.whl.

File metadata

File hashes

Hashes for diana-3.0.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 0b7945a63a53e20a7232e6ba9ce5242c97f30a06d61ca89faea90363107dce17
MD5 9753f32c8878a9038c42774ba476bdeb
BLAKE2b-256 f63943394f08f5be5caa7ea87997fb69faaf781decb1903b218f04c1591b7620

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