我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用pydoc.Helper()。
def test_DynamicClassAttribute(self): class Meta(type): def __getattr__(self, name): if name == 'ham': return 'spam' return super().__getattr__(name) class DA(metaclass=Meta): @types.DynamicClassAttribute def ham(self): return 'eggs' expected_text_data_docstrings = tuple('\n | ' + s if s else '' for s in expected_data_docstrings) output = StringIO() helper = pydoc.Helper(output=output) helper(DA) expected_text = expected_dynamicattribute_pattern % ( (__name__,) + expected_text_data_docstrings[:2]) result = output.getvalue().strip() if result != expected_text: print_diffs(expected_text, result) self.fail("outputs are not equal, see diff above")
def test_virtualClassAttributeWithOneMeta(self): class Meta(type): def __dir__(cls): return ['__class__', '__module__', '__name__', 'LIFE'] def __getattr__(self, name): if name =='LIFE': return 42 return super().__getattr(name) class Class(metaclass=Meta): pass output = StringIO() helper = pydoc.Helper(output=output) helper(Class) expected_text = expected_virtualattribute_pattern1 % __name__ result = output.getvalue().strip() if result != expected_text: print_diffs(expected_text, result) self.fail("outputs are not equal, see diff above")
def test_help_output_redirect(self): # issue 940286, if output is set in Helper, then all output from # Helper.help should be redirected old_pattern = expected_text_pattern getpager_old = pydoc.getpager getpager_new = lambda: (lambda x: x) self.maxDiff = None buf = StringIO() helper = pydoc.Helper(output=buf) unused, doc_loc = get_pydoc_text(pydoc_mod) module = "test.pydoc_mod" help_header = """ Help on module test.pydoc_mod in test: """.lstrip() help_header = textwrap.dedent(help_header) expected_help_pattern = help_header + expected_text_pattern pydoc.getpager = getpager_new try: with captured_output('stdout') as output, \ captured_output('stderr') as err: helper.help(module) result = buf.getvalue().strip() expected_text = expected_help_pattern % \ (doc_loc, inspect.getabsfile(pydoc_mod)) self.assertEqual('', output.getvalue()) self.assertEqual('', err.getvalue()) self.assertEqual(expected_text, result) finally: pydoc.getpager = getpager_old
def test_keywords(self): self.assertEqual(sorted(pydoc.Helper.keywords), sorted(keyword.kwlist))
def test_help_output_redirect(self): # issue 940286, if output is set in Helper, then all output from # Helper.help should be redirected old_pattern = expected_text_pattern getpager_old = pydoc.getpager getpager_new = lambda: (lambda x: x) self.maxDiff = None buf = StringIO() helper = pydoc.Helper(output=buf) unused, doc_loc = get_pydoc_text(pydoc_mod) module = "test.pydoc_mod" help_header = """ Help on module test.pydoc_mod in test: """.lstrip() help_header = textwrap.dedent(help_header) expected_help_pattern = help_header + expected_text_pattern pydoc.getpager = getpager_new try: with captured_output('stdout') as output, \ captured_output('stderr') as err: helper.help(module) result = buf.getvalue().strip() expected_text = expected_help_pattern % ( (doc_loc,) + expected_text_data_docstrings + (inspect.getabsfile(pydoc_mod),)) self.assertEqual('', output.getvalue()) self.assertEqual('', err.getvalue()) self.assertEqual(expected_text, result) finally: pydoc.getpager = getpager_old
def test_pydoc(self): # indirectly test __objclass__ expected_text = expected_help_output % __name__ output = StringIO() helper = pydoc.Helper(output=output) helper(self.Color) result = output.getvalue().strip() self.assertEqual(result, expected_text)
def test_modules(self): # See Helper.listmodules(). num_header_lines = 2 num_module_lines_min = 5 # Playing it safe. num_footer_lines = 3 expected = num_header_lines + num_module_lines_min + num_footer_lines output = StringIO() helper = pydoc.Helper(output=output) helper('modules') result = output.getvalue().strip() num_lines = len(result.splitlines()) self.assertGreaterEqual(num_lines, expected)
def test_modules_search(self): # See Helper.listmodules(). expected = 'pydoc - ' output = StringIO() helper = pydoc.Helper(output=output) with captured_stdout() as help_io: helper('modules pydoc') result = help_io.getvalue() self.assertIn(expected, result)
def test_modules_search_builtin(self): expected = 'gc - ' output = StringIO() helper = pydoc.Helper(output=output) with captured_stdout() as help_io: helper('modules garbage') result = help_io.getvalue() self.assertTrue(result.startswith(expected))
def test_buggy_dir(self): class M(type): def __dir__(cls): return ['__class__', '__name__', 'missing', 'here'] class C(metaclass=M): here = 'present!' output = StringIO() helper = pydoc.Helper(output=output) helper(C) expected_text = expected_missingattribute_pattern % __name__ result = output.getvalue().strip() if result != expected_text: print_diffs(expected_text, result) self.fail("outputs are not equal, see diff above")
def help(self, request): if hasattr(request, 'i_am_a_casapy_task'): pydoc.pager('Help on ' + pydoc.text.bold(request.__name__) + ' task:\n\n' + request.__doc__) else: return pydoc.Helper.help(self, request)
def test_virtualClassAttributeWithTwoMeta(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 fail1 = fail2 = False output = StringIO() helper = pydoc.Helper(output=output) helper(Class1) expected_text1 = expected_virtualattribute_pattern2 % __name__ result1 = output.getvalue().strip() if result1 != expected_text1: print_diffs(expected_text1, result1) fail1 = True output = StringIO() helper = pydoc.Helper(output=output) helper(Class2) expected_text2 = expected_virtualattribute_pattern3 % __name__ result2 = output.getvalue().strip() if result2 != expected_text2: print_diffs(expected_text2, result2) fail2 = True if fail1 or fail2: self.fail("outputs are not equal, see diff above")