Skip to main content

Dependency injection framework using pure Python

Project description

Prion

A dependency injection framework using pure Python. No external dependencies.

Installation

pip install prion

Usage

Define a container by subclassing Syringe and declaring dependencies with single[T]() or factory[T]().

>>> from prion import Syringe, single, factory

>>> class AppSyringe(Syringe):
...     config = single[dict]()
...     session = factory[dict]()

Create an instance and register providers using decorators.

>>> syringe = AppSyringe()

>>> @syringe.config
... def create_config() -> dict:
...     return {"debug": True}

>>> @syringe.session
... def create_session() -> dict:
...     return {}

Access dependencies as attributes.

>>> syringe.config is syringe.config
True

>>> syringe.session is not syringe.session
True

Dependency Wiring

Providers can receive other dependencies as parameters. Parameter names are matched to dependency names automatically.

>>> class WiredSyringe(Syringe):
...     config = single[dict]()
...     greeting = single[str]()

>>> syringe = WiredSyringe()

>>> @syringe.config
... def create_config() -> dict:
...     return {"name": "world"}

>>> @syringe.greeting
... def create_greeting(config: dict) -> str:
...     return f"hello {config['name']}"

>>> syringe.greeting
'hello world'

Circular dependencies are detected at resolve time with a RuntimeError.

Lifecycle

Use reset() to clear all cached singletons, or use the container as a context manager.

>>> syringe = AppSyringe()

>>> @syringe.config
... def create_config() -> dict:
...     return {"debug": True}

>>> with syringe:
...     _ = syringe.config
...
>>> # singleton cache is cleared after exiting the context

Testing

Use override() to temporarily replace a provider in tests.

>>> syringe = AppSyringe()

>>> @syringe.config
... def create_config() -> dict:
...     return {"debug": True}

>>> syringe.config
{'debug': True}

>>> with syringe.override("config", lambda: {"debug": False}):
...     syringe.config
{'debug': False}

>>> syringe.config
{'debug': True}

Strategies

Strategy Behavior
single[T]() Creates once, returns cached
factory[T]() Creates a new instance each time

License

MIT

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

prion-0.2.0.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prion-0.2.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: prion-0.2.0.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","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}

File hashes

Hashes for prion-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ee2ee3a4c221df098d84093a126d936d4de86cb8d50f44ab3edfc63c56da732e
MD5 1dd4318ffdbe551d5c73c1a0ba3830d1
BLAKE2b-256 4ab794ad3ff8d6e44e8ac1acd48c44d9379fafec4ec7036f81dae4a1193d298d

See more details on using hashes here.

File details

Details for the file prion-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: prion-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","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}

File hashes

Hashes for prion-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98a7f35a3da3d9d9a5a9b7783805e680c5c751730a0e5e377b85e75c5c2785d0
MD5 b91c02c6b7415daf2620d4cf0ec680ff
BLAKE2b-256 6e2d94880670a0f616088669db4fea7b8c0c6a81c877af5b79b49fb976ae76fa

See more details on using hashes here.

Supported by

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