Skip to main content

Lunnaris-PyInject

Dependency injection library. Aims to be as simple as posible.

Usage

Using Dependency will create the object in every call.

# example.py
# import the dependency you need and the decorator 'inject'
from lunnaris_pyinject import Dependency, inject

class MyNeededDependency:
    def __init__(self, arg, other_arg):
        pass

# Supports *args and *kwargs
dependency = Dependency(MyNeededDependency,"some argument", other_arg="other arg")

@inject
def some_awesome_function(my_dependency: MyNeededDependency = dependency):
    # Do something cool with my_dependency
    pass

To use Singleton pattern

from lunnaris_pyinject import Dependency
class MyNeededDependency:
    pass
# Same code from above
# use is_singleton keyword argument
dependency = Dependency(MyNeededDependency, "one_argument", other_args=1, is_singleton=True)
# Same code

Take in count that is_singleton keyword argument is reserved and will be removed from the kwargs dictionary passed to the dependency.

You might want to use another object to hold all of your dependency creators.

# example.py
from lunnaris_pyinject import Dependency, DependantDependency, inject

class DB:
    pass


class Service:
    # No need to specify a default value, since it will be used 
    def __init__(self, db: DB):
        pass


class MyDependenciesHolder:
    # A simple dependency
    db = Dependency(DB, "connection_string", is_singleton=True)
    # Here a dependency can call another if the __init__ function is decorated with @inject
    service = DependantDependency(Service, db, is_singleton=True)

@inject
def some_func(service: Service = MyDependenciesHolder.service):
    pass

Release files for lunnaris-pyinject 1.0.0

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

Source distribution (sdist)

Source distribution for lunnaris-pyinject 1.0.0
File Size Uploaded
lunnaris-pyinject-1.0.0.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lunnaris-pyinject 1.0.0
File Interpreter ABI Platform
lunnaris_pyinject-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.8 kB

Release files / lunnaris-pyinject-1.0.0.tar.gz

Download URL lunnaris-pyinject-1.0.0.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e3dbb75a0b28b1a8488e8bcf02dcd7cac553707683c41fa28e4cbbcd7b563d03
BLAKE2b-256 checksum
How to use checksums
f71e395d5e09487821daf88ffb925387507419545843ec94f09479fd18078de0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / lunnaris_pyinject-1.0.0-py3-none-any.whl

Download URL lunnaris_pyinject-1.0.0-py3-none-any.whl
Size 3.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b28b898a38a191cd9e366f91c8466f5426675b9ae5e874d4e97172ac6b7605a4
BLAKE2b-256 checksum
How to use checksums
19ebc9cdf967489226cf2e5d87fa08d2ee34316711ce475690994bd862723747
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

1.0.0 This release

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