Created
June 20, 2014 02:17
-
-
Save struys/3077f8a2a6c1c3cef096 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class _ImporterProxy(object): | |
def __init__(self, mod): | |
self.__mod = mod | |
def __getattr__(self, meth): | |
try: | |
ret = getattr(self.__mod, meth) | |
except AttributeError: | |
ret = _ImporterProxy(__import__(self.__mod.__name__ + '.' + meth, fromlist=['__trash'])) | |
self.__dict__[meth] = ret | |
return ret | |
def __repr__(self): | |
return repr(self.__mod) | |
class Importer(object): | |
def __getattr__(self, meth): | |
ret = _ImporterProxy(__import__(meth)) | |
self.__dict__[meth] = ret | |
return ret | |
i = Importer() | |
if __name__ != '__main__': | |
import sys | |
self = sys.modules[__name__] | |
i.__self__ = self | |
sys.modules[__name__] = i |
This file contains hidden or 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
import importer as i | |
i.whatever_you_want |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment