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.

Source Distribution

gluer-0.2.0.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file gluer-0.2.0.tar.gz.

File metadata

  • Download URL: gluer-0.2.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for gluer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6492e1bd54befac2d7f8dd453e83b314ade314b139aad30f3fb642979813d25d
MD5 416ce12220b085ed6ebb9e05e90e6be0
BLAKE2b-256 a89453db582549eac9d3b547c2da5024b664f5f8c825e4da0141c33ad0ee8e61

See more details on using hashes here.

Supported by

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