Skip to main content

A friendly dependency injection package

Project description

Depinj

Depinj is a python dependency injection library based on type annotations, it allow's you to define members of a class, and if the type of those members are in the dependency injection system it will be injected once that is called. For example

import depinj 
import random

class RandomNumber:
	rnumber = random.random()

class TestClass:
	rnumber: RandomNumber

depinj.add_scoped(RandomNumber)
depinj.add_scoped(TestClass)

test_class = depinj.get_scoped(TestClass)

The test_class variable will have an scoped RandomNumber instance, there is support for singletons to, with the function add_singleton.

inject

There is an inject decorator that inject's the dependencies into function objects, for example following the previous code:

import depinj 

@depinj.inject
def print_number(rn: RandomNumber):
	print(rn.rnumber)

print_number()

It can also be used in the init method like this:

class TestClass:
	rnumber: RandomNumber

	@pydep.inject
	def __init__(self, rnumber: RandomNumber):
		self.rnumber = rnumber
	
test_class = TestClass()

Depinj object

from depinj import Injector 

dep = Injector()

dep.add_scoped(SomeClass)

This Injector object represent's the dependency injection system, in fact, the functions defined at top-level module are using a global instance of the Depinj class. This way you can have multiple dependency injection systems if you want to have different classes for each one.

If you are going to use a different Injector object than the global one, then you have pass that object into the inject function with the injector named parameter.

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

depinj-0.1.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

depinj-0.1.1-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file depinj-0.1.1.tar.gz.

File metadata

  • Download URL: depinj-0.1.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

File hashes

Hashes for depinj-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e665d7dc393cb5d23065e5a5333cbf7099aa52ea907a9eda1b2795044bf5c668
MD5 79bd121ee19bf1afe98a4f6235826271
BLAKE2b-256 7039b5f837968e32adaa490174fdd86f18ca7801968b3a1344bbe9e42870cf82

See more details on using hashes here.

File details

Details for the file depinj-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: depinj-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

File hashes

Hashes for depinj-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c6af106e9b1b115d21420fe2648c607fbb8be6f4f8e95365634f7c13b10b45dc
MD5 cb32e7b1bf1a55c1cba18c425d4c61c3
BLAKE2b-256 96708d11dce503bad156c8c8af2d9ecd6e06f96193610c132004d85097333c96

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