jacinle.utils.imp#

Functions

classname_to_tuple(classname)

Convert a string to a module-class tuple.

load_class(classname[, exit_on_error])

Load a class by its classname (including module).

load_module(module_name)

Import a module by its module name (e.g., jacinle.utils.imp).

load_module_filename(module_filename)

Import a module by its filename (e.g., /Users/jiayuan/Projects/Jacinle/jacinle/utils/imp.py).

load_source(filename[, name])

Load a source file as a module.

module_vars_as_dict(module)

Get all variables in a module as a dictionary.

tuple_to_classname(t)

Convert a module-class tuple to a string.

Functions

classname_to_tuple(classname)[source]#

Convert a string to a module-class tuple.

Parameters:

classname (str) – the string representation of the module-class tuple. E.g., ‘jacinle.utils.imp.load_module’.

Returns:

the module-class tuple. E.g., (‘jacinle.utils.imp’, ‘load_module’).

Return type:

Tuple[str, str]

load_class(classname, exit_on_error=True)[source]#

Load a class by its classname (including module).

Parameters:
  • classname (str | Tuple[str, str]) – the classname of the class.

  • exit_on_error (bool) – whether to exit the program when the class cannot be loaded.

Returns:

the loaded class.

load_module(module_name)[source]#

Import a module by its module name (e.g., jacinle.utils.imp).

Parameters:

module_name (str) – the name of the module.

Returns:

the imported module.

Return type:

Any

load_module_filename(module_filename)[source]#

Import a module by its filename (e.g., /Users/jiayuan/Projects/Jacinle/jacinle/utils/imp.py).

Parameters:

module_filename (str) – the filename of the module.

Returns:

the imported module.

Return type:

Any

load_source(filename, name=None)[source]#

Load a source file as a module.

Parameters:
  • filename (str) – the filename of the source file.

  • name (str | None) – the name of the module.

Returns:

the loaded module.

Return type:

Any

module_vars_as_dict(module)[source]#

Get all variables in a module as a dictionary.

Parameters:

module (Any) – the module.

Returns:

a dictionary of all variables in the module.

Return type:

Dict[str, Any]

tuple_to_classname(t)[source]#

Convert a module-class tuple to a string.

Parameters:

t (Tuple[str, str]) – the module-class tuple. E.g., (‘jacinle.utils.imp’, ‘load_module’).

Returns:

the string representation of the module-class tuple. E.g., ‘jacinle.utils.imp.load_module’.

Return type:

str