当前位置: 首页>>代码示例>>Python>>正文


Python ast.get_docstring方法代码示例

本文整理汇总了Python中ast.get_docstring方法的典型用法代码示例。如果您正苦于以下问题:Python ast.get_docstring方法的具体用法?Python ast.get_docstring怎么用?Python ast.get_docstring使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ast的用法示例。


在下文中一共展示了ast.get_docstring方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_multiple_line_item_definition_cyk

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def test_multiple_line_item_definition_cyk(self):
        """Make sure item definitions can span multiple lines."""
        func = '\n'.join([
            'def do_nothing(x):',
            '    """Do nothing with x.',
            '    ',
            '    :param x: This is an argument which must be ',
            '        qualified by a large amount of text.',
            '',
            '    """',
            '    pass',
        ])
        doc = ast.get_docstring(ast.parse(func).body[0])
        node = parse(condense(lex(doc)))
        self.assertTrue(
            CykNodeUtils.contains(node, 'arguments-section'),
            node,
        ) 
开发者ID:terrencepreilly,项目名称:darglint,代码行数:20,代码来源:test_sphinx_parser.py

示例2: test_parser_sections_correctly

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def test_parser_sections_correctly(self):
        program = '\n'.join([
            'def func(x, l):',
            '    """Add an item to the head of the list.',
            '    ',
            '    :param x: The item to add to the list.',
            '    :return: The list with the item attached.',
            '    ',
            '    """',
            '    return l.appendleft(x)',
        ])
        doc = ast.get_docstring(ast.parse(program).body[0])
        tokens = condense(lex(doc))
        node = parse(tokens)
        self.assertTrue(
            CykNodeUtils.contains(node, 'returns-section'),
        )
        self.assertTrue(
            CykNodeUtils.contains(node, 'arguments-section'),
        ) 
开发者ID:terrencepreilly,项目名称:darglint,代码行数:22,代码来源:test_sphinx_parser.py

示例3: test_parse_noqa_for_argument

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def test_parse_noqa_for_argument(self):
        func = '\n'.join([
            'def my_function():',
            '    """Has an extra argument, but thats okay.',
            '',
            '    Args:',
            '        arg1: This will be defined very soon.  # noqa: I102',
            '',
            '    """',
            '    print("Not done yet!")',
        ])
        doc = ast.get_docstring(ast.parse(func).body[0])
        self.assertTrue(doc.startswith('Has an extra'))
        node = parse(condense(lex(doc)))
        self.assertTrue(
            CykNodeUtils.contains(node, 'noqa'),
        )
        noqa = self.get_identifier(node, NoqaIdentifier)
        self.assertTrue(
            noqa is not None,
        )
        self.assertEqual(
            NoqaIdentifier.extract(noqa),
            'I102',
        ) 
开发者ID:terrencepreilly,项目名称:darglint,代码行数:27,代码来源:test_parser.py

示例4: test_parses_long_description

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def test_parses_long_description(self):
        func = '\n'.join([
            'def this_function_has_a_long_description(arg1):',
            '    """Return the arg, unchanged.',
            '',
            '    This function returns the arg, unchanged.  There is',
            '    no particular reason, but this is a good place to check to ',
            '    see that long descriptions are being parsed correctly. ',
            '    If they are, I\'m not sure why.  There is some magic ',
            '    going on here, in fact.',
            '',
            '    """',
            '    return arg1',
        ])
        doc = ast.get_docstring(ast.parse(func).body[0])
        tokens = list(lex(doc))
        node = parse(tokens)
        self.assertTrue(node is not None) 
开发者ID:terrencepreilly,项目名称:darglint,代码行数:20,代码来源:test_parser.py

示例5: test_parse_long_description_multiple_sections

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def test_parse_long_description_multiple_sections(self):
        func = '\n'.join([
            'def this_function_has_multiple_long_descriptions():',
            '    """Do some math.',
            '',
            '    This is the first part of the long description.',
            '    it can be multiple lines, but doesn\'t have to be',
            '',
            '    This is the second half of the long description.',
            '',
            '    And the final part of it.',
            '',
            '    """',
            '    pass',
        ])
        doc = ast.get_docstring(ast.parse(func).body[0])
        tokens = list(lex(doc))
        node = parse(tokens)
        self.assertTrue(node is not None) 
开发者ID:terrencepreilly,项目名称:darglint,代码行数:21,代码来源:test_parser.py

示例6: get_func_comments

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def get_func_comments(function_definitions):
    doc = ''
    for f in function_definitions:
        temp_str = to_md(parse_func_string(ast.get_docstring(f)))
        doc += ''.join(
            [
                '### ',
                f.name.replace('_', '\\_'),
                '\n',
                '```python',
                '\n',
                'def ',
                f.name,
                parse_func_args(f),
                '\n',
                '```',
                '\n',
                temp_str,
                '\n',
            ]
        )

    return doc 
开发者ID:idealo,项目名称:image-quality-assessment,代码行数:25,代码来源:autogen.py

示例7: get_comments_str

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def get_comments_str(file_name):
    with open(file_name) as fd:
        file_contents = fd.read()
    module = ast.parse(file_contents)

    function_definitions = [node for node in module.body if isinstance(node, ast.FunctionDef)]

    doc = get_func_comments(function_definitions)

    class_definitions = [node for node in module.body if isinstance(node, ast.ClassDef)]
    for class_def in class_definitions:
        temp_str = to_md(parse_func_string(ast.get_docstring(class_def)))

        # excludes private methods (start with '_')
        method_definitions = [
            node
            for node in class_def.body
            if isinstance(node, ast.FunctionDef) and (node.name[0] != '_' or node.name[:2] == '__')
        ]

        temp_str += get_func_comments(method_definitions)
        doc += '## class ' + class_def.name + '\n' + temp_str
    return doc 
开发者ID:idealo,项目名称:image-quality-assessment,代码行数:25,代码来源:autogen.py

示例8: parse_class

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def parse_class(node):
    """
    parse_class parses the given node representing a test class for example
    test cases and puts everything into a Section object.
    """
    name = node.name[4:]
    title, details = parse_docstring(ast.get_docstring(node))
    examples = []

    for n in node.body:
        if isinstance(n, _ast.FunctionDef) and n.name.startswith('test_'):
            example = parse_function(n)
            examples.append(example._replace(
                name='{}__{}'.format(name, example.name)))

    return Section(name, title, details, examples) 
开发者ID:ulope,项目名称:pyformat.info,代码行数:18,代码来源:main.py

示例9: get_actor_io_ids

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def get_actor_io_ids(filename):
    '''
    Parse IMAS Python actor script and return actor input and output IDSs

    :param filename: filename of the IMAS Python actor

    :return: tuple with list of input IDSs and output IDSs
    '''
    import ast
    with open(filename, 'r') as f:
        module = ast.parse(f.read())
    actor = os.path.splitext(os.path.split(filename)[-1])[0]
    function_definitions = [node for node in module.body if isinstance(node, ast.FunctionDef)]
    docstring = ast.get_docstring([f for f in function_definitions if f.name == actor][0])
    ids_in = []
    ids_out = []
    for line in docstring.split('\n'):
        if 'codeparams' in line:
            pass
        elif line.strip().startswith(':param result:'):
            ids_out = list(map(lambda x: x.strip()[:-1], line.split(':')[2].strip(', ').split(',')))
            break
        elif line.strip().startswith(':param '):
            ids_in.append(line.split(':')[2].strip())
    return ids_in, ids_out 
开发者ID:gafusion,项目名称:omas,代码行数:27,代码来源:omas_utils.py

示例10: visit_ClassDef

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def visit_ClassDef(self, tree):
        bases = (T(', ').join(map(self.visit, tree.bases)) +
                 ','*(len(tree.bases) == 1))
        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, after=T('{__l}'))
        doc = ast.get_docstring(tree, clean=False)
        body = self.close(ns, "{{'__module__': __name__{}}}".format(
            '' if doc is None else ", '__doc__': {!r}".format(doc)), body)
        if tree.bases:
            class_code = T("(lambda b, d: d.get('__metaclass__', getattr(b[0], "
                           "'__class__', type(b[0])))({!r}, b, d))(({}), "
                           "{})").format(tree.name, bases, body)
        else:
            class_code = T("(lambda d: d.get('__metaclass__', {__g}.get("
                           "'__metaclass__', {__types}.ClassType))({!r}, (), "
                           "d))({})").format(tree.name, body)
        class_code = decoration.format(class_code)
        return assignment_component(T('{after}'), self.store_var(tree.name), class_code) 
开发者ID:csvoss,项目名称:onelinerizer,代码行数:23,代码来源:onelinerizer.py

示例11: get_docstring_and_version_via_ast

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def get_docstring_and_version_via_ast(target):
    """
    Return a tuple like (docstring, version) for the given module,
    extracted by parsing its AST.
    """
    # read as bytes to enable custom encodings
    with target.file.open('rb') as f:
        node = ast.parse(f.read())
    for child in node.body:
        # Only use the version from the given module if it's a simple
        # string assignment to __version__
        is_version_str = (isinstance(child, ast.Assign) and
                          len(child.targets) == 1 and
                          child.targets[0].id == "__version__" and
                          isinstance(child.value, ast.Str))
        if is_version_str:
            version = child.value.s
            break
    else:
        version = None
    return ast.get_docstring(node), version 
开发者ID:holzschu,项目名称:Carnets,代码行数:23,代码来源:common.py

示例12: parse_class

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def parse_class(class_ast, relpath, imports):
  """Parses a class AST object.

  Args:
    * class_ast (ast.ClassDef) - The class definition to parse.
    * relpath (str) - The posix-style relative path which should be associated
      with the code in class_ast.
    * imports (Dict[str, object]) - The objects which should be available while
      evaluating class_ast.

  Returns Doc.Class proto message.
  """
  assert isinstance(class_ast, ast.ClassDef), type(class_ast)

  classes, funcs = _extract_classes_funcs(class_ast, relpath, imports, False)

  ret = doc.Doc.Class(
    relpath=relpath,
    name=class_ast.name,
    docstring=ast.get_docstring(class_ast) or '',
    lineno=class_ast.lineno,
    classes=classes,
    funcs=funcs,
  )

  for b in class_ast.bases:
    item = _apply_imports_to_unparsed_expression(b, imports)
    if isinstance(item, str):
      ret.bases.add(generic=item)
    else:
      ret.bases.add(known=item.__module__+'.'+item.__name__)

  return ret 
开发者ID:luci,项目名称:recipes-py,代码行数:35,代码来源:cmd.py

示例13: parse_func

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def parse_func(func_ast, relpath, imports):
  """Parses a function into a Doc.Func.

  Args:
    * func_ast (ast.FunctionDef) - The function to parse.
    * relpath (str) - The posix-style relative path which should be associated
      with the code in func_ast.
    * imports (Dict[str, object]) - The symbols to include during the evaluation
      of `func_ast`.

  Returns Doc.Func.
  """
  assert isinstance(func_ast, ast.FunctionDef), type(func_ast)
  ret = doc.Doc.Func(
    name=func_ast.name,
    relpath=relpath,
    lineno=func_ast.lineno,
    docstring=ast.get_docstring(func_ast) or '',
  )

  for exp in func_ast.decorator_list:
    item = _apply_imports_to_unparsed_expression(exp, imports)
    if isinstance(item, str):
      ret.decorators.add(generic=item)
    else:
      ret.decorators.add(known=item.__module__+'.'+item.__name__)

  ret.signature = _unparse(func_ast.args).strip()
  return ret 
开发者ID:luci,项目名称:recipes-py,代码行数:31,代码来源:cmd.py

示例14: parse_recipe

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def parse_recipe(recipe):
  """Parses a recipe object into a Doc.Recipe.

  Args:
    * recipe (Recipe) - The recipe object to parse.

  Returns Doc.Recipe.
  """
  assert isinstance(recipe, Recipe), type(recipe)
  relpath = _to_posix(recipe.relpath)

  recipe_ast = _grab_ast(recipe.repo, recipe.path)
  if not recipe_ast:
    return None
  classes, funcs = _extract_classes_funcs(recipe_ast, relpath,
                                          MOCK_IMPORTS_RECIPE)
  funcs.pop('GenTests', None)

  return doc.Doc.Recipe(
    name=recipe.name,
    relpath=relpath,
    docstring=ast.get_docstring(recipe_ast) or '',
    deps=parse_deps(recipe.repo.name, recipe_ast, relpath),
    parameters=parse_parameters(recipe_ast, relpath),
    classes=classes,
    funcs=funcs,
  ) 
开发者ID:luci,项目名称:recipes-py,代码行数:29,代码来源:cmd.py

示例15: extra_docstrings

# 需要导入模块: import ast [as 别名]
# 或者: from ast import get_docstring [as 别名]
def extra_docstrings(
    project_folder,
    extra_dockstrings_paths_to_ignore,
    functions_with_docstrings_percent_limit,
    *args,
    **kwargs
):
    for parsed_file in project_folder.get_parsed_py_files(whitelist=extra_dockstrings_paths_to_ignore):
        defs = ast_helpers.get_nodes_of_type(parsed_file.ast_tree, ast.FunctionDef)
        if not defs:
            continue

        docstrings = [ast.get_docstring(d) for d in defs if ast.get_docstring(d) is not None]
        if len(docstrings) / len(defs) * 100 > functions_with_docstrings_percent_limit:
            return parsed_file.name 
开发者ID:devmanorg,项目名称:fiasko_bro,代码行数:17,代码来源:comments.py


注:本文中的ast.get_docstring方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。