![PythonSupport](https://img.shields.io/static/v1?label=python&message=%203.8|%203.9|%203.10&color=blue?style=flat-square&logo=python)
![PyPI version](https://badge.fury.io/py/xinject.svg?)
- [Introduction](#introduction)
- [Documentation](#documentation)
- [Install](#install)
- [Quick Start](#quick-start)
- [Licensing](#licensing)
# 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](https://xyngular.github.io/py-xinject/latest/)** | **[🐍 PyPi](https://pypi.org/project/xinject/)**
# Install
```bash
# via pip
pip install xinject
# via poetry
poetry add xinject
```
# Quick Start
```python
# 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:
```python
# 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:
```python
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.
Raw data
{
"_id": null,
"home_page": "https://github.com/xyngular/py-xinject",
"name": "xinject",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "dependency,injection,lazy,resource",
"author": "Josh Orr",
"author_email": "josh@orr.blue",
"download_url": "https://files.pythonhosted.org/packages/1a/bc/b9f3ef6be0a170cedcb8f2e23b424cd97180433e8246767a41ed164f78ba/xinject-1.4.1.tar.gz",
"platform": null,
"description": "![PythonSupport](https://img.shields.io/static/v1?label=python&message=%203.8|%203.9|%203.10&color=blue?style=flat-square&logo=python)\n![PyPI version](https://badge.fury.io/py/xinject.svg?)\n\n- [Introduction](#introduction)\n- [Documentation](#documentation)\n- [Install](#install)\n- [Quick Start](#quick-start)\n- [Licensing](#licensing)\n\n# Introduction\n\nMain focus is an easy way to create lazy universally injectable dependencies;\nin less magical way. It also leans more on the side of making it easier to get\nthe dependency you need anywhere in the codebase.\n\npy-xinject allows you to easily inject lazily created universal dependencies into whatever code that needs them,\nin an easy to understand and self-documenting way.\n\n# Documentation\n\n**[\ud83d\udcc4 Detailed Documentation](https://xyngular.github.io/py-xinject/latest/)** | **[\ud83d\udc0d PyPi](https://pypi.org/project/xinject/)**\n\n# Install\n\n```bash\n# via pip\npip install xinject\n\n# via poetry\npoetry add xinject\n```\n\n# Quick Start\n\n```python\n# This is the \"my_resources.py\" file/module.\n\nimport boto3\nfrom xinject import DependencyPerThread\n\n\nclass S3(DependencyPerThread):\n def __init__(self, **kwargs):\n # Keeping this simple; a more complex version\n # may store the `kwargs` and lazily create the s3 resource\n # only when it's asked for (via a `@property or some such).\n\n self.resource = boto3.resource('s3', **kwargs)\n```\n\nTo use this resource in codebase, you can do this:\n\n```python\n# This is the \"my_functions.py\" file/module\n\nfrom .my_resources import S3\n\ndef download_file(file_name, dest_path):\n # Get dependency\n s3_resource = S3.grab().resource\n s3_resource.Bucket('my-bucket').download_file(file_name, dest_path)\n```\n\nInject a different version of the resource:\n\n```python\nfrom .my_resources import S3\nfrom .my_functions import download_file\n\nus_west_s3_resource = S3(region_name='us-west-2')\n\ndef get_s3_file_from_us_west(file, dest_path):\n # Can use Dependencies as a context-manager,\n # inject `use_west_s3_resource` inside `with`:\n with us_west_s3_resource:\n download_file(file, dest_path)\n\n# Can also use Dependencies as a function decorator,\n# inject `use_west_s3_resource` whenever this method is called.\n@us_west_s3_resource\ndef get_s3_file_from_us_west(file, dest_path):\n download_file(file, dest_path)\n```\n\n# Licensing\n\nThis library is licensed under the MIT-0 License. See the LICENSE file.\n",
"bugtrack_url": null,
"license": "",
"summary": "Lazy dependency injection.",
"version": "1.4.1",
"split_keywords": [
"dependency",
"injection",
"lazy",
"resource"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b4f1eddd6fe2be207e725174f0fdf8afd7fa9694ecaa707e83a3585f92eba467",
"md5": "216b3eaeefdf58f4e4f7a8f49eefb8fa",
"sha256": "849d8d6c7b6d337283624d28a43e7e741c886ea7118910c50d052bd213d21d93"
},
"downloads": -1,
"filename": "xinject-1.4.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "216b3eaeefdf58f4e4f7a8f49eefb8fa",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 35260,
"upload_time": "2023-04-15T14:11:57",
"upload_time_iso_8601": "2023-04-15T14:11:57.057329Z",
"url": "https://files.pythonhosted.org/packages/b4/f1/eddd6fe2be207e725174f0fdf8afd7fa9694ecaa707e83a3585f92eba467/xinject-1.4.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1abcb9f3ef6be0a170cedcb8f2e23b424cd97180433e8246767a41ed164f78ba",
"md5": "b0f9f5ad7d32a9b93b9a4f454421df10",
"sha256": "48588c3b5bb62731fde5e759cccdf4ab8ea1c53d2e442c6aa1109415485491cd"
},
"downloads": -1,
"filename": "xinject-1.4.1.tar.gz",
"has_sig": false,
"md5_digest": "b0f9f5ad7d32a9b93b9a4f454421df10",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 33280,
"upload_time": "2023-04-15T14:11:58",
"upload_time_iso_8601": "2023-04-15T14:11:58.526236Z",
"url": "https://files.pythonhosted.org/packages/1a/bc/b9f3ef6be0a170cedcb8f2e23b424cd97180433e8246767a41ed164f78ba/xinject-1.4.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-15 14:11:58",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "xyngular",
"github_project": "py-xinject",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "xinject"
}