How do I programatically find what symbols were imported with Python import * command? -
i have system collects classes derive base classes , stores them in dictionary. want avoid having specify classes available (i discover them programatically), have used from modulename import *
statement. user directed place tests collected in modulename
module. however, cannot find way programatically determine symbols imported import statement. have tried using dir()
, __dict__
indicated in following example, no avail. how 1 programatically find symbols imported in manner (with import *
)? unable find them above methods.
testtypefigureouterrer.py:
from testtype1 import * testtype2 import * class testfigureouterrer(object): def __init__(self): self.existingtests = {'type1':{},'type2':{}} def findandsorttests(self): symbol in dir(): # tried: dir(self) , __dict__ try: thing = self.__getattribute__(symbol) except attributeerror: continue if issubclass(thing,testtype1): self.existingtests['type1'].update( dict(symbol,thing) ) elif issubclass(thing,testtype3): self.existingtests['type2'].update( dict(symbol,thing) ) else: continue if __name__ == "__main__": testfigureouterrer = testfigureouterrer() testfigureouterrer.findandsorttests()
testtype1.py:
class testtype1(object): pass class testa(testtype1): pass class testb(testtype1): pass
testtype2.py:
class testtype2: pass class testc(testtype2): pass class testd(testtype2): pass
take @ this answer, describes how determine name of loaded classes, can name of classes defined within context of module.
import sys, inspect clsmembers = inspect.getmembers(sys.modules['testtype1'], inspect.isclass)
which defined as
[('testa', testtype1.testa), ('testb', testtype1.testb), ('testtype1', testtype1.testtype1)]
you can replace testtype1
__name__
when you're within function of interest.
Comments
Post a Comment