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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for diana-3.0.0b2-py3-none-any.whl
Algorithm Hash digest
SHA256 ac45ec02c2a1db12f254a563a07abde8a9d91e6a6c18ef81fdd155ff6a6a2b2a
MD5 096f728e1581ad96b9763adf306f365d
BLAKE2b-256 140958423422120d10ae7b0f1b9ffa11d434c7174131e6d842600f3cf7a66007

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