Dispatchers: factory classes that call other constructors.
Contents
Delegate (base.Factory): builds classes and/or instances using methods
that follow a naming convention and the str names of the types of the
first argument passed to the create class method.
Sourcerer (base.Factory, abc.ABC): builds classes and/or instances
using methods that follow a naming convention (set at
options._METHOD_NAMER) and a dict of types stored in the sources
class attribute.
Delegate()
dataclass
¶
Bases: Factory
Builds based on the str name of the type passed.
This factory acts as a dispatcher to call creation methods based on the type
or name of the type passed in a manner similar to Sourcerer. However,
unlike Sourcerer, Delegate only finds a matching creation method if the
str name of the type of item matches a substring of the creation method
name using the format of options._METHOD_NAMER. A str item is not named
by its type: the str itself is used as the name (so 'ini' calls the
from_ini method with 'ini').
create(item, parameters=None, **kwargs)
classmethod
¶
Creates an item based on item and possibly parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Any
|
data for construction of the returned item. |
required |
parameters
|
Parameters | None
|
keyword arguments to pass or add to a created instance.
Defaults to |
None
|
**kwargs
|
Kwargs
|
keyword arguments passed to the creation method. |
{}
|
Raises:
| Type | Description |
|---|---|
AttributeError
|
if an appropriate method does not exist for the
data type of |
Returns:
| Type | Description |
|---|---|
Any
|
Created item. |
Source code in src/wonka/dispatchers.py
Sourcerer()
dataclass
¶
Bases: Factory, ABC
Builds based on compatibility with keys in the sources class attribute.
This factory acts as a dispatcher to call other methods based on the type
passed. Unlike Delegate, Sourcerer is more forgiving by allowing the
type passed to be a subtype or instance of the type listed as a key in the
sources class attribute. The keys of sources are checked in order, so
more specific types should be listed before more general ones.
The name for a Sourcerer is spelled the way it is instead of "Sorcerer"
because the sources attribute is used. This is inspired by the "Divinity:
Original Sin" games where the magic users are called "Sourcerers" because
they may manipulate the magical energy known as "source".
https://divinity.fandom.com/wiki/Sourcerer
Attributes:
| Name | Type | Description |
|---|---|---|
sources |
MutableMapping[type[Any], str]
|
|
sources = {}
class-attribute
¶
Class Methods
create(item, parameters=None, **kwargs)
classmethod
¶
Creates an item based on item and possibly parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Any
|
data for construction of the returned item. |
required |
parameters
|
Parameters | None
|
keyword arguments to pass or add to a created instance.
Defaults to |
None
|
**kwargs
|
Kwargs
|
keyword arguments passed to the creation method. |
{}
|
Raises:
| Type | Description |
|---|---|
AttributeError
|
if the value matching the key |
KeyError
|
if there is no key in |
Returns:
| Type | Description |
|---|---|
Any
|
Created item. |