def import_class(val):
try:<EOL><INDENT>parts = val.split("<STR_LIT:.>")<EOL>module_path, class_name = "<STR_LIT:.>".join(parts[:-<NUM_LIT:1>]), parts[-<NUM_LIT:1>]<EOL>module = import_module(module_path)<EOL>return getattr(module, class_name)<EOL><DEDENT>except (ImportError, AttributeError) as e:<EOL><INDENT>msg = "<STR_LIT>".format(<EOL>val, e.__class__.__name__, e<EOL>)<EOL>raise ImportError(msg)<EOL><DEDENT>
Import a class from a string module path.
Pattern borrowed from Django REST Framework.
See rest_framework/settings.py#L170-L182