Skip to main content

Dependency injection for python

Project description

Gluer

Dependency Injection for Python

Installation

Gluer uses type annotations, so python 3.6+ is required

python3 -m pip install --user gluer

Usage

Let's follow a wonderful explanation of DI:

from abc import ABC, abstractmethod
from gluer import Gluer

class Drink(ABC): # this doesn't *have* to be abstract
	@abstractmethod
	def sip(self):
		pass


class Child:
	def __init__(self, drink: Drink):
		self.drink = drink

	def take_a_sip(self):
		self.drink.sip()
		print("ahhh")


class Juice(Drink):
	def sip(self):
		print("*gulp*")


class Coke(Drink):
	def sip(self):
		print("*fizzle*")



if __name__ == "__main__":
	gluer = Gluer()
	mom_watches = True

	gluer.register(Child)
	gluer.register(Juice if mom_watches else Coke).As(Drink)

	container = gluer.build()

	kid = container.resolve(Child)
	kid.take_a_sip() # *gulp* ahhh

Project details


Download files

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

Files for gluer, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size gluer-0.2.0.tar.gz (3.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page