clarena.utils.misc

The submodule in utils of miscellaneous utilities.

 1r"""The submodule in `utils` of miscellaneous utilities."""
 2
 3__all__ = ["str_to_class"]
 4
 5import importlib
 6import logging
 7
 8# always get logger for built-in logging in each module
 9pylogger = logging.getLogger(__name__)
10
11
12def str_to_class(class_path: str) -> type:
13    r"""Convert a string to a class.
14
15    **Args:**
16    - **class_path** (`str`): the string of the class path, e.g. `torchvision.datasets.MNIST`.
17
18    **Returns:**
19    - **cls** (`type`): the class object.
20    """
21    module_name, class_name = class_path.rsplit(".", 1)
22    module = importlib.import_module(module_name)
23    cls = getattr(module, class_name)
24    return cls
def str_to_class(class_path: str) -> type:
13def str_to_class(class_path: str) -> type:
14    r"""Convert a string to a class.
15
16    **Args:**
17    - **class_path** (`str`): the string of the class path, e.g. `torchvision.datasets.MNIST`.
18
19    **Returns:**
20    - **cls** (`type`): the class object.
21    """
22    module_name, class_name = class_path.rsplit(".", 1)
23    module = importlib.import_module(module_name)
24    cls = getattr(module, class_name)
25    return cls

Convert a string to a class.

Args:

  • class_path (str): the string of the class path, e.g. torchvision.datasets.MNIST.

Returns:

  • cls (type): the class object.