Lazy dependency injection.
Project description
Introduction
Main focus is an easy way to create lazy universally injectable dependencies; in less magical way. It also leans more on the side of making it easier to get the dependency you need anywhere in the codebase.
py-xinject allows you to easily inject lazily created universal dependencies into whatever code that needs them, in an easy to understand and self-documenting way.
Documentation
📄 Detailed Documentation | 🐍 PyPi
Install
# via pip
pip install xinject
# via poetry
poetry add xinject
Quick Start
# This is the "my_resources.py" file/module.
import boto3
from xinject import DependencyPerThread
class S3(DependencyPerThread):
def __init__(self, **kwargs):
# Keeping this simple; a more complex version
# may store the `kwargs` and lazily create the s3 resource
# only when it's asked for (via a `@property or some such).
self.resource = boto3.resource('s3', **kwargs)
To use this resource in codebase, you can do this:
# This is the "my_functions.py" file/module
from .my_resources import S3
def download_file(file_name, dest_path):
# Get dependency
s3_resource = S3.grab().resource
s3_resource.Bucket('my-bucket').download_file(file_name, dest_path)
Inject a different version of the resource:
from .my_resources import S3
from .my_functions import download_file
us_west_s3_resource = S3(region_name='us-west-2')
def get_s3_file_from_us_west(file, dest_path):
# Can use Dependencies as a context-manager,
# inject `use_west_s3_resource` inside `with`:
with us_west_s3_resource:
download_file(file, dest_path)
# Can also use Dependencies as a function decorator,
# inject `use_west_s3_resource` whenever this method is called.
@us_west_s3_resource
def get_s3_file_from_us_west(file, dest_path):
download_file(file, dest_path)
Licensing
This library is licensed under the MIT-0 License. See the LICENSE file.
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
Built Distribution
File details
Details for the file xinject-1.2.0.tar.gz
.
File metadata
- Download URL: xinject-1.2.0.tar.gz
- Upload date:
- Size: 33.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.15.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b982cfd84b740c27cd0a9989ae1639f6d5cc2b9307e720bd1202f91dac61c012 |
|
MD5 | 6ce66e11c67077cd274a36fb59b923ef |
|
BLAKE2b-256 | 1765d6f3b482f0db52559cdbf6100a9c0f4e4aaf24ea6813ae1b3fe5d14e8744 |
File details
Details for the file xinject-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: xinject-1.2.0-py3-none-any.whl
- Upload date:
- Size: 34.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.15.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff46db0660de0de98e8d636579c2f8bfc6435275310b48e2128b321952ed4e5b |
|
MD5 | 09b10386fe9c76aa9aa5cc9b6db82170 |
|
BLAKE2b-256 | 18b12ace7ed9f04064e4b4b6822cd5b7afdf8b7cbfce059eabbe3dd81eb8b212 |