prefect.utilities.dispatch
Provides methods for performing dynamic dispatch for actions on base type to one of its subtypes.
Example:
@register_base_type
class Base:
@classmethod
def __dispatch_key__(cls):
return cls.__name__.lower()
class Foo(Base):
...
key = get_dispatch_key(Foo) # 'foo'
lookup_type(Base, key) # Foo
get_dispatch_key(cls_or_instance, allow_missing=False)
Retrieve the unique dispatch key for a class type or instance.
This key is defined at the __dispatch_key__
attribute. If it is a callable, it
will be resolved.
If allow_missing
is False
, an exception will be raised if the attribute is not
defined or the key is null. If True
, None
will be returned in these cases.
Source code in src/prefect/utilities/dispatch.py
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 |
|
get_registry_for_type(cls)
Get the first matching registry for a class or any of its base classes.
If not found, None
is returned.
Source code in src/prefect/utilities/dispatch.py
33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
lookup_type(cls, dispatch_key)
Look up a dispatch key in the type registry for the given class.
Source code in src/prefect/utilities/dispatch.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
register_base_type(cls)
Register a base type allowing child types to be registered for dispatch with
register_type
.
The base class may or may not define a __dispatch_key__
to allow lookups of the
base type.
Source code in src/prefect/utilities/dispatch.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
register_type(cls)
Register a type for lookup with dispatch.
The type or one of its parents must define a unique __dispatch_key__
.
One of the classes base types must be registered using register_base_type
.
Source code in src/prefect/utilities/dispatch.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|