Skip to content

get_obj_from_str

The object initiation from strings.

It will initiate an object according to the class description as a string.

Parameters:

Name Type Description Default
string str

The object class description as a string, e.g., "tinybig.expansion.bspline_expansion" and "torch.nn.functional.sigmoid"

required
reload bool

The module reloading boolean tag.

False

Returns:

Type Description
object

The initiated object of the corresponding class described by the input parameter "string".

Source code in tinybig/util/util.py
def get_obj_from_str(string: str, reload: bool = False):
    """
    The object initiation from strings.

    It will initiate an object according to the class description as a string.

    Parameters
    ----------
    string: str
        The object class description as a string,
        e.g., "tinybig.expansion.bspline_expansion" and "torch.nn.functional.sigmoid"
    reload: bool, default = False
        The module reloading boolean tag.

    Returns
    -------
    object
        The initiated object of the corresponding class described by the input parameter "string".
    """
    module, cls = string.rsplit(".", 1)
    if reload:
        module_imp = importlib.import_module(module)
        importlib.reload(module_imp)
    return getattr(importlib.import_module(module, package=None), cls)