telefonicaid/di-py

Name: di-py

Owner: Telefónica I+D

Description: Dependency injection in python #di #python

Created: 2015-03-22 13:09:22.0

Updated: 2018-04-23 11:53:12.0

Pushed: 2018-03-05 01:28:20.0

Homepage: null

Size: 67

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

di-py

Dependency injection library for python.

Version Downloads Build Status

Background

Dependency injection enables us to write better code, regardless of doing so manually or with the help of some library. It helps specially to keep our code inside the Law of Demeter, by programming against a known interface without having to worry about how a concrete implementation is composed.

Python is a highly dynamic language with an “open class” implementation for user types, thus the need for a full blown dependency injection framework is not specially needed. For medium to large applications though there is still the issue of how to actually implement dependency injection in the code using only Python's standard syntax/library.

This library is designed to be very lightweight and flexible as to allow its use in a variety of scenarios, including their use to aid with unit testing. It doesn't form a framework but just a set of utilities to keep the dependency injection needs in a project under control by applying it only where it makes sense, with minimum overhead and a lean learning curve.

Scenarios

Basic example
 http.client import HTTPSConnection
 di import injector

eate the decorator setting up the dependencies (at configuration time)
ct = injector({
TPSConnection: HTTPSConnection('localhost', '8080')


ply the decorator to our app logic to inject what we have configured (at runtime)
ect
fetch_it(id, http=HTTPSConnection):
tp.request("GET","/?id={0}".format(id))
turn http.getresponse()

ll the logic without worrying about dependencies :)
t fetch_it(100).status

erride the dependency if we have some specific use case
t fetch_it(100, http=HTTPSConnect('google.com', '80')).status
Advanced usage with DependencyMap
rt hashlib
 di import injector, Key, DependencyMap

tup the dependency map
 DependencyMap()

fine a custom Key to map a dependency when it's not a class
Dep = Key('hash')

ild a dependency programatically but only the first time it's used
singleton(HashDep)
hash(deps):
turn lambda x: hashlib.md5(x).hexdigest()

eate the decorator and bind it to the dependency map
ct = injector(dm)

fine our logic defining what should be injected by default
ect
hasher(subject, hash=HashDep)
turn hash(subject)

t hasher('foobarbaz')
Explore the unit tests

Python interpreters supported

Those are the python interpreters being validated via travis.ci upon every change in the repository.

Install

Get the latest stable version
install di-py
Get the edge version directly from github
install git+ssh://git@github.com/telefonicaid/di-py.git@master
Using RPM

Generate RPM from source code

clone git@github.com/telefonicaid/di-py.git
i-py
on setup.py bdist_rpm

License

See LICENSE

Contributing

Use the GitHub's pull request and issue tracker to provide patches or report problems with the library. All new functionality must be covered by unit tests before it can be included in the repository.

The master branch always has the cutting edge version of the code, if you are using it in your project it would be wise to create a fork of the repository or target a specific tag/commit for your dependencies.

Credits


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.