util.py 498B

12345678910111213141516
  1. # coding: utf-8
  2. import importlib
  3. def get_class_from_string_path(config, string_path: str) -> type:
  4. """
  5. Return class matching with given path, eg "mymodule.MyClass"
  6. :param config: config object
  7. :param string_path: class path, eg "mymodule.MyClass"
  8. :return: imported class
  9. """
  10. module_address = '.'.join(string_path.split('.')[:-1])
  11. class_name = string_path.split('.')[-1]
  12. module_ = importlib.import_module(module_address)
  13. return getattr(module_, class_name)