我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用inspect.classify_class_attrs()。
def classify_class_attrs(object): """Wrap inspect.classify_class_attrs, with fixup for data descriptors.""" def fixup(data): name, kind, cls, value = data if inspect.isdatadescriptor(value): kind = 'data descriptor' return name, kind, cls, value return map(fixup, inspect.classify_class_attrs(object)) # ----------------------------------------------------- module manipulation
def classify_class_attrs(object): """Wrap inspect.classify_class_attrs, with fixup for data descriptors.""" def fixup(data): name, kind, cls, value = data if inspect.isdatadescriptor(value): kind = 'data descriptor' return name, kind, cls, value return map(fixup, inspect.classify_class_attrs(object)) # ----------------------------------------------------- Unicode support helpers
def __getattr__(*args): raise AssertionError("should not __getattr__ method descriptors") # Helper for testing classify_class_attrs.
def attrs_wo_objs(cls): return [t[:3] for t in inspect.classify_class_attrs(cls)]
def test_classify_builtin_types(self): # Simple sanity check that all built-in types can have their # attributes classified. for name in dir(__builtins__): builtin = getattr(__builtins__, name) if isinstance(builtin, type): inspect.classify_class_attrs(builtin)
def classify_class_attrs(object): """Wrap inspect.classify_class_attrs, with fixup for data descriptors.""" results = [] for (name, kind, cls, value) in inspect.classify_class_attrs(object): if inspect.isdatadescriptor(value): kind = 'data descriptor' results.append((name, kind, cls, value)) return results # ----------------------------------------------------- module manipulation
def test_classify_oldstyle(self): """classify_class_attrs finds static methods, class methods, properties, normal methods, and data attributes on an old-style class. """ self._classify_test(False)
def test_classify_builtin_types(self): # Simple sanity check that all built-in types can have their # attributes classified. for name in dir(__builtin__): builtin = getattr(__builtin__, name) if isinstance(builtin, type): inspect.classify_class_attrs(builtin)
def test_inspect_classify_class_attrs(self): # indirectly test __objclass__ from inspect import Attribute values = [ Attribute(name='__class__', kind='data', defining_class=object, object=EnumMeta), Attribute(name='__doc__', kind='data', defining_class=self.Color, object=None), Attribute(name='__members__', kind='property', defining_class=EnumMeta, object=EnumMeta.__members__), Attribute(name='__module__', kind='data', defining_class=self.Color, object=__name__), Attribute(name='blue', kind='data', defining_class=self.Color, object=self.Color.blue), Attribute(name='green', kind='data', defining_class=self.Color, object=self.Color.green), Attribute(name='red', kind='data', defining_class=self.Color, object=self.Color.red), Attribute(name='name', kind='data', defining_class=Enum, object=Enum.__dict__['name']), Attribute(name='value', kind='data', defining_class=Enum, object=Enum.__dict__['value']), ] values.sort(key=lambda item: item.name) result = list(inspect.classify_class_attrs(self.Color)) result.sort(key=lambda item: item.name) failed = False for v, r in zip(values, result): if r != v: print('\n%s\n%s\n%s\n%s\n' % ('=' * 75, r, v, '=' * 75), sep='') failed = True if failed: self.fail("result does not equal expected, see print above")
def __getattr__(*args): raise AttributeError("broken method descriptor") # Helper for testing classify_class_attrs.
def test_classify_DynamicClassAttribute(self): class Meta(type): def __getattr__(self, name): if name == 'ham': return 'spam' return super().__getattr__(name) class VA(metaclass=Meta): @types.DynamicClassAttribute def ham(self): return 'eggs' should_find_dca = inspect.Attribute('ham', 'data', VA, VA.__dict__['ham']) self.assertIn(should_find_dca, inspect.classify_class_attrs(VA)) should_find_ga = inspect.Attribute('ham', 'data', Meta, 'spam') self.assertIn(should_find_ga, inspect.classify_class_attrs(VA))
def test_classify_metaclass_class_attribute(self): class Meta(type): fish = 'slap' def __dir__(self): return ['__class__', '__module__', '__name__', 'fish'] class Class(metaclass=Meta): pass should_find = inspect.Attribute('fish', 'data', Meta, 'slap') self.assertIn(should_find, inspect.classify_class_attrs(Class))
def test_classify_VirtualAttribute(self): class Meta(type): def __dir__(cls): return ['__class__', '__module__', '__name__', 'BOOM'] def __getattr__(self, name): if name =='BOOM': return 42 return super().__getattr(name) class Class(metaclass=Meta): pass should_find = inspect.Attribute('BOOM', 'data', Meta, 42) self.assertIn(should_find, inspect.classify_class_attrs(Class))
def test_classify_VirtualAttribute_multi_classes(self): class Meta1(type): def __dir__(cls): return ['__class__', '__module__', '__name__', 'one'] def __getattr__(self, name): if name =='one': return 1 return super().__getattr__(name) class Meta2(type): def __dir__(cls): return ['__class__', '__module__', '__name__', 'two'] def __getattr__(self, name): if name =='two': return 2 return super().__getattr__(name) class Meta3(Meta1, Meta2): def __dir__(cls): return list(sorted(set(['__class__', '__module__', '__name__', 'three'] + Meta1.__dir__(cls) + Meta2.__dir__(cls)))) def __getattr__(self, name): if name =='three': return 3 return super().__getattr__(name) class Class1(metaclass=Meta1): pass class Class2(Class1, metaclass=Meta3): pass should_find1 = inspect.Attribute('one', 'data', Meta1, 1) should_find2 = inspect.Attribute('two', 'data', Meta2, 2) should_find3 = inspect.Attribute('three', 'data', Meta3, 3) cca = inspect.classify_class_attrs(Class2) for sf in (should_find1, should_find2, should_find3): self.assertIn(sf, cca)
def test_classify_class_attrs_with_buggy_dir(self): class M(type): def __dir__(cls): return ['__class__', '__name__', 'missing'] class C(metaclass=M): pass attrs = [a[0] for a in inspect.classify_class_attrs(C)] self.assertNotIn('missing', attrs)