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)


	kid = gluer.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.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

gluer-0.0.1-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

Supported by

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