https://github.com/msoedov/broccoli
A dependency injection package
https://github.com/msoedov/broccoli
Last synced: 9 months ago
JSON representation
A dependency injection package
- Host: GitHub
- URL: https://github.com/msoedov/broccoli
- Owner: msoedov
- License: mit
- Created: 2015-08-31T12:22:48.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2019-10-22T23:48:08.000Z (over 6 years ago)
- Last Synced: 2025-09-22T11:18:29.113Z (9 months ago)
- Language: Python
- Homepage:
- Size: 47.9 KB
- Stars: 8
- Watchers: 3
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README

------------------------------
[]()
[]()
[](https://travis-ci.org/msoedov/broccoli)
[](https://codeclimate.com/github/msoedov/broccoli)
![License][license-img]
[license-img]: http://img.shields.io/badge/license-MIT-brightgreen.svg
[license]: http://opensource.org/licenses/MIT
Broccoli is a simple dependency injection package based on type annotations
Installation
-----------
```shell
pip install broccoli
```
Features
--------
- **Simple** - less than 100 lines of code without external dependencies
- **Error check** - Function signature check on start up - less errors during refactoring
- **Fast** - Designed to have zero runtime overhead, all dependencies injected either on module load or application start up.
- **Powerful** - Auto-discovery of dependencies by package and by module
- **Convenient** - Hackable and elegant programmatic API. Really easy to start using it.
Python 2 support?
-----------------
Oups, sorry
Basic Usage
-----------
Inject dependency to function with type annotations
```python
from broccoli import bind
class Dependency:
pass
def foo(a, bar:Dependency):
print(a, bar)
bind(foo, Dependency())
foo(1) # prints 1 <__main__.Dependency object at 0x11111b7b8>
```
Inject dependency to module/package
```python
# module foo.py
def foo(a, b:DependencyA):
print(a, b)
def bar(a, b:DependencyB):
print(a, b)
# main.py
from broccoli import inject
from package import foo
inject(foo, DependencyA(), DependencyB())
# or
inject('package.foo', DependencyA(), DependencyB())
```
Example with decorator wich inject deps on demand
```python
from brocolli.fixtures.types import *
from broccoli import Dependency
def dependecies():
return Db(), Service(), Cache()
default_dependencies = Dependency(dependecies)
@default_dependencies
def a(db: Db):
return db.query('User').all()
```
Inject deps on application entry point
```python
...
default_dependencies = Dependency()
@default_dependencies
def a(db: Db):
return db.query('User').all()
if __name__ == '__main__':
default_dependencies << dependecies
# or even
default_dependencies << dependecies()
```
Test examples
--------
[examples](https://github.com/msoedov/broccoli/tree/master/tests/fixtures)
This looks as dirty hack why should use it?
---------------------------------------------
No reason, you can keep using module level variables and singleton objects. But if you know a good example of unit tests for such code don't hesitate to share it.
Getting Help
------------
For **feature requests** and **bug reports** [submit an issue
](https://github.com/msoedov/brocolli/issues) to the GitHub issue tracker for
Broccoli.