Inject

Fast Python dependency injection
Download

Inject Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • Ivan Korobkov
  • Publisher web site:
  • http://code.google.com/u/Ivan.Korobkov/

Inject Tags


Inject Description

Fast Python dependency injection Inject is a fast Python dependency injection library. It uses decorators and descriptors to reference external dependencies, and scopes (Guice-inspired) to specify how to reuse objects. Dependencies can be referenced by types and optional annotations. No configuration is required, but advanced in-code configuration is possible.Most other python dependency injection tools, such as PyContainer or Spring Python, are ports from other languages (Java). So they are based on dependency injection ways specific for statically typed languages, described by Martin Fowler.Python is not Java. Patterns and programming techniques, which seem proper and usable in one language, can be awkward in another. Inject has been created to provide a _pythonic_ way of dependency injection, utilizing specific Python functionality. Terminology used in inject has been intentionally made similar to Guice, however the internal architecture is different.Example:import inject@inject.appscopeclass Config(object): passclass A(object): passclass B(object): passclass C(object): config = inject.attr('config', Config) a = inject.attr('a', A) @inject.param('b', B): def __init__(self, b): self.b = bc = C() Here are some key features of "Inject": · Fast, only 2-3 times slower than direct instantiation. · Normal way of instantiating objects, Class(*args, **kwargs). · Injecting arguments into functions and methods. · Referencing dependencies by types and optional annotations. · Binding to callables, instances and unbound methods (see Invokers). · Request scope middleware for WSGI and Django applications (requires Python2.5+). · No configuration required at all. Advanced flexible configuration possible: · injector.bind(Class, to=Class2) · injector.bind(Database, annotation='user', to=UsersDatabase, scope=appscope) · injector.bind('app_started_at', to=datetime.now()) · injector.bind('some_var', to=Class.unbound_method) Two injection methods, a descriptor and a decorator: class My(object): · attr = inject.attr('attr', Class2) @inject.param('param', Class2): def myfunc(param): · pass Support for inheritance by passing inject.super as the default kwarg value: class My(object): · @inject.param('param1', Class1) def __init__(self, param1): · self.param1 = param1 class My2(My): · @inject.param('param2', Class2) def __init__(self, param2, param1=inject.super): · super(My2, self).__init__(param1=param1) · self.param2 = param2 Invokers to call unbound methods (cool for listeners): class My(object): def get_data(self): · pass · Create an invoker, which calls an unbound method. · invoker = inject.invoker(My.get_data) · data = invoker() · Bind directly to an unbound method. · @inject.param('data', My.get_data) def func(data): · pass · Partial injections, when only some arguments are injected. · @inject.param('logger', Logger) def mylog(msg, logger): · pass · mylog('My message') · Scopes: application (singleton), request, noscope. class Controller(object): · session = inject.attr('session', Session, scope=reqscope) · or in configuration · injector.bind(Session, to=Session, scope=reqscope) · or set the default scope · @reqscope class Session(object): · pass · @appscope class DatabasePool(object): · pass · Easy integration into existing projects. Requirements: · Python


Inject Related Software