Skip to main content

Simple Dependency Injector for python

Project description

https://img.shields.io/pypi/v/diana.svg?style=for-the-badge https://img.shields.io/pypi/pyversions/diana.svg?style=for-the-badge https://img.shields.io/travis/xlevus/python-diana.svg?style=for-the-badge

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.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for diana-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7226a31e80baa4b24918c4e667742ee7c8ea46baf0c1a7cc0846999195a5a776
MD5 84ab0303d7ef68be8ec882c26d13470d
BLAKE2b-256 067e28a64ca7332f40ea26d503fdf3ed3e485752a1bcacc33025a1f0be67fcac

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