hbutils.reflection.imports

Overview:

Import functions, may be useful when support some dynamic features (such as imports in cli development).

import_object

hbutils.reflection.imports.import_object(obj_name: str, module_name: Optional[str] = None)[source]
Overview:

Dynamically import an object from module.

Arguments:
  • obj_name (str): Name of the object.

  • module_name (Optional[str]): Name of the module, default is None which means the builtins module.

Returns:
  • obj: Imported object.

Example::
>>> import_object('zip')               # <class 'zip'>
>>> import_object('ndarray', 'numpy')  # <class 'numpy.ndarray'>

quick_import_object

hbutils.reflection.imports.quick_import_object(full_name: str, predicate: Optional[Callable] = None) → Tuple[Any, str, str][source]
Overview:

Quickly dynamically import an object with a single name.

Arguments:
  • full_name (str): Full name of the object, attribute is supported as well.

  • predicate (Callable): Predicate function, default is None means no limitation.

Returns:
  • obj (Tuple[Any, str, str]): Imported object.

Example::
>>> quick_import_object('zip')                     # <class 'zip'>, '', 'zip'
>>> quick_import_object('numpy.ndarray')           # <class 'numpy.ndarray'>, 'numpy', 'ndarray'
>>> quick_import_object('numpy.ndarray.__name__')  # 'ndarray', 'numpy', 'ndarray.__name__'

iter_import_objects

hbutils.reflection.imports.iter_import_objects(full_pattern: str, predicate: Optional[Callable] = None) → Iterator[Tuple[Any, str, str]][source]
Overview:

Quickly dynamically import all objects with full name pattern.

Arguments:
  • full_pattern (str): Full pattern of the object, attribute is supported as well.

  • predicate (Callable): Predicate function, default is None means no limitation.

Returns:
  • iter (Iterator[Tuple[Any, str, str]]): Iterator for all the imported objects.