module
OptionalImportError
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
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
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').