Skip to content

A simple python dependency injector

License

Notifications You must be signed in to change notification settings

ZharkovMihail/giveme

 
 

Repository files navigation

Build Status

Giveme: dependency injection for python

GiveMe is a simple, no-nonsense dependency injection framework for python.

Its features include:

  • Simple register and inject decorators to register dependency factories or classes and inject their return value into other function/method arguments.
  • Painfree configuration for singleton and thread local dependencies
  • Injected dependencies can always be overridden by manually passed arguments (great for testing)

Documentation

Examples and API documentation can be found on ReadTheDocs: https://giveme.readthedocs.io

Quick start

from giveme import Injector

injector = Injector()

@injector.register
def magic_number():
    return 42

@injector.inject
def multiply(n, magic_number):
    return n*magic_number

multiply(2)
84

GiveMe has many more advanced options, for more examples and full API documentation please visit https://giveme.readthedocs.io

Install

pip install giveme

Python3.5 and up are supported.

Changes in version 1.0

GiveMe has received some improvements in 1.0:

  • New Injector class with register and inject decorators as instance methods. To support more than one dependency registry in a project.
  • Module level decoraters giveme.register and giveme.inject have been deprecated, Injector.register and Injector.inject should be used instead.
  • Vastly improved argument binding in Injector.inject which acts in accordance to default python argument binding. Better distinction between injected arguments and manually passed arguments.
  • DependencyNotFoundError thrown from inject when an explicitly mapped (e.g. argname='dependencyname') dependency is not registered or passed in manually for easier debugging
  • DependencyNotFoundWarning raised in ambigious cases where an argument is not explicitly mapped to dependency and not passed in manually.

Migrating from <1.0

The API is mostly the same. If you were using the module levels decorators in a standard way before:

from giveme import register, inject

@register
def something():
    ...

@inject
def do_stuff(something):
    ...

The only change you'll have to make is to create an instance of Injector and use its instance method decorators instead:

from giveme import Injector

injector = Injector()

@injector.register
def something():
    ...

@injector.inject
def do_stuff(something):
    ...

Testing

You can run the included test suite with pytest

  1. Clone this repository
  2. cd path/to/giveme
  3. Install pytest -> pip install pytest
  4. Run the tests -> pytest tests.py

Contributing

Pull requests are welcome. Please post any bug reports, questions and suggestions to the issue tracker https://github.com/steinitzu/giveme/issues

About

A simple python dependency injector

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%