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.1.0.tar.gz (33.2 kB view details)

Uploaded Source

Built Distribution

xinject-1.1.0-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xinject-1.1.0.tar.gz
  • Upload date:
  • Size: 33.2 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

Hashes for xinject-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f21998a3d55a13f3a19361123d4448ce05be0287c8743a8beab091492c2332fb
MD5 7c5f24ffd66f3191857574e0e0e84ebf
BLAKE2b-256 efebec0d599486b52021bea5970d1bf25f0524c420ef1b492481e2abc9feb3ae

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xinject-1.1.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

Hashes for xinject-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ec5e3540e34f8582c2dd3c99071dc28d74c940fe22fe8ccdaff5e6618840c4e
MD5 6261c4d25cfaa85e105f78b8623d3a58
BLAKE2b-256 3c84ee4d923ca12869a95c9eaa439fa3bf877a14aeca81842452881102b5b160

See more details on using hashes here.

Supported by

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