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
| ATTRIBUTE |
DESCRIPTION |
Inject |
The singleton instance of _Inject, the only public member of this module.
|
Attributes
Inject = _Inject()
module-attribute
The singleton instance of _Inject, the only public member of this module.
Classes
_Inject
Injection class
This is a private class that is never directly instantiated.
| ATTRIBUTE |
DESCRIPTION |
_dependencies |
TYPE:
dict[Any, Any]
|
Source code in fractal_server/syringe.py
| 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) -> None:
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
|
Methods:
__call__(_callable)
classmethod
Call the dependency
| PARAMETER |
DESCRIPTION |
_callable
|
Callable dependency object
TYPE:
Callable[..., T]
|
| RETURNS |
DESCRIPTION |
T
|
The output of calling _callalbe or its dependency override.
|
Source code in fractal_server/syringe.py
| @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.
| PARAMETER |
DESCRIPTION |
_callable
|
Callable dependency object
TYPE:
Callable[..., T]
|
value
|
TYPE:
Callable[..., T]
|
Source code in fractal_server/syringe.py
| @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
| PARAMETER |
DESCRIPTION |
_callable
|
Callable dependency object
TYPE:
Callable[..., T]
|
Source code in fractal_server/syringe.py
| @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}")
|