本文整理汇总了Python中ast.get_docstring函数的典型用法代码示例。如果您正苦于以下问题:Python get_docstring函数的具体用法?Python get_docstring怎么用?Python get_docstring使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_docstring函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _parse_docstring
def _parse_docstring(self):
"""Parses the test docstring extracting expected values.
If the expected tags is not spelled right they will not be parsed.
"""
if self.docstring is None:
return
# Create the contexts
tags, unexpected_tags, _ = self._parse_tags(
ast.get_docstring(self.module_def))
class_tags, class_unexpected_tags, _ = self._parse_tags(
ast.get_docstring(self.parent_class_def))
function_tags, function_unexpected_tags, self.skipped_lines = (
self._parse_tags(self.docstring))
# Update context dictionaries
tags.update(class_tags)
tags.update(function_tags)
unexpected_tags.update(class_unexpected_tags)
unexpected_tags.update(function_unexpected_tags)
for tag, value in tags.items():
if tag == 'bz':
tag = 'bugs'
if tag == 'assert':
tag = 'assertion'
if tag == 'type':
tag = 'test_type'
setattr(self, tag, value)
self.unexpected_tags = unexpected_tags
# Always use the first line of docstring as test case name
if self.test is None:
self.test = self.docstring.strip().split('\n')[0]
示例2: extract_info
def extract_info(self):
for node in self.module.body:
if isinstance(node, ast.ClassDef):
yield {
"name": node.name,
"lineno": node.lineno,
"docstring": ast.get_docstring(node),
"type": 'class',
}
for sub_node in node.body:
if isinstance(sub_node, ast.FunctionDef):
yield {
"name": sub_node.name,
"lineno": sub_node.lineno,
"docstring": ast.get_docstring(sub_node),
"type": 'attribute',
"args": [arg.id for arg in sub_node.args.args],
"header": ''
}
elif isinstance(node, ast.FunctionDef):
yield {
"name": node.name,
"lineno": node.lineno,
"docstring": ast.get_docstring(node),
"type": 'function',
"args": [arg.id for arg in node.args.args],
}
示例3: test_get_docstring
def test_get_docstring(self):
node = ast.parse('def foo():\n """line one\n line two"""')
self.assertEqual(ast.get_docstring(node.body[0]),
'line one\nline two')
node = ast.parse('async def foo():\n """spam\n ham"""')
self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
示例4: __init__
def __init__(self, function_def, parent_class=None, testmodule=None):
"""Wrap a ``ast.FunctionDef`` instance used to extract information."""
self.docstring = ast.get_docstring(function_def)
self.function_def = function_def
self.name = function_def.name
if parent_class:
self.parent_class = parent_class.name
self.parent_class_def = parent_class
self.class_docstring = ast.get_docstring(self.parent_class_def)
else:
self.parent_class = None
self.parent_class_def = None
self.class_docstring = None
self.testmodule = testmodule.path
self.module_def = testmodule
self.module_docstring = ast.get_docstring(self.module_def)
self.pkginit = os.path.join(
os.path.dirname(self.testmodule), '__init__.py')
if os.path.exists(self.pkginit):
self.pkginit_def = ast.parse(''.join(open(self.pkginit)))
self.pkginit_docstring = ast.get_docstring(self.pkginit_def)
else:
self.pkginit_def = None
self.pkginit_docstring = None
self.tokens = {}
self.invalid_tokens = {}
self._rst_parser_messages = []
self.parser = DocstringParser(
SETTINGS.get('tokens'),
SETTINGS.get('minimum_tokens'),
)
self._parse_docstring()
示例5: collect_docstrings
def collect_docstrings(self):
"""Collect list of paths + classes + methods/functions, and docstrings.
This function assumes that there are no classes or methods/functions
nested within others.
"""
self.node_sequence = []
# Module-level docstrings.
self.node_sequence.append([self.path, ast.get_docstring(self.module)])
# Class-level doc-strings
# Function-level doc-strings
for class_def in self.class_defs:
for node in class_def.body:
if isinstance(node, ast.ClassDef):
self.node_sequence.append(
[self.path + '.' + node.name,
ast.get_docstring(node)]
)
elif isinstance(node, ast.FunctionDef):
if self.tests_only and node.name[:5] != 'test_':
continue
self.node_sequence.append(
[self.path + '.' + class_def.name + '.' + node.name,
ast.get_docstring(node)]
)
for func_def in self.func_defs:
if isinstance(func_def, ast.FunctionDef):
if self.tests_only and func_def.name[:5] != 'test_':
continue
self.node_sequence.append(
[self.path + '.' + func_def.name,
ast.get_docstring(func_def)]
)
示例6: parse_module
def parse_module(file_path):
module = ast.parse(file_path.read_text())
tmpl_str = HEADER.format(file_path.name.rsplit('.',1)[0])
cls_defs = [node for node in module.body if isinstance(node, ast.ClassDef)]
mod_func_defs = [node for node in module.body if isinstance(node, ast.FunctionDef)]
for cls_def in cls_defs:
cls_name = cls_def.name
cls_bases = ','.join([parse(each) for each in cls_def.bases])
tmpl_str += f'== {{{{class {cls_name}{":" + cls_bases if cls_bases else ""}}}}}\n\n'
method_str = None
for fn_def in (fn_def for fn_def in cls_def.body if isinstance(fn_def, ast.FunctionDef)):
if fn_def.name == '__init__':
tmpl_str += "=== Arguments\n" + parse_args(fn_def.args) + "\n\n"
else:
if not method_str:
method_str = '=== Methods\n\n'
doc_str = ast.get_docstring(fn_def)
method_str += f'{{{{method {fn_def.name},{doc_str if doc_str else ""}}}}}\n\n'
tmpl_str += method_str if method_str else ''
method_str = None
for fn_def in mod_func_defs:
if not method_str:
method_str = '== Module Functions\n\n'
doc_str = ast.get_docstring(fn_def)
method_str += f'{{{{method {fn_def.name},{doc_str if doc_str else ""}}}}}\n\n'
tmpl_str += method_str if method_str else ''
return tmpl_str
示例7: get_doc
def get_doc(self, module):
for node in module.body:
if isinstance(node, ast.ClassDef):
yield ast.get_docstring(node)
for sub_node in node.body:
if isinstance(sub_node, ast.FunctionDef):
yield ast.get_docstring(sub_node)
示例8: _getMarvinTestDocStrings
def _getMarvinTestDocStrings(classname, testnames, marvinCodePath):
pathToClass = os.path.join(marvinCodePath, *classname.split('.')[1:-1])+'.py'
astData = ast.parse(open(pathToClass).read())
classElement = filter(lambda x:isinstance(x, ast.ClassDef) and x.name == classname.split('.')[-1], astData.body)[0]
classDocString = ast.get_docstring(classElement)
classDocString = classDocString and classDocString.rstrip() or ''
testMethodElements = filter(lambda x:isinstance(x, ast.FunctionDef) and x.name in testnames, classElement.body)
testMethodDocStrings = []
for testMethod in testMethodElements:
docStr = ast.get_docstring(testMethod)
docStr = docStr and docStr.rstrip() or ''
testMethodDocStrings.append((testMethod.name, docStr))
return (classDocString, testMethodDocStrings)
示例9: visit_FunctionDef
def visit_FunctionDef(self, node, **kwargs):
"""
Handles function definitions within code.
Process a function's docstring, keeping well aware of the function's
context and whether or not it's part of an interface definition.
"""
if self.options.debug:
stderr.write("# Function {0.name}{1}".format(node, linesep))
# Push either 'interface' or 'class' onto our containing nodes
# hierarchy so we can keep track of context. This will let us tell
# if a function is nested within another function or even if a class
# is nested within a function.
containingNodes = kwargs.get('containingNodes', []) or []
containingNodes.append((node.name, 'function'))
if self.options.topLevelNamespace:
fullPathNamespace = self._getFullPathName(containingNodes)
contextTag = '.'.join(pathTuple[0] for pathTuple in fullPathNamespace)
modifiedContextTag = self._processMembers(node, contextTag)
tail = '@namespace {0}'.format(modifiedContextTag)
else:
tail = self._processMembers(node, '')
if get_docstring(node):
self._processDocstring(node, tail,
containingNodes=containingNodes)
# Visit any contained nodes.
self.generic_visit(node, containingNodes=containingNodes)
# Remove the item we pushed onto the containing nodes hierarchy.
containingNodes.pop()
示例10: _get_file_content
def _get_file_content(realpath):
"""Helper function to turn a file into HTML"""
result = """<h2>Module: %s</h2>
<div class="alert alert-info">
<div style = "details">path: %s</div>
<div style = "details">created: %s, last modified: %s</div></div>""" %(
os.path.basename(realpath),
urlparse.unquote(_convert_path_to_url(realpath)),
time.ctime(os.path.getmtime(realpath)),
time.ctime(os.path.getctime(realpath))
)
with open(realpath) as sourcefile:
code = sourcefile.readlines()
abstract_syntax_tree = ast.parse(''.join(code))
description = ast.get_docstring(abstract_syntax_tree)
if description:
result += "<h3>Summary:</h3> <div style = 'summary'>%s</div>" % _convert_str_to_html(description)
visitor = DocVisitor()
visitor.visit(abstract_syntax_tree)
parsed_code = visitor.get_doc()
entries = [ parsed_code[key] for key in sorted(parsed_code.keys())]
for entry in entries:
begin, end = entry.get("lines")
result += '<hr /><div><pre>' + "".join(code[begin:end]).rstrip().rstrip(":") +"</pre>"
result += _convert_str_to_html(entry.get("description"))+"</div>"
return result
示例11: header
def header():
'''
displays current module docstring
'''
f=inspect.stack()[1][1]
m=ast.parse(''.join(open(f)))
print "\n%s" % ast.get_docstring(m)
示例12: parse_tree
def parse_tree (self, name, node):
"""
Recursive function that explores the python parse tree and
constructs our TreeNode datastructure to represent a document.
Takes the root node of the tree provided by the std ast module and
the file name.
"""
logging.debug('Exploring node %s of %s' % (node, name))
tree_node = ParserNode(
type = type_lookup[type(node)],
name = node.name if 'name' in node.__dict__ else \
name.split('.')[0],
docstring = ast.get_docstring(node),
children = [],
terms = []
)
for child in node.body:
# We only want to look for modules, functions and classes as
# these are the only items that have doc strings and therefore
# terms at the moment. Later on, we'll want to use imports and
# function names and other elements of the parse tree.
if type(child) not in [ast.Module, ast.FunctionDef, ast.ClassDef]:
continue
tree_node.children.append(self.parse_tree(name, child))
return tree_node
示例13: scripts2rst
def scripts2rst(path, f):
""" creates rst summary documentation for files in scripts folder
which is not a package so cannot be imported """
# get list of script files
try:
files = [name for name in os.listdir(path)
if not name.startswith("_")
and name.endswith(".py")]
except:
return
f.write("**Scripts**\n\n")
# loop over script files
for name in files:
sfile = os.path.join(path, name)
try:
try:
source = ast.parse(open(sfile, "r", encoding="utf8").read())
except:
#py2 fails if encoding in string
source = ast.parse(open(sfile, "r").read())
except Exception as e:
log.warning("Problem parsing %s\n%s"%(name, e))
f.write(name+"\n")
doc = i.cleandoc(ast.get_docstring(source)) or "."
doc = py2decode(doc) #py2
f.write(" "+doc.splitlines()[0]+"\n")
f.write("\n")
示例14: visit_FunctionDef
def visit_FunctionDef(self, tree):
# self.visit() returns something of the form
# ('lambda x, y, z=5, *args: ', ['x', 'y', 'z', 'args'])
args, arg_names = self.visit(tree.args)
decoration = T('{}')
for decorator in tree.decorator_list:
decoration = decoration.format(T('{}({})').format(self.visit(decorator), T('{}')))
ns = self.next_child()
body = ns.many_to_one(tree.body).format(pre_return='', post_return='')
if arg_names:
body = assignment_component(body,
T(', ').join(ns.var(name) for name in arg_names),
T(', ').join(arg_names))
body = self.close(ns, '{}', body)
function_code = args + body
doc = ast.get_docstring(tree, clean=False)
if tree.decorator_list:
return assignment_component(
T('{after}'),
self.store_var(tree.name),
decoration.format(assignment_component(
'__func',
'__func, __func.__name__' + ('' if doc is None else ', __func.__doc__'),
T('{}, {!r}' + ('' if doc is None else ', {!r}')).format(
function_code, tree.name, doc))))
else:
return assignment_component(
T('{after}'),
T('{}, {}.__name__' + ('' if doc is None else ', {}.__doc__')).format(
self.store_var(tree.name), self.var(tree.name), self.var(tree.name)),
T('{}, {!r}' + ('' if doc is None else ', {!r}')).format(
function_code, tree.name, doc))
示例15: shortdesc
def shortdesc():
filename = os.path.join(PACKAGE, '__init__.py')
data = Setup.read(filename)
node = ast.parse(data, filename)
docstring = ast.get_docstring(node)
desc = docstring.strip().split('\n\n', 1)[0]
return desc.replace('\n', ' ')