from random import randrange from tombola import Tombola @Tombola.register # <1> class TomboList(list): # <2> def pick(self): if self: # <3> position = randrange(len(self)) return super().pop(position) # <4> else: raise LookupError('pop from empty TomboList') def load(self, iterable): self.extend(iterable) # <5> def loaded(self): return bool(self) # <6> """ Tombola.register(TomboList) # <- Python 3.2 or earlier """
def main(argv): verbose = '-v' in argv real_subclasses = Tombola.__subclasses__() virtual_subclasses = [ref() for ref in abc._get_dump(Tombola)[0] if ref()] for cls in real_subclasses + virtual_subclasses: test(cls, verbose)
def main(argv): verbose = '-v' in argv real_subclasses = Tombola.__subclasses__() # virtual_subclasses = list(Tombola._abc_registry) virtual_subclasses = [] for cls in real_subclasses + virtual_subclasses: test(cls, verbose)
def main(argv): verbose = '-v' in argv real_subclasses = Tombola.__subclasses__() # <2> 실제서브클래스 # virtual_subclasses = list(Tombola._dump_registry()) # <3> 가상서브클래스; 업데이트..필요 for cls in real_subclasses: # + virtual_subclasses: # <4> test(cls, verbose) # 모든 서브클래스를 테스트 해보자
def main(argv): verbose = '-v' in argv real_subclasses = Tombola.__subclasses__() virtual_subclasses = [tombolist.TomboList] for cls in real_subclasses + virtual_subclasses: test(cls, verbose)
def main(): real_sub = Tombola.__subclasses__() virtual_sub = [*Tombola._abc_registry] for cls in real_sub + virtual_sub: test(cls) return
def main(argv): verbose = '-v' in argv real_subclasses = Tombola.__subclasses__() virtual_subclasses = list(Tombola._abc_registry) for cls in real_subclasses + virtual_subclasses: test(cls, verbose)
def main2(): print('=' * 50) print(issubclass(TomboList, Tombola)) print(isinstance(TomboList(range(100)), Tombola)) print(TomboList.__mro__) # 데코레이터와 상속의 차이 print(Tombola.__subclasses__()) print(Tombola._abc_registry)
def main(argv): verbose = '-v' in argv real_subclasses = Tombola.__subclasses__( ) # __subclasses__() 返回的列表是内存中存在的直接子代 virtual_subclasses = list(Tombola._abc_registry) for cls in real_subclasses + virtual_subclasses: test(cls, verbose)
def main(argv): verbose = '-v in argv' # List direct descendents of Tomobala real_subclasses = Tombola.__subclasses__() #virtual_subclasses = list(Tombola._abc_registery) for cls in real_subclasses: do_test(cls, verbose=verbose)
def main(argv): verbose = '-v' in argv real_subclasses = Tombola.__subclasses__() # <2> # virtual_subclasses = list(Tombola._abc_registry) # <3> # virtual_subclasses = list(Tombola._abc_impl) # <3> # for cls in real_subclasses + virtual_subclasses: # <4> for cls in real_subclasses: # <4> test(cls, verbose)
def main(argv): verbose = '-v' in argv real_subclasses = Tombola.__subclasses__() # virtual_subclasses = list(Tombola.__abc_registry) virtual_subclasses = [ref() for ref in abc._get_dump(Tombola)[0] if ref()] print(real_subclasses + virtual_subclasses) for cls in real_subclasses + virtual_subclasses: test(cls, verbose)
def main(argv): verbose = '-v' in argv real_subclasses = Tombola.__subclasses__( ) # __subclasses__() lists the direct descendants that are alive in memory. That’s why we imported the modules to test, even if there is no further mention of them in the source code: to load the classes into memory. virtual_subclasses = list( Tombola._abc_registry ) # Build a list from _abc_registry (which is a WeakSet) so we can concatenate it with the result of __subclasses__(). for cls in real_subclasses + virtual_subclasses: # Iterate over the subclasses found, passing each to the test function. test(cls, verbose)
def main(argv): verbose = '-v' in argv # ❷ __subclasses__() 返回的列表是内存中存在的直接子代。 即便源码中用不到想测试的模块, 也要将其导入, 因为要把那些类载入内存 real_subclasses = Tombola.__subclasses__() # ❸ 把 _abc_registry(WeakSet 对象) 转换成列表, 这样方能与__subclasses__() 的结果拼接起来 virtual_subclasses = list(Tombola._abc_registry) # ❹ 迭代找到的各个子类, 分别传给 test 函数 for cls in real_subclasses + virtual_subclasses: test(cls, verbose)
def main(argv): verbose = '-v' in argv # __subclasses__() lists the direct descendants that are alive in memory. that's why we imported the modules to test, even if there is no further mention of them in the source code: to load the classes into memory real_subclasses = Tombola.__subclasses__() # print(real_subclasses) # Python3.7 does NOT support _abc_registry # virtual_subclasses = list(Tombola._abc_registry) # abc._get_dump(Tombola) --> returns a 4-tuple of (_abc_registry, _abc_cache, _abc_negative_cache, _abc_negative_cache_version) # index the _abc_registry in abc._get_dump and build a list so we can concatenate with the result of __subclasses__() virtual_subclasses = [ref() for ref in abc._get_dump(Tombola)[0] if ref()] # print(virtual_subclasses) # iterate over the subclasses found, passing each to the test function for cls in real_subclasses + virtual_subclasses: test(cls, verbose)
class TestarUsosBasicos(unittest.TestCase): def setUp(self): self.tombola = Tombola() def testar_carregada(self): self.assertFalse(self.tombola.carregada()) def testar_carregar_e_carregada(self): self.tombola.carregar('ABC') self.assertTrue(self.tombola.carregada()) def testar_sortear(self): itens = [1] self.tombola.carregar(itens) res = self.tombola.sortear() self.assertEqual(res, itens[0])
def setUp(self): self.tombola = Tombola()
def main(argv): verbose = '-v' in argv real_subclasses = Tombola.__subclasses__() for cls in real_subclasses: test(cls, verbose)
from tombola import Tombola TEST_FILE = 'tombola_tests.rst' MODULE_NAMES = 'bingo lotto tombolist drum'.split() TEST_MSG = '{0:16} {1.attempted:2} tests, {1.failed:2} failed - {2}' def test(cls, verbose=False): res = doctest.testfile(TEST_FILE, globs={'TombolaUnderTest': cls}, verbose=verbose, optionflags=doctest.REPORT_ONLY_FIRST_FAILURE) tag = 'FAIL' if res.failed else 'OK' print(TEST_MSG.format(cls.__name__, res, tag)) if __name__ == '__main__': for name in MODULE_NAMES: # import modules to test, by name importlib.import_module(name) verbose = '-v' in sys.argv real_subclasses = Tombola.__subclasses__() virtual_subclasses = list(Tombola._abc_registry) for cls in real_subclasses + virtual_subclasses: test(cls, verbose)