Skip to main content

Simple Dependency Injection for python

Project description

CNS Dependency Injection is a very simple way to store and inject services into your software.

It looks like this for Flask (dumb example, but you should get the point):

from di.helpers import get_service, add_service
from di.decorator import service, inject
from flask import Flask

app = Flask(__name__)
add_service('router', app.route)

@service('adder')
def adder(*args):
  sum = 0
  for arg in args:
    sum += arg
  return sum

router = get_service('router')

@router('/adds')
def adds():
  sum = get_service('adder')([1,2,3])
  return "hello " + repr(sum)

get_service('app').run()

If you are going to @inject into a class, do it on the __init__ function. @service can be used with classes or functions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

CNSdi-0.1.4.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file CNSdi-0.1.4.tar.gz.

File metadata

  • Download URL: CNSdi-0.1.4.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for CNSdi-0.1.4.tar.gz
Algorithm Hash digest
SHA256 7c1aef95afbe5b1d5a861597b39f11daa786e90277ae8d1fc6f79ead8ec16c89
MD5 2c8d0d036a5cfca0344448f639317d5f
BLAKE2b-256 b339440f446b7ae18ae53939464f9f6790259568918abf974c1acab1a977effe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page