• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python ast.iter_fields函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: str_node

def str_node(node):
	if isinstance(node, ast.AST):
		fields = [(name, str_node(val)) for name, val in ast.iter_fields(node) if name not in ('left', 'right')]
		rv = '%s(%s' % (node.__class__.__name__, ', '.join('%s=%s' % field for field in fields))
		return rv + ')'
	else:
		return repr(node)
开发者ID:dhrone,项目名称:music-display,代码行数:7,代码来源:asttest.py


示例17: generic_visit

    def generic_visit(self, node):

        if isinstance(node, list):
            nodestart = "["
            nodeend = "]"
            children = [("", child) for child in node]
        else:
            nodestart = type(node).__name__ + "("
            nodeend = ")"
            children = [(name + "=", value) for name, value in ast.iter_fields(node)]

        if len(children) > 1:
            self.indentation += 1

        self.write(nodestart)
        for i, pair in enumerate(children):
            attr, child = pair
            if len(children) > 1:
                self.write("\n" + self.indent_with * self.indentation)
            if isinstance(child, (ast.AST, list)):
                self.write(attr)
                self.visit(child)
            else:
                self.write(attr + repr(child))

            if i != len(children) - 1:
                self.write(",")
        self.write(nodeend)

        if len(children) > 1:
            self.indentation -= 1
开发者ID:fjarri,项目名称:astunparse,代码行数:31,代码来源:printer.py


示例18: _format

  def _format(node, field='', level=0):
    if isinstance(node, ast.AST):

      head_string  = get_indent( level ) + node.__class__.__name__
      head_string += " ({})".format(field) if field else ""

      if show_lineno and hasattr( node, 'lineno' ):
        head_string += ' [{}]'.format( node.lineno )

      body_string  = ""
      for label, value in ast.iter_fields(node):
        if label == "ctx":
          continue
        if isinstance( value, (str, int, float, long) ):
          head_string +=  '  {} = {}'.format( label, value )
        elif not value:
          body_string += get_indent( level+1, pipe=True) + '{} ({})\n'.format( value, label )
        else:
          body_string += _format( value, label, level+1 )


      return head_string + "\n" + body_string

    elif isinstance(node, list):
      string = ''
      for x in node:
        string += _format( x, field, level )
      return string

    return repr(node)
开发者ID:cornell-brg,项目名称:pymtl,代码行数:30,代码来源:ast_helpers.py


示例19: code_generic_visit

 def code_generic_visit(self, node):
     for field, old_value in ast.iter_fields(node):
         old_value = getattr(node, field, None)
         if isinstance(old_value, list):
             prev_insertion_point = self._insertion_point
             new_values = []
             if field in ("body", "orelse", "finalbody"):
                 self._insertion_point = new_values
             for value in old_value:
                 if isinstance(value, ast.AST):
                     value = self.code_visit(value)
                     if value is None:
                         continue
                     elif not isinstance(value, ast.AST):
                         new_values.extend(value)
                         continue
                 new_values.append(value)
             old_value[:] = new_values
             self._insertion_point = prev_insertion_point
         elif isinstance(old_value, ast.AST):
             new_node = self.code_visit(old_value)
             if new_node is None:
                 delattr(node, field)
             else:
                 setattr(node, field, new_node)
     return node
开发者ID:yuyichao,项目名称:artiq,代码行数:26,代码来源:inline.py


示例20: visit_AllNumVar

 def visit_AllNumVar(self, node):
     self.indent_print(node.__class__.__name__ + ":")
     for field, value in ast.iter_fields(node):
         if field == "id":
             self.indent = self.indent + 1
             self.indent_print(field + ":" + str(value))
             self.indent = self.indent - 1
开发者ID:nacuong,项目名称:cs294-98,代码行数:7,代码来源:print_visitor.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python ast.keyword函数代码示例发布时间:2022-05-24
下一篇:
Python ast.iter_child_nodes函数代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap