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


Python ast.iter_fields函数代码示例

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


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

示例1: visit_FunctionDef

  def visit_FunctionDef(self, node, parent):
    if node in self.seen:
      return node
    self.seen.add(node)
    oldNode = dict(ast.iter_fields(node))
    finalBlock = []
    newNode = ast.FunctionDef(name=oldNode['name'], args=oldNode['args'], body=[ast.TryFinally(body=dict(ast.iter_fields(node))['body'], finalbody=finalBlock)], decorator_list=oldNode['decorator_list'])
    
    self.funcName = dict(ast.iter_fields(node))['name']
    if self.funcName in self.localFuncs:
      args = dict(ast.iter_fields(dict(ast.iter_fields(node))['args']))
      args['args'].append(ast.Name(id=self.calleeInfo, ctx=ast.Param()))
      args['defaults'].append(ast.Str(s=''))
      self.addChild(ast.Expr(value=ast.Call(func=ast.Attribute(value=ast.Name(id=self.locStack, ctx=ast.Load()), attr='append', ctx=ast.Load()), args=[ast.Name(id=self.calleeInfo, ctx=ast.Load())], keywords=[], starargs=None, kwargs=None)), newNode)

    #self.addChild(ast.Print(dest=None, values=[ast.Name(id=self.funcStack, ctx=ast.Load())], nl=True), node)
    self.addChild(ast.Expr(value=ast.Call(func=ast.Attribute(value=ast.Name(id=self.funcStack, ctx=ast.Load()), attr='append', ctx=ast.Load()), args=[ast.Str(s=self.funcName)], keywords=[], starargs=None, kwargs=None)), newNode)
    self.addChild(ast.Assign(targets=[ast.Name(id=self.loopSize, ctx=ast.Store())], value=ast.Call(func=ast.Name(id='len', ctx=ast.Load()), args=[ast.Name(id=self.loopStack, ctx=ast.Load())], keywords=[], starargs=None, kwargs=None)), newNode)

    finalBlock.append(ast.Expr(value=ast.Call(func=ast.Attribute(value=ast.Name(id=self.funcStack, ctx=ast.Load()), attr='pop', ctx=ast.Load()), args=[], keywords=[], starargs=None, kwargs=None)))
    if self.funcName in self.localFuncs:
      finalBlock.append(ast.Expr(value=ast.Call(func=ast.Attribute(value=ast.Name(id=self.locStack, ctx=ast.Load()), attr='pop', ctx=ast.Load()), args=[], keywords=[], starargs=None, kwargs=None)))
    loopCorr = ast.While(test=ast.Compare(left=ast.Call(func=ast.Name(id='len', ctx=ast.Load()), args=[ast.Name(id=self.loopStack, ctx=ast.Load())], keywords=[], starargs=None, kwargs=None), ops=[ast.Gt()], comparators=[ast.Name(id=self.loopSize, ctx=ast.Load())]), body=[ast.Expr(value=ast.Call(func=ast.Attribute(value=ast.Name(id=self.loopStack, ctx=ast.Load()), attr='pop', ctx=ast.Load()), args=[], keywords=[], starargs=None, kwargs=None))], orelse=[])
    self.seen.add(loopCorr)
    finalBlock.append(loopCorr)
    #self.addChild(ast.Print(dest=None, values=[ast.Str(s=self.funcName + '_afterPop')], nl=True), node, loc=len(dict(ast.iter_fields(node))['body']))

    ast.fix_missing_locations(newNode)
    #print ast.dump(newNode) 
    self.funcs.append(newNode)
    self.generic_visit(newNode, parent)
    return newNode
开发者ID:RazvanRanca,项目名称:StocPyDev,代码行数:32,代码来源:stocPyDev.py

示例2: parse_imports

def parse_imports(s):
    module = ast.parse(s)
    imports = []
    for c in ast.iter_child_nodes(module):
        if isinstance(c, ast.ImportFrom):
            fields = dict(ast.iter_fields(c))
            from_module = fields['module']
            if from_module != '__future__':
                raise ProcessingException(
                    'non-future ImportFrom on line ' + str(c.lineno))
            names = [dict(ast.iter_fields(i))['name'] for i in fields['names']]
            if len(names) != 1:
                raise ProcessingException(
                    'multiple imports on line ' + str(c.lineno))
            imports.append(ParsedImport(c.lineno, names[0], True))

        if isinstance(c, ast.Import):
            fields = dict(ast.iter_fields(c))
            names = [dict(ast.iter_fields(i))['name'] for i in fields['names']]
            if len(names) != 1:
                raise ProcessingException(
                    'multiple imports on line ' + str(c.lineno))
            imports.append(ParsedImport(c.lineno, names[0], False))

    return imports
开发者ID:craigedmunds,项目名称:phabricator-tools,代码行数:25,代码来源:fiximports.py

示例3: match_recurse

        def match_recurse(pattern_node, actual_node, collected):
            supertypes = [actual_node.__class__]
            while supertypes[-1] != object:
                supertypes.append(supertypes[-1].__base__)

            if is_placeholder(pattern_node, ["__" + t.__name__ for t in supertypes]):
                collected.append(actual_node)
                return True
            elif type(pattern_node) != type(actual_node):
                return False
            elif isinstance(pattern_node, ast.AST):
                return all(
                    match_recurse(left, right, collected)
                    for ((left_name, left), (right_name, right))
                    in zip(ast.iter_fields(pattern_node), ast.iter_fields(actual_node))
                )
            elif isinstance(pattern_node, list):
                if len(pattern_node) != len(actual_node):
                    return False
                else:
                    return all(
                        match_recurse(left, right, collected)
                        for (left, right) in zip(pattern_node, actual_node)
                    )
            else:
                if pattern_node == "__" and type(actual_node) is str:
                    collected.append(actual_node)
                    return True
                else:
                    return pattern_node == actual_node
开发者ID:dropbox,项目名称:changes,代码行数:30,代码来源:analysis.py

示例4: attach_data

    def attach_data(self, node):
        """Generic method called for visit_XXXX() with XXXX in
        GatherOMPData.statements list

        """
        if self.current:
            for curr in self.current:
                md = OMPDirective(curr)
                metadata.add(node, md)
            self.current = list()
        # add a Pass to hold some directives
        for field_name, field in ast.iter_fields(node):
            if field_name in GatherOMPData.statement_lists:
                if field and isinstance(field[-1], ast.Expr) and self.isompdirective(field[-1].value):
                    field.append(ast.Pass())
        self.generic_visit(node)

        # add an If to hold scoping OpenMP directives
        directives = metadata.get(node, OMPDirective)
        field_names = {n for n, _ in ast.iter_fields(node)}
        has_no_scope = field_names.isdisjoint(GatherOMPData.statement_lists)
        if directives and has_no_scope:
            # some directives create a scope, but the holding stmt may not
            # artificially create one here if needed
            sdirective = "".join(d.s for d in directives)
            scoping = ("parallel", "task", "section")
            if any(s in sdirective for s in scoping):
                node = ast.If(ast.Num(1), [node], [])
        return node
开发者ID:decabyte,项目名称:pythran,代码行数:29,代码来源:openmp.py

示例5: 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

示例6: match

def match(node, pat):
    """Return `True` if AST tree `node` matches AST pattern `pat`.
    """
    if isinstance(pat, ast.Name) and pat.id == '_':
        return True
    elif isinstance(pat, ast.AST):
        if not isinstance(node, ast.AST):
            return False
        if not (issubclass(node.__class__, pat.__class__) or
                issubclass(pat.__class__, node.__class__)):
            return False
        assert _check_fields(node, pat)
        for (field1, val1), (field2, val2) in \
                zip(ast.iter_fields(node),
                    ast.iter_fields(pat)):
            assert(field1 == field2)
            if not match(val1, val2):
                return False
    elif isinstance(pat, list):
        if not isinstance(node, list):
            return False
        if len(node) != len(pat):
            return False
        for val1, val2 in zip(node, pat):
            if not match(val1, val2):
                return False
    else:
        # Primitive comparison.
        if node != pat:
            return False

    return True
开发者ID:jrydberg,项目名称:pyssla,代码行数:32,代码来源:pat.py

示例7: visit_arguments

  def visit_arguments(self, node):
    args = None
    vararg = None
    kwarg = None
    defaults = None
    for field, value in ast.iter_fields(node):
      if field == "args":
        args = value
        for arg in args:
          #print "Arg: ", arg
          for field, value in ast.iter_fields(arg):
            if field == "id":
              #print "Define arg: ", value
              self.env[value] = "arg"
            else:
              JSONVisitorException("Unexpected error: argument's field is not id.")
      elif field == "vararg":
        vararg = value
      elif field == "kwarg":
        kwarg = value
      elif field == "defaults":
        defaults = value

      if vararg or kwarg or defaults:
        raise JSONVisitorException("Unexpected error: Missed case: vararg, kwarg or defaults is not empty.")
开发者ID:nacuong,项目名称:cs294-98,代码行数:25,代码来源:define_visitor.py

示例8: visit_Call

    def visit_Call(self, node):
        super(srcpuller, self).generic_visit(node)
        state = 0
        for n in ast.iter_child_nodes(node):
            if state == 0:
                if type(n) == ast.Name:
                    for field, value in ast.iter_fields(n):
                        if (field == 'id' and value == 'source'):
                            state = 1
                            break
                    continue
            elif state == 1:
                if type(n) == ast.Str:
                    for field, value in ast.iter_fields(n):
                        if (field == 's'):
                            print 'sourc name:', value
                            state = 2
                            break
                    continue

            elif state == 2:
                if type(n) == ast.Str:
                    for field, value in ast.iter_fields(n):
                        if (field == 's'):
                            print 'pat name:', value
                            state = 3
                            break
                    continue
            elif state == 3:
                if type(n) == ast.Str:
                    for field, value in ast.iter_fields(n):
                        if (field == 's'):
                            print 'inst name:', value
                            break
            break
开发者ID:pietergvw,项目名称:quilt,代码行数:35,代码来源:protoparser.py

示例9: editfunctiondef

    def editfunctiondef(self, node):
        for fname,fnode in ast.iter_fields(node):
            if fname == 'args':
                for argname, argnode in ast.iter_fields(fnode):
                    if argname == 'kwarg' and argnode != None:
                        del self.functionstofix[node.lineno]

        if node.lineno in self.functionstofix.keys():
            print "%d | Fixing function definition." % (node.lineno)
开发者ID:pombredanne,项目名称:concoord,代码行数:9,代码来源:serversideproxyast.py

示例10: visit_Call

  def visit_Call(self, node, parent):
    if node in self.seen:
      return node
    self.seen.add(node)

    callName = dict(ast.iter_fields(dict(ast.iter_fields(node))['func'])).get('id', None)
    callType = dict(ast.iter_fields(dict(ast.iter_fields(node))['func'])).get('attr',None)
    #print ast.dump(dict(ast.iter_fields(node))['func']), callType, node.lineno, node.col_offset
    #print callName, self.localFuncs
    if callName in self.localFuncs:
      #print ast.dump(node)
      #print callName, node.lineno, node.col_offset
      dict(ast.iter_fields(node))['keywords'].append(ast.keyword(arg=self.calleeInfo, value=ast.Str(s=str(node.lineno) + "-" + str(node.col_offset))))
      
    if callType in erps.keys() or callType == "stocPrim":
      if callType not in self.primsNumArgs:
        self.primsNumArgs[callType] = len(inspect.getargspec(globals()[callType]).args)

      namedArgs = map(lambda x: dict(ast.iter_fields(x))['arg'], dict(ast.iter_fields(node))['keywords'])
      numArgs = len(namedArgs) + len(dict(ast.iter_fields(node))['args']) 
      #print callType, node.lineno, node.col_offset
      #print ast.dump(parent)
      if not ('name' in namedArgs or numArgs == self.primsNumArgs[callType]): #check if name already supplied
        dict(ast.iter_fields(node))['keywords'].append(ast.keyword(arg='name', value=ast.BinOp(left=ast.BinOp(left=ast.Call(func=ast.Name(id='str', ctx=ast.Load()), args=[ast.Name(id=self.funcStack, ctx=ast.Load())], keywords=[], starargs=None, kwargs=None), op=ast.Add(), right=ast.Call(func=ast.Name(id='str', ctx=ast.Load()), args=[ast.Name(id=self.locStack, ctx=ast.Load())], keywords=[], starargs=None, kwargs=None)), op=ast.Add(), right=ast.BinOp(left=ast.Str(s=str(node.lineno) + "-" + str(node.col_offset)), op=ast.Add(), right=ast.Call(func=ast.Name(id='str', ctx=ast.Load()), args=[ast.Name(id=self.loopStack, ctx=ast.Load())], keywords=[], starargs=None, kwargs=None)))))

    
    ast.fix_missing_locations(node)
    #print map(ast.dump, dict(ast.iter_fields(node))['keywords'])
    self.generic_visit(node)
    return node
开发者ID:RazvanRanca,项目名称:StocPyDev,代码行数:30,代码来源:stocPyDev.py

示例11: shallow_match_main

    def shallow_match_main(self, ins_node, std_node, check_meta=True, ignores=[]):
        """
                Checks that all non astNode attributes are equal between ins_node and std_node
                :param ins_node: Instructor ast root node
                :param std_node: Student AST root node
                :param check_meta: flag to check whether the fields of the instructor node and the student node should match
                :return: a mapping between the isntructor and student root nodes, or False if such a mapping doesn't exist
                """
        ins = ins_node.astNode
        std = std_node.astNode
        ins_field_list = list(ast.iter_fields(ins))
        std_field_list = list(ast.iter_fields(std))
        meta_matched = self.metas_match(ins_node, std_node, check_meta)
        is_match = len(ins_field_list) == len(std_field_list) and type(ins).__name__ == type(
            std).__name__ and meta_matched
        for insTup, stdTup in zip(ins_field_list, std_field_list):
            if not is_match:
                break

            ins_field = insTup[0]
            ins_value = insTup[1]
            std_field = stdTup[0]
            std_value = stdTup[1]

            if ins_value is None:
                continue

            ignore_field = ins_field in ignores

            is_match = (ins_field == std_field) or ignore_field

            if not isinstance(ins_value, list):
                ins_value = [ins_value]

            if not isinstance(std_value, list):
                std_value = [std_value]

            # is_match = len(ins_value) == len(std_value)# for stretchy matching this isn't True
            # Reference ast_node_visitor.js for the original behavior and keep note of it for the purposes of handling
            # the children noting the special case when the nodes of the array are actually parameters of the node
            # (e.g. a load function) instead of a child node
            if not ignore_field:
                for inssub_value, stdsub_value in zip(ins_value, std_value):
                    if not is_match:
                        break
                    # TODO: make this a smarter comparison, maybe handle dictionaries, f-strings, tuples, etc.
                    if is_primitive(inssub_value):
                        is_match = inssub_value == stdsub_value
        mapping = False
        if is_match:
            mapping = AstMap()  # return MAPPING
            mapping.add_node_pairing(ins_node, std_node)
            mapping = [mapping]
        return mapping
开发者ID:RealTimeWeb,项目名称:skulpt,代码行数:54,代码来源:stretchy_tree_matching.py

示例12: generic_visit

    def generic_visit(self, node):
        for key, value in ast.iter_fields(node):
            if isinstance(value, AST):
                for k, v in ast.iter_fields(value):
                    if isinstance(v, AST):
                        return True
            elif isinstance(value, list):
                for node in value:
                    if isinstance(node, AST):
                        return True

        return False
开发者ID:EcmaXp,项目名称:PyCraft,代码行数:12,代码来源:pc_old.py

示例13: visit_ImportFrom

 def visit_ImportFrom(self, node):
     fields = dict(ast.iter_fields(node))
     alias_nodes = list(ast.iter_child_nodes(node))
     for alias_node in alias_nodes:
         alias_fields = dict(ast.iter_fields(alias_node))
         if fields['level'] == 0:
             # from a import b
             # it might mean a.b is a module
             # or b might be a function of a module
             self.absolute_imports.append(fields['module'])
             self.absolute_imports.append('{}.{}'.format(fields['module'], alias_fields['name']))
         else:
             self.relative_imports.append((fields['level'], fields['module'], alias_fields['name']))
开发者ID:honovation,项目名称:veil,代码行数:13,代码来源:import_collector.py

示例14: nodes_equal

def nodes_equal(x, y):
    __tracebackhide__ = True
    assert type(x) == type(y)
    if isinstance(x, (ast.Expr, ast.FunctionDef, ast.ClassDef)):
        assert x.lineno == y.lineno
        assert x.col_offset == y.col_offset
    for (xname, xval), (yname, yval) in zip(ast.iter_fields(x),
                                            ast.iter_fields(y)):
        assert xname == yname
        assert type(xval) == type(yval)
    for xchild, ychild in zip(ast.iter_child_nodes(x),
                              ast.iter_child_nodes(y)):
        assert nodes_equal(xchild, ychild)
    return True
开发者ID:AndreaCrotti,项目名称:xonsh,代码行数:14,代码来源:test_parser.py

示例15: visit_Assign

  def visit_Assign(self, node):
    lhs = None
    rhs = None
    for field, value in ast.iter_fields(node):
      if field == "targets":
        lhs = value[0]
      elif field == "value":
        rhs = value
      else:
        raise JSONVisitorException("Unexpected error: Missed case: %s." % value)

    if isinstance(lhs, ast.Name):
      node.targets = [self.visit(lhs)]
      node.value = self.visit(rhs)
    elif isinstance(lhs, ast.Tuple):
      new_lhs = []
      new_rhs = []
      for l,r in zip(lhs.elts, rhs.elts):
        new_lhs.append(self.visit(l))
        new_rhs.append(self.visit(r))

      lhs.elts = new_lhs
      rhs.elts = new_rhs

    if (node.lineno, node.col_offset) in self.locs:
      self.fixes.append(node)

    return node
开发者ID:nacuong,项目名称:cs294-98,代码行数:28,代码来源:synthesis_visitor.py


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