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

If you're not sure about the file name format, learn more about wheel file names.

diana-3.0.0b3-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for diana-3.0.0b3-py3-none-any.whl
Algorithm Hash digest
SHA256 702ae87def117ddfdfa845dd954bb815ea1ff317c3aa24db0b05270bb01f938c
MD5 4ddf04b29a60faf3c7d2c5a3cf04b8f6
BLAKE2b-256 e145f8e965659b4e227232266efaac1987add91a4b959dc255f8c939e8da49a4

See more details on using hashes here.

Supported by

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