Skip to main content

Injecty

A simple IOC / Dependency injection micro framework for Python.

What Problem does this try and solve?

Suppose you are tasked with implementing a library which does some task, the specification for which is...

  • Long and complicated,
  • Possibly unknown.
  • May even have contradictory methods depending on the context

For the purpose of your library, you only need to implement part of the specification, but you want to leave it open ended so other libraries / programs can extend what you have done to fit their needs.

This is what injecty is for!

Installation

pip install injecty

Example

Take the task of processing lists containing hashes representing 2D shapes and calculate the area of each:

[
  {"type": "Circle", radius: 2.0},
  {"type": "Square", length: 3},
  ...
]

You create a python library for this task - let's call it shape_area_calculator.

You may start by defining an abstract base class with a method for calculating area

Your main method for parsing the shapes may look like this

Next, you might define implementations for circle and square

With injecty, your code for a parser may look like this.

  • Get all implementations of shape
  • Find the one where the name matches the type from the input
  • Initialize the implementation

Injecty finds implementations using config modules. These are any top level module with a name matching injecty_config_* (e.g.: injecty_config_shapes.py). Each config module must have:

  • A configure method, which is used to register / deregister impls with a context.
  • A priority - this determines the order of processing, with higher numbers processed after lower ones, so they can
  • override / deregister impls if required

So in this example, the author of a program could add a dependency on this library and define their own processors for any other shape (e.g.: Rectangle) by defining their class and creating a config module for it (e.g.: injecty_config_rectangle.py)

Ordering of Implementations

Often - the ordering of implementations is important. Taking the shape example above - suppose the input data did not contain an explicit type attribute. In that case, the program would have to examine the other attributes to determine if a type is suitable. It is considered to be a circle if the input has a radius. If the input has a length, it is a square. If the input has both a length and a height, it is a rectangle.

In order to make sure that we don't confuse a rectangle for a square, we would need to make sure the rectangle check is done BEFORE the square check. Remember, the original program has no knowledge of rectangles. So we accomplish this using implementation ordering.

You can pass a sort_key into any of the injecty get_impl methods. If no explicit sort_key is given, Injecty examines the base class to see if there is an integer priority attribute, and sorts implementations in descending order. So in this case, simply by giving Rectangle a higher priority than Square, we would ensure that processing occurs correctly.

Release Procedure

status

The typical process here is:

  • Create a PR with changes. Merge these to main (The Quality workflows make sure that your PR meets the styling, linting, and code coverage standards).
  • New releases created in github are automatically uploaded to pypi

Metadata

Release files for injecty 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for injecty 0.0.9
File Size Uploaded
injecty-0.0.9.tar.gz 38.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for injecty 0.0.9
File Interpreter ABI Platform
injecty-0.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 42.5 kB

Release files / injecty-0.0.9.tar.gz

Download URL injecty-0.0.9.tar.gz
Size 38.1 kB
Tags Source
SHA-256 checksum
How to use checksums
dd556f29e3c40f47198a94954d92300641aff36671535fd389ace07fac8f6c55
BLAKE2b-256 checksum
How to use checksums
ad7ac83a6705457866ce68d4e78db5dca9f744d450bde9390ae74ae7a7cc1e20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / injecty-0.0.9-py3-none-any.whl

Download URL injecty-0.0.9-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a4d10cd368ab0af787eec49ee4ca3a29f82a7bc815a0011bd4e1fef82dadfe24
BLAKE2b-256 checksum
How to use checksums
4551691454c4ad930f5c8828aeb3e4da0700bee8e0c9567ff3b3f0e746c75f6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.0.9 This release

2 release files

0.0.7

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page