
Security News
NIST Under Federal Audit for NVD Processing Backlog and Delays
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
This repository contains experiments and examples for managing dependencies using dependency injection with class decorators in Python projects. The structures and patterns demonstrated here are flexible and can be adapted to suit various project needs.
The goal of this project is to showcase different approaches to dependency management, focusing on modularity, flexibility, and ease of use. While the provided examples are specific, the underlying concepts can be applied to a wide range of scenarios.
The project is built around four components that implement different aspects of dependency management:
from dependency.core import Module, module
from plugin........component import SomeService, SomeServiceComponent
@module(
declaration=[ # Declare all components that are part of the module
SomeServiceComponent,
... # Every used component must be declared in some module
],
bootstrap=[ # Bootstrap components will be started on loading
SomeServiceComponent
]
)
class SomeModule(Module):
"""This is a module class.
Here the providers are declared and provided
"""
def declare_providers(self):
from plugin........provider import ImplementedSomeService
return [ # Here you declare the providers for the components
ImplementedSomeService,
... # Only one provider per component is allowed
]
from abc import ABC, abstractmethod
from dependency.core import Component, component
class SomeService(ABC):
"""This is the interface for a new component."""
@abstractmethod
def method(self, ...) -> ...:
pass
@component(
interface=SomeService, # Declares the interface used by the component
)
class SomeServiceComponent(Component):
"""This is the component class.
Provider selected for this component will injected here.
Components are only started when provided or bootstrapped.
"""
pass
from dependency_injector import providers
from dependency.core import provider
from plugin........component import SomeService, SomeServiceComponent
from plugin..other_component import OtherService, OtherServiceComponent
@provider(
component=SomeServiceComponent, # Declares the component to be provided
imports=[OtherService, ...], # List of dependencies (components) that are needed
provider=providers.Singleton # Provider type (Singleton, Factory)
)
class ImplementedSomeService(SomeService):
"""This is a provider class. Here the component is implemented.
Providers are injected into the respective components when provided.
"""
def __init__(self, cfg: dict, **kwargs) -> None:
"""Init method will be called when the provider is stared.
This will happen once for singleton and every time for factories.
"""
# Once declared, i can use the dependencies for the class.
self.dependency: OtherService = OtherServiceComponent.provide()
def method(self, ...) -> ...:
"""Methods declared in the interface must be implemented."""
do_something()
from dependency.core import Dependent, dependent
@dependent(
imports=[SomeComponent, ...], # List of dependencies (components) that are needed
)
class SomeDependent(Interface, Dependent):
"""This is the dependent class.
Dependents must be declared in the provider that provides them.
Dependents can be instantiated without the need to define new providers.
"""
def method(self, ...) -> ...:
pass
These components work together to create a powerful and flexible dependency injection system, allowing for more maintainable and testable Python applications.
This repository includes a practical example demonstrating how to use the framework. You can find this example in the example
directory. It showcases the implementation of the core components and how they interact to manage dependencies effectively in a sample application.
This project is a work in progress, and there are several improvements and enhancements planned for the future. Some of the areas that will be explored include:
This project depends on dependency-injector. This library provides a robust and flexible framework for managing dependencies in Python projects.
FAQs
A dependency management tool for Python projects.
We found that module-dependency demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.
Security News
TypeScript Native Previews offers a 10x faster Go-based compiler, now available on npm for public testing with early editor and language support.