• Stars
    star
    291
  • Rank 141,672 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created about 7 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An IoC container for Python 3.6+

Punq

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

More Repositories

1

blog-code-samples

Demos and playgrounds for articles on io.made.com
Python
45
star
2

eventsourcing-101

Slides and code for event sourcing 101 talk
Python
31
star
3

Jwt4Net

A JWT implementation in C#
C#
7
star
4

collectd-vts

A quick n dirty collectd python plugin for the nginx-vts module
Python
6
star
5

cake-shop-kata

JavaScript
5
star
6

talk-boundaries

HTML
5
star
7

a-cloud-guru

Infra as code for studying some aws certifications
TypeScript
5
star
8

nginx-uwsgi-s6

Makefile
3
star
9

cloudtrail-reader

Slack notifications for cloudtrail events
Python
2
star
10

python-ports-adapters

Python
2
star
11

spyfi

Convert regular Python objects into for tests
Python
2
star
12

speciphy

Simple context/specification style testing for PHP, inspired by mspec and contexts
PHP
2
star
13

disconsulate

A light-weight loadbalancing service discovery lib for Consul
JavaScript
2
star
14

chipper

Simple, human-friendly, structured logging for javascript
2
star
15

python-architecture-book

HTML
2
star
16

99bottles

Python
1
star
17

talk-why-so-serverless

Slides for the Why So Serverless talk
CSS
1
star
18

bobthemighty.github.io

1
star
19

advent-of-code-2019

Python
1
star
20

codefiend-site

Blog content
SCSS
1
star
21

pants-lambda-layer

Prototype pants plugin for lambda layers
Python
1
star
22

Goban

An simple library for implementing the game of go, (baduk or weiqi)
C#
1
star
23

fquirrel

An F# library for localisable rendering of jquery-tmpl server-side
F#
1
star
24

OpenRasta-Website

Source code for the openrasta homepage
1
star
25

firehose-sipper

Tiny lib for parsing Kinesis Firehose'd JSON in Python.
Python
1
star
26

OAuthWrapsta

C#
1
star
27

algorithm-refactoring-kata

Transliteration of Ode to Code's algortihm kata https://github.com/OdeToCode/Katas/tree/master/Refactoring/CS/Algorithm
TypeScript
1
star
28

talk-eventbridge-is-so-hot

CSS
1
star
29

complex-things-small-parts

1
star