Code Monkey home page Code Monkey logo

di-py's Introduction

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

from http.client import HTTPSConnection
from di import injector

# Create the decorator setting up the dependencies (at configuration time)
inject = injector({
  HTTPSConnection: HTTPSConnection('localhost', '8080')  
})

# Apply the decorator to our app logic to inject what we have configured (at runtime)
@inject
def fetch_it(id, http=HTTPSConnection):
  http.request("GET","/?id={0}".format(id))
  return http.getresponse()

# Call the logic without worrying about dependencies :)
print fetch_it(100).status

# Override the dependency if we have some specific use case
print fetch_it(100, http=HTTPSConnect('google.com', '80')).status

Advanced usage with DependencyMap

import hashlib
from di import injector, Key, DependencyMap

# Setup the dependency map
dm = DependencyMap()

# Define a custom Key to map a dependency when it's not a class
HashDep = Key('hash')

# Build a dependency programatically but only the first time it's used
@dm.singleton(HashDep)
def hash(deps):
  return lambda x: hashlib.md5(x).hexdigest()

# Create the decorator and bind it to the dependency map
inject = injector(dm)

# Define our logic defining what should be injected by default
@inject
def hasher(subject, hash=HashDep)
  return hash(subject)

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

  • python 2.6
  • python 2.7
  • python 3.3
  • python 3.4
  • pypy

Install

Get the latest stable version

pip install di-py

Get the edge version directly from github

pip install git+ssh://[email protected]/telefonicaid/di-py.git@master

Using RPM

Generate RPM from source code

git clone [email protected]/telefonicaid/di-py.git
cd di-py
python 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.