Skip to main content

Lazy dependency injection.

Project description

PythonSupport PyPI version

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

xinject-1.10.1.tar.gz (34.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xinject-1.10.1-py3-none-any.whl (36.6 kB view details)

Uploaded Python 3

File details

Details for the file xinject-1.10.1.tar.gz.

File metadata

  • Download URL: xinject-1.10.1.tar.gz
  • Upload date:
  • Size: 34.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.3 Linux/6.17.0-1018-azure

File hashes

Hashes for xinject-1.10.1.tar.gz
Algorithm Hash digest
SHA256 050106e897260814184799a6efcaaaec8425daf331efd1a1edcb05a3de59ace1
MD5 9593dd9137a5b62435afb52dcca3dc25
BLAKE2b-256 43ddde0dca9107b86c2c67f40c753fcc1a550442f7d8e161cd96c542e1c5b164

See more details on using hashes here.

File details

Details for the file xinject-1.10.1-py3-none-any.whl.

File metadata

  • Download URL: xinject-1.10.1-py3-none-any.whl
  • Upload date:
  • Size: 36.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.12.3 Linux/6.17.0-1018-azure

File hashes

Hashes for xinject-1.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 100fdcb1524abbb83f24ed6e895a5071d884cf779c39b70aa07728f459ec544b
MD5 44436d694b3927bf0a321cdf52c80c5d
BLAKE2b-256 d470262d8173667e2ea19348fbb38e543c51dd724d28f63c49e1068796536979

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page