Skip to main content

Easy dependency injection in Python.

Project description

#FastInject: easy Python dependency injection

coverage Tests version dependencies PyPI Downloads versions
tweet xfollow

FastInject provides easy dependency injection for Python that makes you code decoupled, testable, uncomplicated and more readable. Decorate your services with the @injectable decorator and decorate your function with @inject. Done! Your function will now be injected with instances of the required service.

pip install fastinject

Table of Contents


Main Features

  • 🐍 Pure Python
  • 🤸 Flexible
  • 🎩 Tailor-made for your app
  • 👨‍🎨 Easy to use with decorators

How to

Injecting services

Inject services that depend on one another

Use the service registy imperatively to get and set dependencies on the fly

Use multiple registries?


Usage Example

Below details a

Step 1: Declare service to be injectable

We have a service that we want to inject, so we mark it injectable with a decorator:

import time, datetime
from fastinject import injectable

@injectable()           # <-- Just add this decorator to declare the TimeStamp service to be injectable
class TimeStamp:
    ts: float

    def __init__(self) -> None:
        self.ts = time.time()

    @property
    def datetime_str(self) -> str:
        return datetime.datetime.fromtimestamp(self.ts).strftime("%Y-%m-%d %H:%M:%S")

Step 2: Use the service in a function that is injected in

from fastinject import inject

@inject()               # <-- This decorator will inject required services in this function
def function_with_injection(ts: TimeStamp):
    print(f"In the injected function, the current time is {ts.datetime_str}.")

if __name__ == "__main__":
    function_with_injection()

Installation

pip install fastinject

The source code is currently hosted on GitHub at: https://github.com/mike-huls/fastinject

Binary installers for the latest released version are available at the Python Package Index (PyPI).

Dependencies

FastInject has one major dependency: injector. FastInject aims to build on injector by making it easier to use.

License

MIT

Documentation

🔨 Under construction

Development

Find the changelog and list of upcoming features here.
Contributions are always welcome; feel free to submit bug reports, bug fixes, feature requests, documentation improvements or enhancements!


Go to Top

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

fastinject-0.0.2.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

fastinject-0.0.2-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file fastinject-0.0.2.tar.gz.

File metadata

  • Download URL: fastinject-0.0.2.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.0

File hashes

Hashes for fastinject-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d6970d8d07a96e7d3fd762f98a5a1c4f46dc17cd1ec5ce7207fbe27101157579
MD5 fa0380ae4eb1285b5e0881b0aa960dca
BLAKE2b-256 44b2d7e1c38b0e3599d63a8eec633a1ff43907561ae5985dac9e81e80961751b

See more details on using hashes here.

File details

Details for the file fastinject-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastinject-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 890d0db3fc894c06c9156c0d64d466eae920f9698d7d49f82d155a9800e5c3fc
MD5 d2b3ec016055fedcb844cd429f5506ae
BLAKE2b-256 10e86286374d4c76937d5a6d9bad0b11a26b055a101afc5470e244736559e140

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