Skip to content

Latest commit

 

History

History
129 lines (79 loc) · 3.27 KB

README.md

File metadata and controls

129 lines (79 loc) · 3.27 KB

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.8 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