Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
service_collection
is a lightweight Python package that mimics ASP.NET's dependency injection system. It also incorporates an Angular-style Inject
method to resolve dependencies, making it easier to manage and inject dependencies in your applications.
It currently supports transient and singleton services. Scoped services will be added in a future release if there is a demand for them.
Install the package using pip:
pip install service-collection
from abc import ABC, abstractmethod
from service_collection import inject
# Define an abstract base class for the service interface
class IFooService(ABC):
@abstractmethod
def do_something(self):
pass
# Implement the interface
class FooService(IFooService):
def do_something(self):
return "FooService did something!"
# Define another service that depends on the interface
class IBarService(ABC):
@abstractmethod
def do_something(self):
pass
class BarService(IBarService):
def __init__(self):
self.foo_service = inject(IFooService)
def do_something(self):
return f"BarService did something with {self.foo_service.do_something()}"
from service_collection import ServiceCollection
services = ServiceCollection()
services.add_transient(IFooService, FooService)
services.add_singleton(IBarService, BarService)
service_provider = services.build_service_provider()
Import the inject
function to retrieve service instances easily in your code:
from service_collection import inject
foo_service = inject(IFooService)
print(foo_service.do_something()) # Output: FooService did something!
bar_service = inject(IBarService)
print(bar_service.do_something()) # Output: BarService did something with FooService did something!
Here is a complete example demonstrating how to set up and use the service_collection
package with the inject
function:
from abc import ABC, abstractmethod
from service_collection import ServiceCollection, inject
# Define an abstract base class for the service interface
class IFooService(ABC):
@abstractmethod
def do_something(self):
pass
# Implement the interface
class FooService(IFooService):
def do_something(self):
return "FooService did something!"
# Define another service that depends on the interface
class IBarService(ABC):
@abstractmethod
def do_something(self):
pass
class BarService(IBarService):
def __init__(self):
self.foo_service = inject(IFooService)
def do_something(self):
return f"BarService did something with {self.foo_service.do_something()}"
# Register services
services = ServiceCollection()
services.add_transient(IFooService, FooService)
services.add_singleton(IBarService, BarService)
# Build the service provider
service_provider = services.build_service_provider()
# Resolve and use services
foo_service = inject(IFooService)
print(foo_service.do_something()) # Output: FooService did something!
bar_service = inject(IBarService)
print(bar_service.do_something()) # Output: BarService did something with FooService did something!
This project is licensed under the MIT License. See the LICENSE file for details.
This README provides an overview of how to install, set up, and use the service_collection
package with example code snippets, demonstrating the use of abstract base classes for service interfaces and including the creation and usage of the inject
function.
FAQs
ASP.NET Core-style dependency injection service collection for Python
We found that service-collection 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.