Loader Utility

The loader utility detecting and dynamically loading of class modules during runtime. Detection can be limited to classes inheriting from a specified class.

Warning

The loader utility only works with new style classes which inherit at least from object.

Note

The loader utility works best with Python modules or packages containing only Python modules, and not sub-packages (recursive search does not properly work).

Detect Class Modules

xobox.utils.loader.detect_class_modules(mod, parent=<class 'object'>)[source]

Detect available class modules or packages and return a dictionary of valid class names, referring to the module they are contained within.

Parameters:
  • mod (str) – the module or package to be scanned for classes
  • parent – the class potential candidates must be derived off
Returns:

dictionary of detected classes, mapping the class name to the module name in which the class has been detected

Example:

>>> from xobox.utils.loader import detect_class_modules
>>> detect_class_modules('queue', object)
{'deque': 'queue', 'Queue': 'queue', 'Empty': 'queue', 'PriorityQueue': 'queue', 'Full': 'queue', 'LifoQueue': 'queue'}
>>> detect_class_modules('zlib', object)
{'error': 'zlib'}
>>> detect_class_modules('math', object)
{}

Load Member

xobox.utils.loader.load_member(mod, member)[source]

Load a member (function, class, …) from a module and return it

Parameters:
  • mod (str) – the module or package name where the class should be loaded from
  • member (str) – the name of the member to be loaded
Returns:

reference to the loaded member (i. e. class or function pointer)

Example:

>>> from xobox.utils.loader import load_member
>>> f = load_member('math', 'ceil')
>>> f(1.4)
2