Skip to content

syringe

This module provides an extremely simple utility for dependency injection.

It's made up of a single singleton class that provides a directory for the dependencies. The dependencies are stored in a dictionary and can be overridden or popped from the directory.

Usage:

>>> from syringe import Inject
>>> def foo():
>>>     return 42
>>>
>>> def oof():
>>>     return 24
>>>
>>> def bar():
>>>     return Inject(foo)
>>>
>>> bar()
42
>>> Inject.override(foo, oof)
>>> bar()
24
>>> Inject.pop(foo)
>>> bar()
42

Inject = _Inject() module-attribute

The singleton instance of _Inject, the only public member of this module.

_Inject

Injection class

This is a private class that is never directly instantiated.

Attributes:

Name Type Description
_dependencies dict[Any, Any]

The dependency directory

Source code in fractal_server/syringe.py
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
class _Inject:
    """
    Injection class

    This is a private class that is never directly instantiated.

    Attributes:
        _dependencies:
            The dependency directory
    """

    _dependencies: dict[Any, Any] = {}

    def __init__(self):
        global _instance_count
        if _instance_count == 1:
            raise RuntimeError("You must only instance this class once")
        _instance_count += 1

    @classmethod
    def __call__(cls, _callable: Callable[..., T]) -> T:
        """
        Call the dependency

        Args:
            _callable:
                Callable dependency object

        Returns:
            The output of calling `_callalbe` or its dependency override.
        """
        try:
            return cls._dependencies[_callable]()
        except KeyError:
            return _callable()

    @classmethod
    def pop(cls, _callable: Callable[..., T]) -> T:
        """
        Remove the dependency from the directory

        Args:
            _callable:
                Callable dependency object
        """
        try:
            return cls._dependencies.pop(_callable)
        except KeyError:
            raise RuntimeError(f"No dependency override for {_callable}")

    @classmethod
    def override(
        cls, _callable: Callable[..., T], value: Callable[..., T]
    ) -> None:
        """
        Override dependency

        Substitute a dependency with a different arbitrary callable.

        Args:
            _callable:
                Callable dependency object
            value:
                Callable override
        """
        cls._dependencies[_callable] = value

__call__(_callable) classmethod

Call the dependency

Parameters:

Name Type Description Default
_callable Callable[..., T]

Callable dependency object

required

Returns:

Type Description
T

The output of calling _callalbe or its dependency override.

Source code in fractal_server/syringe.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@classmethod
def __call__(cls, _callable: Callable[..., T]) -> T:
    """
    Call the dependency

    Args:
        _callable:
            Callable dependency object

    Returns:
        The output of calling `_callalbe` or its dependency override.
    """
    try:
        return cls._dependencies[_callable]()
    except KeyError:
        return _callable()

override(_callable, value) classmethod

Override dependency

Substitute a dependency with a different arbitrary callable.

Parameters:

Name Type Description Default
_callable Callable[..., T]

Callable dependency object

required
value Callable[..., T]

Callable override

required
Source code in fractal_server/syringe.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
@classmethod
def override(
    cls, _callable: Callable[..., T], value: Callable[..., T]
) -> None:
    """
    Override dependency

    Substitute a dependency with a different arbitrary callable.

    Args:
        _callable:
            Callable dependency object
        value:
            Callable override
    """
    cls._dependencies[_callable] = value

pop(_callable) classmethod

Remove the dependency from the directory

Parameters:

Name Type Description Default
_callable Callable[..., T]

Callable dependency object

required
Source code in fractal_server/syringe.py
82
83
84
85
86
87
88
89
90
91
92
93
94
@classmethod
def pop(cls, _callable: Callable[..., T]) -> T:
    """
    Remove the dependency from the directory

    Args:
        _callable:
            Callable dependency object
    """
    try:
        return cls._dependencies.pop(_callable)
    except KeyError:
        raise RuntimeError(f"No dependency override for {_callable}")