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


Python ast.iter_child_nodes函数代码示例

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


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

示例1: _iter_member_names

    def _iter_member_names(self):
        '''
        iterate over assign and def member names, including class bases
        preserves order (bases members goes first)
        '''
        all_bases_names = set(itertools.chain(*[dir(base) for base in self.__class__.__mro__]))
        for base in reversed(self.__class__.__mro__):
            if base.__name__ == 'object':
                continue
            
            class_src = '\n'.join(l for l in inspect.getsource(base).
                                  split('\n') if not l.lstrip().startswith('#'))
            classnodes = ast.iter_child_nodes(ast.parse(dedent(class_src)))
            names = []
            for classnode in classnodes:
                for node in ast.iter_child_nodes(classnode):
                    if isinstance(node, ast.FunctionDef):
                        if node.name in Behaviour._omit_member_names:
                            continue

                        names += node.name,
                        if node.name in all_bases_names:
                            all_bases_names.remove(node.name)
        
                    elif isinstance(node, ast.Assign):
                        for target in node.targets:
                            if target.id in Behaviour._omit_member_names:
                                continue
                            names += target.id,
                            if target.id in all_bases_names:
                                all_bases_names.remove(target.id)
            
            for name in names:
                if not name in all_bases_names:
                    yield name
开发者ID:denz,项目名称:bdd-experiment,代码行数:35,代码来源:behaviour.py

示例2: get_testcases

def get_testcases(paths):
    """Walk each path in ``paths`` and return the test cases found.

    :param path: List o directories to find test modules and test cases.
    :return: A dict mapping a test module path and its test cases.
    """
    testmodules = []
    for path in paths:
        for dirpath, _, filenames in os.walk(path):
            for filename in filenames:
                if filename.startswith('test_') and filename.endswith('.py'):
                    testmodules.append(os.path.join(dirpath, filename))
    testcases = collections.OrderedDict()
    for testmodule in testmodules:
        testcases[testmodule] = []
        with open(testmodule) as handler:
            for node in ast.iter_child_nodes(ast.parse(handler.read())):
                if isinstance(node, ast.ClassDef):
                    # Class test methods
                    class_name = node.name
                    testcases[testmodule].extend([
                        TestFunction(subnode, class_name, testmodule)
                        for subnode in ast.iter_child_nodes(node)
                        if isinstance(subnode, ast.FunctionDef) and
                        subnode.name.startswith('test_')
                    ])
                elif (isinstance(node, ast.FunctionDef) and
                      node.name.startswith('test_')):
                    # Module's test functions
                    testcases[testmodule].append(TestFunction(
                        node, testmodule=testmodule))
    return testcases
开发者ID:spoore1,项目名称:testimony,代码行数:32,代码来源:__init__.py

示例3: get_test_methods

 def get_test_methods(fname):
     seen_classes = {}
     out = []
     lines = open(fname).readlines()
     lines = [x.rstrip('\r\n') for x in lines]
     a = ast.parse(("\n".join(lines)).rstrip() + '\n', fname)
     for cls in ast.iter_child_nodes(a):
         if isinstance(cls, ast.ClassDef) and is_test_class(cls):
             if cls.name in seen_classes:
                 raise ValueError("Duplicate class %s in %s" \
                                  % (cls.name, fname))
             seen_classes[cls.name] = {}
             seen_methods = {}
             for meth in ast.iter_child_nodes(cls):
                 if isinstance(meth, ast.FunctionDef) \
                    and meth.name.startswith('test'):
                     if meth.name in seen_methods:
                         raise ValueError("Duplicate method %s in %s" \
                                          % (meth.name, fname))
                     seen_methods[meth.name] = {}
                     testname = get_test_name(meth, fname, cls.name,
                                              meth.name,
                                              ast.get_docstring(meth, False))
                     out.append("%s.%s %s" % (cls.name, meth.name, testname))
     return out
开发者ID:drussel,项目名称:imp,代码行数:25,代码来源:get_python_tests.py

示例4: get_trait_definition

    def get_trait_definition(self):
        """ Retrieve the Trait attribute definition
        """
        # Get the class source and tokenize it.
        source = inspect.getsource(self.parent)

        nodes = ast.parse(source)
        for node in ast.iter_child_nodes(nodes):
            if isinstance(node, ClassDef):
                parent_node = node
                break
        else:
            return ''

        for node in ast.iter_child_nodes(parent_node):
            if isinstance(node, Assign):
                name = node.targets[0]
                if name.id == self.object_name:
                    break
        else:
            return ''

        endlineno = name.lineno
        for item in ast.walk(node):
            if hasattr(item, 'lineno'):
                endlineno = max(endlineno, item.lineno)

        definition_lines = [
            line.strip()
            for line in source.splitlines()[name.lineno-1:endlineno]]
        definition = ''.join(definition_lines)
        equal = definition.index('=')
        return definition[equal + 1:].lstrip()
开发者ID:itziakos,项目名称:trait-documenter,代码行数:33,代码来源:trait_documenter.py

示例5: visit_Attribute

 def visit_Attribute(self,node):
   if self.messagenode is not None:
     repnode = list(ast.walk(self.messagenode))
     if node in repnode:
       self.generic_visit(node)
       return
   self.messagenode = node
   t = ast.iter_child_nodes(node)
   res = [[t,0]]
   maxcount = 1
   while len(res) >= 1:
     t = res[-1][0]
     for childnode in t:
       # print childnode
       if isinstance(childnode, _ast.Attribute):
         res[-1][1] = 1
       else:
         res[-1][1] = 0
       res.append([ast.iter_child_nodes(childnode),0])
       break
     else:
       maxcount = max(maxcount,sum([flag for (item,flag) in res]) + 2)
       res.pop()
     continue
   # print maxcount
   if maxcount >= config['messagechain']:
     self.result.append((13,self.fileName,node.lineno,maxcount))
   self.generic_visit(node)
开发者ID:njuap,项目名称:python-smells,代码行数:28,代码来源:astChecker.py

示例6: nodes_equal

def nodes_equal(x, y):
    __tracebackhide__ = True
    assert type(x) == type(y), "Ast nodes do not have the same type: '%s' != '%s' " % (
        type(x),
        type(y),
    )
    if isinstance(x, (ast.Expr, ast.FunctionDef, ast.ClassDef)):
        assert (
            x.lineno == y.lineno
        ), "Ast nodes do not have the same line number : %s != %s" % (
            x.lineno,
            y.lineno,
        )
        assert x.col_offset == y.col_offset, (
            "Ast nodes do not have the same column offset number : %s != %s"
            % (x.col_offset, y.col_offset)
        )
    for (xname, xval), (yname, yval) in zip(ast.iter_fields(x), ast.iter_fields(y)):
        assert xname == yname, (
            "Ast nodes fields differ : %s (of type %s) != %s (of type %s)"
            % (xname, type(xval), yname, type(yval))
        )
        assert type(xval) == type(yval), (
            "Ast nodes fields differ : %s (of type %s) != %s (of type %s)"
            % (xname, type(xval), yname, type(yval))
        )
    for xchild, ychild in zip(ast.iter_child_nodes(x), ast.iter_child_nodes(y)):
        assert nodes_equal(xchild, ychild), "Ast node children differs"
    return True
开发者ID:mitnk,项目名称:xonsh,代码行数:29,代码来源:tools.py

示例7: visit_Subscript

 def visit_Subscript(self,node):
   if self.subscriptnode is not None:
     repnode = list(ast.walk(self.subscriptnode))
     if node in repnode:
       self.generic_visit(node)
       return
   self.subscriptnode = node
   t = ast.iter_child_nodes(node)
   res = [[t,0]]
   maxcount = 1
   while len(res) >= 1:
     t = res[-1][0]
     for childnode in t:
       # print childnode
       if isinstance(childnode, _ast.Subscript) or isinstance(childnode, _ast.Tuple) or \
        isinstance(childnode, _ast.Dict) or isinstance(childnode, _ast.List) or isinstance(childnode, _ast.Set):
         res[-1][1] = 1
       else:
         res[-1][1] = 0
       res.append([ast.iter_child_nodes(childnode),0])
       break
     else:
       maxcount = max(maxcount,sum([flag for (item,flag) in res]) + 1)
       res.pop()
     continue
   # print maxcount
   if maxcount >= config['containerdepth']:
     self.result.append((6,self.fileName,node.lineno,maxcount))
   self.generic_visit(node) 
开发者ID:njuap,项目名称:python-smells,代码行数:29,代码来源:astChecker.py

示例8: process_file

    def process_file(self):
        result = []
        with open(self.path) as f:
            current_node = ast.parse(f.read())
        pathes = os.path.splitext(self.path)[0]
        pathes = pathes.split("/")
        base_path = ".".join(pathes)

        nodes = [(i, base_path) for i in ast.iter_child_nodes(current_node)]
        while len(nodes) > 0:
            current_node, path = nodes.pop(0)
            if isinstance(current_node, ast.Import):
                module = ''
            elif isinstance(current_node, ast.ImportFrom):
                module = current_node.module
            elif isinstance(current_node, ast.FunctionDef) or isinstance(current_node, ast.ClassDef):
                path += "." + current_node.name
                next_nodes = [(i, path) for i in ast.iter_child_nodes(current_node)]
                nodes.extend(next_nodes)
                continue
            else:
                continue

            for n in current_node.names:
                result.append(self.node_class(module=module, full_path=path, name=n.name, alias=n.asname))
        return result
开发者ID:vwvolodya,项目名称:project,代码行数:26,代码来源:Analyzer.py

示例9: tag_class_functions

    def tag_class_functions(self, cls_node):
        """Tag functions if they are methods, classmethods, staticmethods"""
        # tries to find all 'old style decorators' like
        # m = staticmethod(m)
        late_decoration = {}
        for node in iter_child_nodes(cls_node):
            if not (isinstance(node, ast.Assign) and
                    isinstance(node.value, ast.Call) and
                    isinstance(node.value.func, ast.Name)):
                continue
            func_name = node.value.func.id
            if func_name in ('classmethod', 'staticmethod'):
                meth = (len(node.value.args) == 1 and node.value.args[0])
                if isinstance(meth, ast.Name):
                    late_decoration[meth.id] = func_name

        # iterate over all functions and tag them
        for node in iter_child_nodes(cls_node):
            if not isinstance(node, ast.FunctionDef):
                continue

            node.function_type = 'method'
            if node.name == '__new__':
                node.function_type = 'classmethod'

            if node.name in late_decoration:
                node.function_type = late_decoration[node.name]
            elif node.decorator_list:
                names = [d.id for d in node.decorator_list
                         if isinstance(d, ast.Name) and
                         d.id in ('classmethod', 'staticmethod')]
                if names:
                    node.function_type = names[0]
开发者ID:jimr,项目名称:pep8-naming,代码行数:33,代码来源:pep8ext_naming.py

示例10: visit_ClassDef

    def visit_ClassDef(self, node):
        self.transforms = {}
        self.in_class_define = True

        functions_to_promote = []
        setup_func = None

        for class_func in ast.iter_child_nodes(node):
            if isinstance(class_func, ast.FunctionDef):
                if class_func.name == 'setup':
                    setup_func = class_func
                    for anon_func in ast.iter_child_nodes(class_func):
                        if isinstance(anon_func, ast.FunctionDef):
                            functions_to_promote.append(anon_func)

        if setup_func:
            for func in functions_to_promote:
                setup_func.body.remove(func)
                func.args.args.insert(0, ast.Name(id='self', ctx=ast.Load()))
                node.body.append(func)
                self.transforms[func.name] = 'self.' + func.name

            ast.fix_missing_locations(node)

        self.generic_visit(node)

        return node
开发者ID:Axik,项目名称:pandas,代码行数:27,代码来源:vbench_to_asv.py

示例11: visit_Expr

    def visit_Expr(self, node):
        if self.document is None:
            raise ValueError()

        children = list(ast.iter_child_nodes(node))
        if len(children) != 1:
            raise ValueError()

        feature_name = None

        child = children[0]
        if isinstance(child, ast.Attribute) \
                and child.attr in self.document.features:
            feature_name = child.attr
        else:
            raise ValueError()

        grandchildren = list(ast.iter_child_nodes(child))
        if len(grandchildren) != 2:
            raise ValueError()

        grandchild = grandchildren[0]
        if isinstance(grandchild, ast.Name) \
                and grandchild.id in self.locals \
                and isinstance(self.locals[grandchild.id], self.document):
            self.doc = self.locals[grandchild.id]
            self.feature_name = feature_name
        else:
            raise ValueError()
开发者ID:JohnVinyard,项目名称:zounds,代码行数:29,代码来源:featureparser.py

示例12: test_iter_child_nodes

 def test_iter_child_nodes(self):
     node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
     self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
     iterator = ast.iter_child_nodes(node.body)
     self.assertEqual(next(iterator).id, 'spam')
     self.assertEqual(next(iterator).c, 23)
     self.assertEqual(next(iterator).c, 42)
     self.assertEqual(ast.dump(next(iterator)),
         "keyword(arg='eggs', value=Const(c='leek', constant=pure_const()))"
     )
开发者ID:lrq3000,项目名称:wpython2.wpython10,代码行数:10,代码来源:test_ast.py

示例13: walklocal

def walklocal(root):
    """Recursively yield all descendant nodes but not in a different scope"""
    todo = collections.deque(ast.iter_child_nodes(root))
    yield root, False
    while todo:
        node = todo.popleft()
        newscope = isinstance(node, ast.FunctionDef)
        if not newscope:
            todo.extend(ast.iter_child_nodes(node))
        yield node, newscope
开发者ID:Distrotech,项目名称:mercurial,代码行数:10,代码来源:import-checker.py

示例14: find_global_defs

    def find_global_defs(self, func_def_node):
        global_names = set()
        nodes_to_check = deque(iter_child_nodes(func_def_node))
        while nodes_to_check:
            node = nodes_to_check.pop()
            if isinstance(node, ast.Global):
                global_names.update(node.names)

            if not isinstance(node, (ast.FunctionDef, ast.ClassDef)):
                nodes_to_check.extend(iter_child_nodes(node))
        func_def_node.global_names = global_names
开发者ID:jimr,项目名称:pep8-naming,代码行数:11,代码来源:pep8ext_naming.py

示例15: visit_FunctionDef

 def visit_FunctionDef(self,node):
   # argsCount
   def findCharacter(s,d):
     try:
       value = s.index(d)
     except ValueError:
       return -1
     else:
       return value
   funcName = node.name.strip()
   p = re.compile("^(__[a-zA-Z0-9]+__)$")
   if p.match(funcName.strip()) and funcName != "__import__" and funcName != "__all__":
     self.defmagic.add((funcName,self.fileName,node.lineno))
   stmt = astunparse.unparse(node.args)
   arguments = stmt.split(",")
   argsCount = 0
   for element in arguments:
     if findCharacter(element,'=') == -1:
       argsCount += 1
   self.result.append((1,self.fileName,node.lineno,argsCount))
   #function length
   lines = set()
   res = [node]
   while len(res) >= 1:
     t = res[0]
     for n in ast.iter_child_nodes(t):
       if not hasattr(n,'lineno') or ((isinstance(t,_ast.FunctionDef) or isinstance(t,_ast.ClassDef)) and n == t.body[0] and isinstance(n,_ast.Expr)):
         continue
       lines.add(n.lineno)
       if isinstance(n,_ast.ClassDef) or isinstance(n,_ast.FunctionDef):
         continue
       else:
         res.append(n)
     del res[0]
   self.result.append((2,self.fileName,node.lineno,len(lines))) 
   #nested scope depth
   if node in self.scopenodes:
     self.scopenodes.remove(node)
     self.generic_visit(node)
     return
   dep = [[node,1]] #node,nestedlevel
   maxlevel = 1
   while len(dep) >= 1:
     t = dep[0][0]
     currentlevel = dep[0][1]
     for n in ast.iter_child_nodes(t):
       if isinstance(n,_ast.FunctionDef):
         self.scopenodes.append(n)
         dep.append([n,currentlevel+1])
     maxlevel = max(maxlevel,currentlevel)
     del dep[0]
   if maxlevel>1:
     self.result.append((3,self.fileName,node.lineno,maxlevel)) #DOC
   self.generic_visit(node) 
开发者ID:chenzhifei731,项目名称:Pysmell,代码行数:54,代码来源:astChecker.py


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