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 |
|
__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 |
Source code in fractal_server/syringe.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
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 |
|
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 |
|