Skip to main content

python dependency injection library, BETA

Reason this release was yanked:

BUG FUNC

Project description

Injector API

The Injector API is a lightweight dependency injection library designed to simplify the management of dependencies in Python projects.

Prerequisites

  • Python 3.7 or higher
  • A proper environment (virtualenv or conda recommended)

Installation

To install the Injector API, simply run the following pip command:


pip install injector-api

Usage

Configuring injectorConfig.py

After installation, you must configure the library to suit your project. Create a injectorConfig.py at the root of your project. This file should define the following variable:


MODULE_APPLICATION = "your_directory_name_here"

Replace your_directory_name_here with the name of the directory where your module.py resides.

Defining and Registering Dependencies in module.py

The module.py is a key file in the Injector API library structure. Here, you define and register the dependencies you wish to inject into your application.

Dependency Registration

To register dependencies, you first need to import the container from the library, then use the register method. This method takes the interface you wish to implement and its concrete implementation as arguments.


from injector_api.dependency import container

# Your interfaces and classes here

container.register(IExampleService, ExampleServiceImpl1, override=True)
container.register(IExampleService, ExampleServiceImpl2, override=True)
container.register(InterfaceWithoutOtherService, ImplInterface,override=True)

Injecting into Functions and Methods

Once your dependencies are registered in module.py, you can use the @inject decorator to inject these dependencies into functions or methods.


from injector_api.cargaDinamica import inject

@inject({IExampleService: 0})
def some_function(service: IExampleService):
    return service.do_something()

@inject()
def some_function_two(service: InterfaceWithoutOtherService):
    return service.do_something()

In the above example, the IExampleService dependency will be injected into the some_function, allowing you to use its methods and properties.

Location of module.py

Ensure that the module.py file is located in the directory you've specified in injectorConfig.py. This file will be automatically read by the library upon execution to load and manage dependencies.

Security Recommendations

While the Injector API simplifies dependency management, there are potential security risks when using dynamic loading. We recommend the following precautions:

  • Always validate dynamically loaded information or modules.
  • Avoid loading modules from locations that can be manipulated by untrusted users.
  • Use a secure, isolated environment for your application.
  • Keep the library and all its dependencies up to date.

Contribute

Contributions are welcome! Please submit pull requests or open issues on our GitHub repository.

License

This library is licensed under the MIT License. See the LICENSE file for details.

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

injector_api-0.0.9.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

injector_api-0.0.9-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file injector_api-0.0.9.tar.gz.

File metadata

  • Download URL: injector_api-0.0.9.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for injector_api-0.0.9.tar.gz
Algorithm Hash digest
SHA256 11917f0d19790a8d25c9c03ea837ad942714c4712a7151f934030c0d6e258fc0
MD5 f3ebbdab9639101d1c091895e4bd5246
BLAKE2b-256 9a0ed3c3064b99f48156a2081e25085039d053f56c165bef5c01215c6a0752cc

See more details on using hashes here.

File details

Details for the file injector_api-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: injector_api-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for injector_api-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 761e87a09d1be029144f03fc32ea3265889610903d2d82e5290a3426679210b3
MD5 4f9d594bc7f9f21ba29a17de1e30c1fe
BLAKE2b-256 aa1c9977d698623fd918f24aea12446332548d6d380faa5fcf66655619529c92

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