Giter Site home page Giter Site logo

python-di's Introduction

di - Python Dependency Injection

What?

di.DependencyInjector is a python class with which you can register dependencies and later wrap/decorate functions and methods. python-di's home is on https://github.com/buhl/python-di

Why?

Dependency Injection was the new black some months/years ago. So it's time I got around to understand the concepts. At the momment, I am deep into Python and loving it, so I used it to play around with DI.

Personally, I haven't used this module yet. But I can see a couple of places it might be useful. For now I will let it mature a bit - perhabs with help from you?

Features

  • Wraps function/method and resolves dependencies when called
  • Looks for dependencies in the di object and function/methods globals() if activated

Quick start

Install:

$ pip install https://github.com/buhl/python-di/archive/master.zip

Import class and initialize:

from di import DependencyInjector
di = DependencyInjector()

Register dependencies:

di.register_dependency('sum_list_of_ints', sum)

Wrap function and call it:

@di
def calc_sum(list_of_ints, sum_list_of_ints):
    return sum_list_of_ints(list_of_ints)

assert calc_sum([1,2,3,4]) == 10  # True

Search in globals:

@di(globals_lookup=True)
def calc_sum2(list_of_ints, sum_list_of_ints):
    return sum_list_of_ints(list_of_ints)

list_of_ints = [1,2,3,4]

assert calc_sum2() == 10  # True

Uninstall (why would you??):

$ pip uninstall python-di

Contributing

Contributions are more than welcome. I would also love to know how you are using this module.

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.