Index
CheckKeyDuplicatesYamlLoader
Bases: SafeLoader
YAML loader that detects duplicate keys and either warns or raises an error. Also tracks line numbers for values to enable better error reporting.
Source code in src/sparkwheel/utils/misc.py
construct_object(node, deep=False)
Construct object and attach source location metadata.
Source code in src/sparkwheel/utils/misc.py
CompInitMode
Bases: StrEnum
Component initialization mode for Component.
- DEFAULT: Instantiate by calling the component
- CALLABLE: Return the callable (or partial with kwargs)
- DEBUG: Use pdb.runcall for debugging
Source code in src/sparkwheel/utils/enums.py
check_key_duplicates(ordered_pairs)
Checks if there is a duplicated key in the sequence of ordered_pairs.
If there is - it will log a warning or raise ValueError
(if configured by environmental var SPARKWHEEL_STRICT_KEYS==1)
Otherwise, it returns the dict made from this sequence.
Note: This function is kept for compatibility but is primarily used by the YAML loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ordered_pairs
|
list[tuple[Any, Any]]
|
sequence of (key, value) |
required |
Source code in src/sparkwheel/utils/misc.py
damerau_levenshtein_distance(s1, s2)
Calculates the Damerau–Levenshtein distance between two strings for spelling correction. https://en.wikipedia.org/wiki/Damerau–Levenshtein_distance
Source code in src/sparkwheel/utils/module.py
ensure_tuple(vals)
Returns a tuple of vals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vals
|
Any
|
input data to convert to a tuple. |
required |
first(iterable, default=None)
Returns the first item in the given iterable or default if empty.
instantiate(__path, __mode, **kwargs)
Create an object instance or call a callable object from a class or function represented by __path.
kwargs will be part of the input arguments to the class constructor or function.
The target component must be a class or a function, if not, return the component directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
__path
|
str
|
if a string is provided, it's interpreted as the full path of the target class or function component.
If a callable is provided, |
required |
__mode
|
str
|
the operating mode for invoking the (callable)
|
required |
kwargs
|
Any
|
keyword arguments to the callable represented by |
{}
|
Source code in src/sparkwheel/utils/module.py
issequenceiterable(obj)
Determine if the object is an iterable sequence and is not a string.
Source code in src/sparkwheel/utils/misc.py
look_up_option(opt_str, supported, default='no_default', print_all_options=True)
Look up the option in the supported collection and return the matched item. Raise a value error possibly with a guess of the closest match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
opt_str
|
Hashable
|
The option string or Enum to look up. |
required |
supported
|
Collection[Any] | EnumMeta
|
The collection of supported options, it can be list, tuple, set, dict, or Enum. |
required |
default
|
Any
|
If it is given, this method will return |
'no_default'
|
print_all_options
|
bool
|
whether to print all available options when |
True
|
Examples:
.. code-block:: python
from enum import Enum
from sparkwheel.utils import look_up_option
class Color(Enum):
RED = "red"
BLUE = "blue"
look_up_option("red", Color) # <Color.RED: 'red'>
look_up_option(Color.RED, Color) # <Color.RED: 'red'>
look_up_option("read", Color)
# ValueError: By 'read', did you mean 'red'?
# 'read' is not a valid option.
# Available options are {'blue', 'red'}.
look_up_option("red", {"red", "blue"}) # "red"
Source code in src/sparkwheel/utils/module.py
optional_import(module, version='', name='')
Imports an optional module specified by module string.
Any importing related exceptions will be stored, and exceptions raise lazily
when attempting to use the failed-to-import module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
str
|
name of the module to be imported. |
required |
version
|
str
|
version string (currently not checked, for future use). |
''
|
name
|
str
|
a non-module attribute (such as method/class) to import from the imported module. |
''
|
Returns:
| Type | Description |
|---|---|
tuple[Any, bool]
|
The imported module and a boolean flag indicating whether the import is successful. |
Examples::
>>> yaml, flag = optional_import('yaml')
>>> print(flag)
True
>>> the_module, flag = optional_import('unknown_module')
>>> print(flag)
False
>>> the_module.method # trying to access a module which is not imported
OptionalImportError: import unknown_module (No module named 'unknown_module').