Skip to main content

No project description provided

Project description

ctx_inject Library

ctx_inject is a Python dependency injection library that provides utilities for managing the injection of arguments and dependencies into functions and classes. It leverages Python's type hints and inspect module to resolve and inject dependencies at runtime.

Features

  • Dependency Injection: Automatically resolve function arguments from a context, enabling clean and flexible dependency injection patterns.
  • Support for Annotations: Works with Python's type annotations, allowing for rich type-checking.
  • Custom Validators: Allows custom validation for injected values.
  • Model-based Injection: Supports injecting values from models (e.g., database models) into functions based on the field names.

Installation

You can install the ctx_inject library via pip:

pip install ctx_inject

Usage

1. Injecting Function Arguments

You can inject arguments into functions using the inject_args function.

from ctx_inject import inject_args

def my_function(a: int, b: str):
    return f"Received {a} and {b}"

# Create a context with dependencies
context = {
    'a': 5,
    'b': 'Hello'
}

# Inject arguments into the function
injected_function = inject_args(my_function, context)

# Call the function with the injected arguments
result = injected_function()
print(result)  # Output: Received 5 and Hello

2. Using Custom Injectables

You can define custom injectables by implementing the Injectable class or its subclasses.

from ctx_inject import Injectable

class MyInjectable(Injectable):
    def __init__(self, default_value):
        super().__init__(default_value)

    def validate(self, instance, basetype):
        return instance  # Custom validation can be added here

# Creating an injectable
injectable = MyInjectable(42)

# Usage in a context
context = {
    'my_injectable': injectable
}

3. Injecting Dependencies with Depends

Dependencies can be injected dynamically into functions using DependsInject.

from ctx_inject import DependsInject, inject_args

def get_service(name: str) -> str:
    return f"Service: {name}"

# Creating a context with dependencies
context = {
    'service_name': 'MyService'
}

# Defining the injected function
injected_function = inject_args(get_service, context)

# The injected function will use the provided context for dependencies
result = injected_function()
print(result)  # Output: Service: MyService

Classes and Functions

Injectable

Base class for defining injectable values.

  • default: The default value that will be injected.
  • validate: Used to validate the injected value.

ArgsInjectable

Subclass of Injectable used for argument injection.

CallableInjectable

Injectable that expects a callable as its default.

DependsInject

Subclass of CallableInjectable used for dependency injection.

ModelFieldInject

An injectable class that injects fields from models (e.g., database models).

inject_args(func, context)

Injects arguments into the given function based on the provided context.

resolve_ctx(args, context, allow_incomplete)

Resolves the context for the provided function arguments.

func_arg_factory(name, param, annotation)

Factory function to create FuncArg objects for function parameters.

Error Handling

The library defines several exceptions for error handling:

  • UnresolvedInjectableError: Raised when a dependency cannot be resolved.
  • UnInjectableError: Raised when a function argument cannot be injected.
  • ValidationError: Raised when a validation fails for injected values.
  • InvalidInjectableDefinition: Raised when an injectable is incorrectly defined.

Validation Functions

The library provides a set of validation functions for constraining argument values:

  • ConstrainedStr: Validate string values.
  • ConstrainedNumber: Validate numeric values.
  • ConstrainedDatetime: Validate datetime values.
  • ConstrainedUUID: Validate UUID values.
  • ConstrainedEnum: Validate Enum values.
  • ConstrainedItems: Validate items in a collection (list, tuple, set, etc.).

Example Usage of Constrained Values

from ctx_inject import ConstrainedStr, ValidationError

def my_function(name: str):
    return f"Hello, {name}"

# Using constrained string validation
context = {
    'name': ConstrainedStr('John', min_length=3)
}

# Injecting arguments with validation
injected_function = inject_args(my_function, context)
result = injected_function()
print(result)  # Output: Hello, John

Contributing

Feel free to contribute to the ctx_inject library! You can submit bug reports, feature requests, or pull requests.

  1. Fork the repository.
  2. Create a new branch for your changes.
  3. Write tests for your changes.
  4. Submit a pull request.

License

ctx_inject is released under the MIT License. See LICENSE for more information.

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

ctxinject-0.1.0.tar.gz (346.3 kB view details)

Uploaded Source

Built Distribution

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

ctxinject-0.1.0-py3-none-any.whl (437.1 kB view details)

Uploaded Python 3

File details

Details for the file ctxinject-0.1.0.tar.gz.

File metadata

  • Download URL: ctxinject-0.1.0.tar.gz
  • Upload date:
  • Size: 346.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Windows/11

File hashes

Hashes for ctxinject-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5b5466cdce9aa08b931eaad49a6fd3b68a91f77794cc1321e18e65d1a6b69bd6
MD5 86aab342a4aa1212432a8cc4eef63f15
BLAKE2b-256 14d389d66d43ad4d57b79b0a75bfff2b162ffb81477310ea13d0bf569a79e93d

See more details on using hashes here.

File details

Details for the file ctxinject-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ctxinject-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 437.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Windows/11

File hashes

Hashes for ctxinject-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da7a12fbc84e8d9cdd663394743c05966cefae636c684f525e441db544bc4d6e
MD5 824da168150c133d18c0bf49388f113a
BLAKE2b-256 d16992acc381a1d5246fd5599965202f5ff059c68eef347c6f9566e223b5a966

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