Skip to content

Instantly share code, notes, and snippets.

@rizo
Last active December 15, 2015 02:39
Show Gist options
  • Save rizo/5188615 to your computer and use it in GitHub Desktop.
Save rizo/5188615 to your computer and use it in GitHub Desktop.
def class_with_str(class_name, module_name):
"""
Returns a class object with the provided `class_name` in the scope of the `module_name`.
"""
try:
class_ = reduce(getattr, class_name.split("."), sys.modules[module_name])
except AttributeError:
raise NameError('Class "%s" does not exist!' % class_name)
if (isinstance(class_var, (types.ClassType, types.TypeType))):
return class_
raise Exception('"%s" is not a class!' % class_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment