當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。