Last active
November 2, 2016 10:04
-
-
Save xianghuzhao/84a477f4f71085119081c25eb01e8259 to your computer and use it in GitHub Desktop.
Load Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def loadObject(moduleName, className): | |
try: | |
m = __import__(moduleName, globals(), locals(), [className]) | |
except ImportError, e: | |
raise Exception('Could not from %s import %s' % (moduleName, className)) | |
return getattr(m, className)() | |
import imp | |
def loadObject2(moduleName, className): | |
pathname = None | |
for n in moduleName.split('.'): | |
path = None if (pathname is None) else [pathname] | |
try: | |
fp, pathname, description = imp.find_module(n, path) | |
except ImportError, e: | |
raise ImportError('Could not from %s import %s' % (moduleName, className)) | |
try: | |
m = imp.load_module(className, fp, pathname, description) | |
finally: | |
if fp: | |
fp.close() | |
return getattr(m, className)() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment