我想基於在另一個類的方法內部使用的類來構建依賴關係樹。所以不是父類。爲此,我想檢查一個方法是否使用從名爲Table
的特殊類繼承的任何類。獲取方法/函數中使用的類的列表
實施例:
class TestTableOne(Table):
"""Class for testing table loader"""
def source(self):
source_file = os.path.join('data',
'test_table_one.csv')
return pd.read_csv(source_file, dtype=dtype, converters=converters)
def output(self):
output_path = 'out/path'
return output_path
def post_processors(self):
return [
drop_age_column,
calculate_new_age
]
class TestTableTwo(Table):
"""Class for testing tables loader"""
def source(self):
return TestTableOne.fetch()
def output(self):
output_path = os.path.join(tempfile.mkdtemp(),
'output',
self.get_cached_filename('test_table_one', 'pkl')
)
return output_path
def something_does_nothing(self, table):
result = TestTableOne.get()
return result
def post_processors(self):
return [
self.something_does_nothing
]
在這裏,我希望能夠以檢查是否TestTableTwo.source
是依賴於從Table
繼承,在這種情況下TestTableOne
任何其他類。
所以我婉問類似inspect.classes_that_appears_in(TestTableTwo.source)
的東西,並得到[TestTableOne]
這是可能在Python?即時通訊使用python 3 btw。
沒有你檢查['__mro__'](https://stackoverflow.com/questions/2010692/what -does-mro-do)魔法? –
如果您使用ast.parse來源,然後使用AST來收集信息,那麼這是可能的。沒有什麼可以做的。如果你幸運的話,可能會有第三方圖書館...... –
@ChihebNexus我認爲這種情況並不合理。 'TestTableTwo.source .__ mro__'不存在我相信,因爲那只是檢查類繼承權的方法解析順序嗎?在這裏,我試圖看到一個類是beeing內部使用的方法。 –