Skip to main content

Dependency injection package

Project description

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

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

lunnaris-pyinject-1.0.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

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

lunnaris_pyinject-1.0.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file lunnaris-pyinject-1.0.0.tar.gz.

File metadata

  • Download URL: lunnaris-pyinject-1.0.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for lunnaris-pyinject-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e3dbb75a0b28b1a8488e8bcf02dcd7cac553707683c41fa28e4cbbcd7b563d03
MD5 c3689879bcb44d08bdd09b89887d513d
BLAKE2b-256 f71e395d5e09487821daf88ffb925387507419545843ec94f09479fd18078de0

See more details on using hashes here.

File details

Details for the file lunnaris_pyinject-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lunnaris_pyinject-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b28b898a38a191cd9e366f91c8466f5426675b9ae5e874d4e97172ac6b7605a4
MD5 8c470fc22bc2fffa5f9010fbf75b4f45
BLAKE2b-256 19ebc9cdf967489226cf2e5d87fa08d2ee34316711ce475690994bd862723747

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