def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
return result and re.sub('^ *\n', '', rstrip(result)) or ''
python类getcomments()的实例源码
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
result = _encode(result)
return result and re.sub('^ *\n', '', rstrip(result)) or ''
def get_doc(obj):
"""Get the doc string or comments for an object.
:param object: object
:returns: doc string
:rtype: str
>>> get_doc(abs)
'abs(number) -> number\\n\\nReturn the absolute value of the argument.'
"""
result = inspect.getdoc(obj) or inspect.getcomments(obj)
return result and RE_EMPTY_LINE.sub('', result.rstrip()) or ''
def test_getcomments(self):
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
return result and re.sub('^ *\n', '', result.rstrip()) or ''
def test_getcomments(self):
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
result = _encode(result)
return result and re.sub('^ *\n', '', rstrip(result)) or ''
def test_getcomments(self):
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
result = _encode(result)
return result and re.sub('^ *\n', '', rstrip(result)) or ''
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
result = _encode(result)
return result and re.sub('^ *\n', '', rstrip(result)) or ''
def test_getcomments(self):
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
return result and re.sub('^ *\n', '', result.rstrip()) or ''
def test_getcomments(self):
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
result = _encode(result)
return result and re.sub('^ *\n', '', rstrip(result)) or ''
def test_getcomments(self):
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
return result and re.sub('^ *\n', '', result.rstrip()) or ''
def test_getcomments(self):
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
return result and re.sub('^ *\n', '', rstrip(result)) or ''
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
result = _encode(result)
return result and re.sub('^ *\n', '', rstrip(result)) or ''
def test_getcomments(self):
self.assertEqual(inspect.getcomments(mod), '# line 1\n')
self.assertEqual(inspect.getcomments(mod.StupidGit), '# line 20\n')
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
return result and re.sub('^ *\n', '', result.rstrip()) or ''
def getdoc(object):
"""Get the doc string or comments for an object."""
result = inspect.getdoc(object) or inspect.getcomments(object)
result = _encode(result)
return result and re.sub('^ *\n', '', rstrip(result)) or ''
def class2md(self, cls, depth=2):
"""Takes a class and creates markdown text to document its methods and variables.
"""
section = "#" * depth
subsection = "#" * (depth + 2)
clsname = cls.__name__
modname = cls.__module__
header = clsname
path = self.get_src_path(cls)
doc = self.doc2md(cls)
try:
init = self.func2md(cls.__init__, clsname=clsname)
except (ValueError, TypeError):
# this happens if __init__ is outside the repo
init = ""
variables = []
for name, obj in getmembers(cls, lambda a: not (inspect.isroutine(a) or inspect.ismethod(a))):
if not name.startswith("_") and type(obj) == property:
comments = self.doc2md(obj) or inspect.getcomments(obj)
comments = "\n %s" % comments if comments else ""
variables.append("\n%s %s.%s%s\n" % (subsection, clsname, name, comments))
handlers = []
for name, obj in getmembers(cls, inspect.ismethoddescriptor):
if not name.startswith("_") and hasattr(obj, "__module__") and obj.__module__ == modname:
handlers.append("\n%s %s.%s\n *Handler*" % (subsection, clsname, name))
methods = []
for name, obj in getmembers(cls, inspect.isfunction):
if not name.startswith("_") and hasattr(obj,
"__module__") and obj.__module__ == modname and name not in handlers:
methods.append(self.func2md(obj, clsname=clsname, depth=depth + 1))
string = CLASS_TEMPLATE.format(section=section,
header=header,
path=path,
doc=doc if doc else "",
init=init,
variables="".join(variables),
handlers="".join(handlers),
methods="".join(methods))
return string
def module2md(self, module):
"""Takes an imported module object and create a Markdown string containing functions and classes.
"""
modname = module.__name__
path = self.get_src_path(module, append_base=False)
path = "[{}]({})".format(path, os.path.join(self.github_link, path))
found = []
classes = []
line_nos = []
for name, obj in getmembers(module, inspect.isclass):
# handle classes
found.append(name)
if not name.startswith("_") and hasattr(obj, "__module__") and obj.__module__ == modname:
classes.append(self.class2md(obj))
line_nos.append(self.get_line_no(obj) or 0)
classes = order_by_line_nos(classes, line_nos)
functions = []
line_nos = []
for name, obj in getmembers(module, inspect.isfunction):
# handle functions
found.append(name)
if not name.startswith("_") and hasattr(obj, "__module__") and obj.__module__ == modname:
functions.append(self.func2md(obj))
line_nos.append(self.get_line_no(obj) or 0)
functions = order_by_line_nos(functions, line_nos)
variables = []
line_nos = []
for name, obj in module.__dict__.items():
if not name.startswith("_") and name not in found:
if hasattr(obj, "__module__") and obj.__module__ != modname:
continue
if hasattr(obj, "__name__") and not obj.__name__.startswith(modname):
continue
comments = inspect.getcomments(obj)
comments = ": %s" % comments if comments else ""
variables.append("- **%s**%s" % (name, comments))
line_nos.append(self.get_line_no(obj) or 0)
variables = order_by_line_nos(variables, line_nos)
if variables:
new_list = ["**Global Variables**", "---------------"]
new_list.extend(variables)
variables = new_list
string = MODULE_TEMPLATE.format(path=path,
global_vars="\n".join(variables) if variables else "",
functions="\n".join(functions) if functions else "",
classes="".join(classes) if classes else "")
return string
def class2md(self, cls, depth=2):
"""Takes a class and creates markdown text to document its methods and variables.
"""
section = "#" * depth
subsection = "#" * (depth + 2)
clsname = cls.__name__
modname = cls.__module__
header = clsname
path = self.get_src_path(cls)
doc = self.doc2md(cls)
try:
init = self.func2md(cls.__init__, clsname=clsname)
except (ValueError, TypeError):
# this happens if __init__ is outside the repo
init = ""
variables = []
for name, obj in getmembers(cls, lambda a: not (inspect.isroutine(a) or inspect.ismethod(a))):
if not name.startswith("_") and type(obj) == property:
comments = self.doc2md(obj) or inspect.getcomments(obj)
comments = "\n %s" % comments if comments else ""
variables.append("\n%s %s.%s%s\n" % (subsection, clsname, name, comments))
handlers = []
for name, obj in getmembers(cls, inspect.ismethoddescriptor):
if not name.startswith("_") and hasattr(obj, "__module__") and obj.__module__ == modname:
handlers.append("\n%s %s.%s\n *Handler*" % (subsection, clsname, name))
methods = []
for name, obj in getmembers(cls, inspect.ismethod):
if not name.startswith("_") and hasattr(obj,
"__module__") and obj.__module__ == modname and name not in handlers:
methods.append(self.func2md(obj, clsname=clsname, depth=depth + 1))
string = CLASS_TEMPLATE.format(section=section,
header=header,
path=path,
doc=doc if doc else "",
init=init,
variables="".join(variables),
handlers="".join(handlers),
methods="".join(methods))
return string