The smallest dependency injection mechanism possible in python. SmallDI provides you with intuitive and simple interface for doing dependency injections in your project.
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()Injector is a static class(i.e., one that should never be instantiated) which is the main (and currently the only) DI container inside the library. Injector provides two decorators:
@Injector.singletoncreates an instance of a class which may further be injected in functions@Injector.injectreplaces parameters annotated with typeProvide[Singleton]with actual instances of Singleton
Singletons are classes having a single instance. In smalldi singletons may not take constructor(__init__) other
than annotated with Provide[] type. Only singletons may be decorated with @Injector.singleton. As a consequence,
only singleton classes may be injected at the current state of library development.
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.