Skip to main content

An IOC Container for Python 3.6+

Project description

https://codecov.io/gh/bobthemighty/punq/branch/master/graph/badge.svg?token=52hQhaggnk Documentation Status

An unintrusive library for dependency injection in modern Python. Inspired by Funq, Punq is a dependency injection library you can understand.

  • No global state

  • No decorators

  • No weird syntax applied to arguments

  • Small and simple code base with 100% test coverage and developer-friendly comments.

Installation

Punq is available on the cheese shop.

pip install punq

Documentation is available on Read the docs.

Quick Start

Punq avoids global state, so you must explicitly create a container in the entrypoint of your application:

import punq

container = punq.Container()

Once you have a container, you can register your application’s dependencies. In the simplest case, we can register any arbitrary object with some key:

container.register("connection_string", instance="postgresql://...")

We can then request that object back from the container:

conn_str = container.resolve("connection_string")

Usually, though, we want to register some object that implements a useful service.:

class ConfigReader:
   def get_config(self):
      pass

class EnvironmentConfigReader(ConfigReader):
   def get_config(self):
      return {
         "logging": {
            "level": os.env.get("LOGGING_LEVEL", "debug")
         },
         "greeting": os.env.get("GREETING", "Hello world")
      }

container.register(ConfigReader, EnvironmentConfigReader)

Now we can resolve the ConfigReader service, and receive a concrete implementation:

config = container.resolve(ConfigReader).get_config()

If our application’s dependencies have their own dependencies, Punq will inject those, too:

class Greeter:
   def greet(self):
      pass


class ConsoleGreeter(Greeter):
   def __init__(self, config_reader: ConfigReader):
      self.config = config_reader.get_config()

   def greet(self):
      print(self.config['greeting'])


container.register(Greeter, ConsoleGreeter)
container.resolve(Greeter).greet()

If you just want to resolve an object without having any base class, that’s okay:

class Greeter:
   def __init__(self, config_reader: ConfigReader):
      self.config = config_reader.get_config()

   def greet(self):
      print(self.config['greeting'])

container.register(Greeter)
container.resolve(Greeter).greet()

And if you need to have a singleton object for some reason, we can tell punq to register a specific instance of an object:

class FileWritingGreeter:
   def __init__(self, path, greeting):
      self.path = path
      self.message = greeting
      self.file = open(self.path, 'w')

   def greet(self):
      self.file.write(self.message)


one_true_greeter = FileWritingGreeter("/tmp/greetings", "Hello world")
container.register(Greeter, instance=one_true_greeter)

You might not know all of your arguments at registration time, but you can provide them later:

container.register(Greeter, FileWritingGreeter)
greeter = container.resolve(Greeter, path="/tmp/foo", greeting="Hello world")

Conversely, you might want to provide arguments at registration time, without adding them to the container:

container.register(Greeter, FileWritingGreeter, path="/tmp/foo", greeting="Hello world")

Fuller documentation is available on Read the docs.

Github workflows, nox configuration, and linting gratefully stolen from Hypermodern Python

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

punq-0.6.2.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

punq-0.6.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file punq-0.6.2.tar.gz.

File metadata

  • Download URL: punq-0.6.2.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for punq-0.6.2.tar.gz
Algorithm Hash digest
SHA256 4d824c92ca640feefb080c1ecdd034e37f79833b3cb88dacac38ed7e048f4dca
MD5 dc72a47611909a9741d3b4f84c55163b
BLAKE2b-256 1baad4e06056e7ceb5cd91de02b3fc7b3af83c5dc1cdd853f57ee139c43794ad

See more details on using hashes here.

File details

Details for the file punq-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: punq-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for punq-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8d365bdc01038897dfcc0c9bf63bc8910a95afc46e5e427c2a7abbda7144502b
MD5 de373bc97c29c9ed6257ec20e2dab7a9
BLAKE2b-256 76f9f87e431e7b4a92f042e97ea22ecfaa5069ba0a9039f2ab67b3e1bf877c27

See more details on using hashes here.

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