Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
py-dependency-injection
Advanced tools
A dependency injection library for Python.
The library is compatible with the following Python versions:
$ pip install py-dependency-injection
Here's a quick example to get you started:
from dependency_injection.container import DependencyContainer
# Define an abstract Connection
class Connection:
pass
# Define a specific implementation of the Connection
class PostgresConnection(Connection):
def connect(self):
print("Connecting to PostgreSQL database...")
# Define a repository that depends on some type of Connection
class UserRepository:
def __init__(self, connection: Connection):
self._connection = connection
def fetch_users(self):
self._connection.connect()
print("Fetching users from the database...")
# Get an instance of the (default) DependencyContainer
container = DependencyContainer.get_instance()
# Register the specific connection type as a singleton instance
container.register_singleton(Connection, PostgresConnection)
# Register UserRepository as a transient (new instance every time)
container.register_transient(UserRepository)
# Resolve an instance of UserRepository, automatically injecting the required Connection
user_repository = container.resolve(UserRepository)
# Use the resolved user_repository to perform an operation
user_repository.find_all()
For more advanced usage and examples, please visit our readthedocs page.
py-dependency-injection
is released under the GPL 3 license. See LICENSE for more details.
You can find the source code for py-dependency-injection
on GitHub.
Tagged
, AnyTagged
, and AllTagged
classes. This allows for seamless injection of dependencies that have been registered with specific tags, enhancing flexibility and control in managing your application's dependencies.RegistrationSerializer
class for for serializing and deserializing them. These additions provide a more flexible way to interact with the container's registrations.@inject
decorator can only be used on static class methods and class methods. It can't be used on instance methods anymore.FAQs
A dependency injection library for Python.
We found that py-dependency-injection 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.