1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
# -*- coding: utf-8 -*-
"""Module for methods used to load objects/symbols dynamically in python"""
from typing import Union
import types
__version__ = "0.0.1"
__all__ = "__version__", "load_obj", "full_name"
def load_obj(name: str) -> Union[type, types.FunctionType, types.BuiltinFunctionType]:
"""Load a type from a fully qualified name"""
components = name.split(".")
mod = __import__(components[0])
# Get the components one by one
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
def full_name(
symbol: Union[type, types.FunctionType, types.BuiltinFunctionType]
) -> str:
"""Get the fully qualified name of a type."""
return symbol.__module__ + "." + symbol.__name__
|