Skip to main content

Small, annotation-driven dependency injection for Python.

Project description

Small DI

The smallest dependency injection mechanism possible in python. SmallDI provides you with intuitive and simple interface for doing dependency injections in your project.

Example usage

import random

from smalldi import Injector
from smalldi.annotation import Provide

# Lets create some service
@Injector.singleton
class MeowService:
    _MEOWS = ["Meow", "Meow-meow", "Meowwwwww", "Mrromeowww", "Meeeeoooow"]
    def __init__(self):
        print("Meow-meow! Meowing service is initialized")
    
    def meow(self):
        print(random.choice(self._MEOWS))

# Now lets make purring service.
# But cats do not purr without telling meow!(at least in this test)
# So its time to inject dependency
@Injector.singleton
class PurrService:
    _PURRS = ["Purrrrr", "Purr-purr"]
    
    @Injector.inject
    def __init__(self, meow_service: Provide[MeowService]):
        self.meow_service = meow_service
        print("Purr-purr! Purring service is initialized")

    def purr(self):
        self.meow_service.meow()
        print(random.choice(self._PURRS))

# Now lets put it all together. 
# Ask our services to meow and then purr
@Injector.inject
def main(meow_service: Provide[MeowService], purr_service: Provide[PurrService]):
    meow_service.meow()
    purr_service.purr()

if __name__ == '__main__':
    main()

Library logic

Injector

Injector is a static class(i.e. one that should never be instantiated) storing all singletons and responsible for injecting those singletons to a functions.

Provide

Provide[T] is an annotation for injector telling it that instead of this argument instance of T should be passed. Caller of function with Provide[T] may explicitly override argument annotated with Provide[T] by directly passing annotated_di_arg=my_value.

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

smalldi-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

smalldi-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file smalldi-0.1.0.tar.gz.

File metadata

  • Download URL: smalldi-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for smalldi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 be42a62d254b5a48f3a732ff5a18aeecdc41dfe8e1b2db1ed638023432161f6d
MD5 1d961bb977bbaf39e1d6329e4ea1816e
BLAKE2b-256 39714116c09cfef10fe3b05ca3a64f8522f18ea2177ae026486d35b370c77bcd

See more details on using hashes here.

Provenance

The following attestation bundles were made for smalldi-0.1.0.tar.gz:

Publisher: publish.yaml on 0xf104a/smalldi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file smalldi-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: smalldi-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for smalldi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b92acfebab8f43223ad2d08f7b12b7dfed1b6d62210ac8c8c70920b55c12cf25
MD5 f2c7aced32eb1365a678baa1fac47070
BLAKE2b-256 559e2d9112db8e83059d4ea7fa19cfdb6b95900b3cc3cea465b7f45f10f4693f

See more details on using hashes here.

Provenance

The following attestation bundles were made for smalldi-0.1.0-py3-none-any.whl:

Publisher: publish.yaml on 0xf104a/smalldi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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