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


Python ast.parse函数代码示例

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


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

示例1: parse_config

def parse_config(files, commands):
    inner_globals = dict(SAFE_FUNCTIONS)
    inner_globals["__builtins__"] = {} # Disable any builtin function
    inner_globals["named_items"] = {} # Store the provided named items
    
    for file in files:
        text = file.read()
        root = ast.parse(text, filename=file.name)
        execute_code(root, file.name, inner_globals)
    
    for text in commands:
        root = ast.parse(text, filename="<execute>")
        execute_code(root, '--execute', inner_globals)
    
    # Make sure we have the necessary variables in the inner_globals dictionary
    for varname in REQUIRED:
        if varname not in inner_globals:
            raise ParseException("Missing the {!r} variable".format(varname))
    
    comparer = inner_globals["comparer"]
    groups = inner_globals["groups"]
    items = inner_globals["named_items"]
    
    if "total" in inner_globals:
        total = inner_globals["total"]
    else:
        total = len(groups)
    
    return Config(comparer, items, groups, total)
开发者ID:jdferreira,项目名称:MOSSy,代码行数:29,代码来源:parse_config.py

示例2: walk

    def walk(filename, visitor):
        """Generate an AST for the given filename and walk over it using
        the given visitor instance.
        """

        filename = os.path.abspath(filename)

        try:
            tree = ast.parse(open(filename, 'r').read())
        except SyntaxError:
            if sys.version_info[0] < 3:
                e = sys.exc_info()[1]
                log.warn('SyntaxError while parsing file %s: %s' %
                         (filename, str(e)))
                return
            # We're probably in Python 3 and looking at a file intended for
            # Python 2.  Otherwise there's an unintended SyntaxError in the
            # file, so there are bigger problems anyways
            try:
                import lib2to3.refactor

                tool = StringRefactoringTool(
                    lib2to3.refactor.get_fixers_from_package('lib2to3.fixes'))
                tool.refactor_file(filename, write=True)
                tree = ast.parse(tool.refactored[filename])
            except ImportError:
                # Without 2to3 we can't do much more.
                # TODO: Issue a warning?
                return

        visitor.visit(tree)
开发者ID:dhomeier,项目名称:stsci.distutils,代码行数:31,代码来源:astutils.py

示例3: ParseCodeToTree

def ParseCodeToTree(code):
  """Parse the given code to a lib2to3 pytree.

  Arguments:
    code: a string with the code to parse.

  Raises:
    SyntaxError if the code is invalid syntax.
    parse.ParseError if some other parsing failure.

  Returns:
    The root node of the parsed tree.
  """
  # This function is tiny, but the incantation for invoking the parser correctly
  # is sufficiently magical to be worth abstracting away.
  try:
    # Try to parse using a Python 3 grammar, which is more permissive (print and
    # exec are not keywords).
    parser_driver = driver.Driver(_GRAMMAR_FOR_PY3, convert=pytree.convert)
    tree = parser_driver.parse_string(code, debug=False)
  except parse.ParseError:
    # Now try to parse using a Python 2 grammar; If this fails, then
    # there's something else wrong with the code.
    try:
      parser_driver = driver.Driver(_GRAMMAR_FOR_PY2, convert=pytree.convert)
      tree = parser_driver.parse_string(code, debug=False)
    except parse.ParseError:
      # Raise a syntax error if the code is invalid python syntax.
      try:
        ast.parse(code)
      except SyntaxError as e:
        raise e
      else:
        raise
  return _WrapEndMarker(tree)
开发者ID:minttu,项目名称:yapf,代码行数:35,代码来源:pytree_utils.py

示例4: scripts2rst

def scripts2rst(path, f):
    """ creates rst summary documentation for files in scripts folder
    which is not a package so cannot be imported """
       
    # get list of script files
    try:
        files = [name for name in os.listdir(path)
                    if not name.startswith("_")
                    and name.endswith(".py")]
    except:
        return
        
    f.write("**Scripts**\n\n")
    
    # loop over script files
    for name in files:
        sfile = os.path.join(path, name)
        try:
            try:
                source = ast.parse(open(sfile, "r", encoding="utf8").read())
            except:
                #py2 fails if encoding in string
                source = ast.parse(open(sfile, "r").read())
        except Exception as e:
            log.warning("Problem parsing %s\n%s"%(name, e))
        f.write(name+"\n")
        doc = i.cleandoc(ast.get_docstring(source)) or "."
        doc = py2decode(doc) #py2
        f.write(" "+doc.splitlines()[0]+"\n")
    f.write("\n")
开发者ID:simonm3,项目名称:qdoc,代码行数:30,代码来源:parse.py

示例5: infer_ast

def infer_ast(src):
    """Attempts to infer an abstract syntax tree from the provided value.
    
    - Python ast.AST instances are passed through.
    - Strings are parsed. A SyntaxError is raised if invalid.
    - Functions are sent through cypy.fn_get_source to get a source
      string, then parsed. If the source can't be a found, an exception is 
      raised by fn_get_source.
      
    .. WARNING:: Functions defined on the iPython command line do not have 
                 their source saved. A bug has been filed: 
                 
                 http://github.com/ipython/ipython/issues/issue/120
    """
    if isinstance(src, _ast.AST):
        return src
    elif isinstance(src, basestring):
        return _ast.parse(src)
    else:
        # if a function instance is passed in, it's source is found
        # and parsed. note that finding source can be precarious for
        # functions defined on the command line. If you get an error
        # you'll have to use strings instead of regular function
        # definitions
        src = cypy.fn_get_source(src)
        return _ast.parse(src)
开发者ID:atlang,项目名称:ace,代码行数:26,代码来源:astx.py

示例6: update

 def update(self, name, script, template):
     self.name = name
     try:
         ast.parse(script)
         self.script = script
     except SyntaxError, e:
         raise UserException(e)
开发者ID:AndyDingley,项目名称:chellow,代码行数:7,代码来源:models.py

示例7: test_free_vars

    def test_free_vars(self):
        stmt = ast.parse("foo", "", "single")
        with self.assertRaises(ValueError):
            freevars(stmt, [])

        suite = ast.parse("foo; bar();", "exec")
        with self.assertRaises(ValueError):
            freevars(suite, [])

        def freevars_(source, env=[]):
            return freevars(ast.parse(source, "", "eval"), env)

        self.assertEqual(freevars_("1"), [])
        self.assertEqual(freevars_("..."), [])
        self.assertEqual(freevars_("a"), ["a"])
        self.assertEqual(freevars_("a", ["a"]), [])
        self.assertEqual(freevars_("f(1)"), ["f"])
        self.assertEqual(freevars_("f(x)"), ["f", "x"])
        self.assertEqual(freevars_("f(x)", ["f"]), ["x"])
        self.assertEqual(freevars_("a + 1"), ["a"])
        self.assertEqual(freevars_("a + b"), ["a", "b"])
        self.assertEqual(freevars_("a + b", ["a", "b"]), [])
        self.assertEqual(freevars_("a[b]"), ["a", "b"])
        self.assertEqual(freevars_("a[b]", ["a", "b"]), [])
        self.assertEqual(freevars_("f(x, *a)", ["f"]), ["x", "a"])
        self.assertEqual(freevars_("f(x, *a, y=1)", ["f"]), ["x", "a"])
        self.assertEqual(freevars_("f(x, *a, y=1, **k)", ["f"]),
                         ["x", "a", "k"])
        if sys.version_info >= (3, 5):
            self.assertEqual(freevars_("f(*a, *b, k=c, **d, **e)", ["f"]),
                             ["a", "b", "c", "d", "e"])

        self.assertEqual(freevars_("True"), [])
        self.assertEqual(freevars_("'True'"), [])
        self.assertEqual(freevars_("None"), [])
        self.assertEqual(freevars_("b'None'"), [])

        self.assertEqual(freevars_("a < b"), ["a", "b"])
        self.assertEqual(freevars_("a < b <= c"), ["a", "b", "c"])
        self.assertEqual(freevars_("1 < a <= 3"), ["a"])

        self.assertEqual(freevars_("{}"), [])
        self.assertEqual(freevars_("[]"), [])
        self.assertEqual(freevars_("()"), [])
        self.assertEqual(freevars_("[a, 1]"), ["a"])
        self.assertEqual(freevars_("{a: b}"), ["a", "b"])
        self.assertEqual(freevars_("{a, b}"), ["a", "b"])
        self.assertEqual(freevars_("0 if abs(a) < 0.1 else b", ["abs"]),
                         ["a", "b"])
        self.assertEqual(freevars_("lambda a: b + 1"), ["b"])
        self.assertEqual(freevars_("lambda a: b + 1", ["b"]), [])
        self.assertEqual(freevars_("lambda a: a + 1"), [])
        self.assertEqual(freevars_("(lambda a: a + 1)(a)"), ["a"])
        self.assertEqual(freevars_("lambda a, *arg: arg + (a,)"), [])
        self.assertEqual(freevars_("lambda a, *arg, **kwargs: arg + (a,)"), [])

        self.assertEqual(freevars_("[a for a in b]"), ["b"])
        self.assertEqual(freevars_("[1 + a for c in b if c]"), ["a", "b"])
        self.assertEqual(freevars_("{a for _ in [] if b}"), ["a", "b"])
        self.assertEqual(freevars_("{a for _ in [] if b}", ["a", "b"]), [])
开发者ID:rekonder,项目名称:orange3,代码行数:60,代码来源:test_owfeatureconstructor.py

示例8: runcode

 def runcode(self, the_code, source, filename='<input>'):
     # code taken from InteractiveInterpreter.runsource in code.py
     try:
         tree = ast.parse(source)
         try:
             expr = ast.parse(source, mode='eval')
         except:
             expr = None
         #todo get this to work for multiple expr's, not just 1:
         if expr and len(tree.body) == 1:
             # _ = expr_value
             tree.body[0] = ast_wrap_in_assn('_', tree.body[0])
             # print _
             underscore = _ast.Name(id="_", ctx=_ast.Load())
             print_node = ast_print_node([_ast.Str(s=' '*50), underscore])
             tree.body.append(print_node)
             # play_whatever
                 #todo doesn't work for generators yet
             play_whatever_node = ast_call_node('music.play_whatever', '_', show_notes=SHOW_NOTES)
             tree.body.append(play_whatever_node)
         #print ast.dump(tree)
         code_obj = compile(tree, '<input>', 'exec')
         exec code_obj in self.locals
     except SystemExit:
         raise
     except:
         self.showtraceback()
     else:
         if code.softspace(sys.stdout, 0):
             print
开发者ID:ryanfmurphy,项目名称:music,代码行数:30,代码来源:console.py

示例9: python_code

def python_code(tokenIterator, envName, endToken):
    """
    Given an iterator of tokens, and the name of current environment, iterates through to the end of
    the environment, and returns the text in the environment as python code. This function only
    works for environments that are supposed to contain Python code.
    """
    count = 0
    pythonLine = ''
    pythonCode = []
    #parseNodes = []
    startLine = 0
    for token, lineNum in tokenIterator:
        if not startLine:
            #The first token is the endline at the end of \begin{envName}, so the environment starts on the same line as the first token in tokenIterator
            startLine = lineNum
        if endToken == token:
            pythonCode = [line for line in pythonCode if line.strip()]
            #We use this to figure out what the top level indent is, and strip that away so that Python can parse the code properly.
            topLevelIndent = 1 if pythonCode[0][0] == '\t' else compute_indent(pythonCode[0])
            try:
                ast.parse(''.join(line[topLevelIndent:] for line in  pythonCode))
            except SyntaxError, e:
                raise transExceptions.TranslationError(' '.join([parseTree.color("Error:", parseTree.bcolors.RED), 'Error in Python code found in', parseTree.color(envName,parseTree.bcolors.YELLOW),  
                    'environment. Environment start line:', parseTree.color(str(startLine), parseTree.bcolors.GREEN),  'Python error:\n\n', str(e)]))
            else:
                return (startLine, pythonCode)
        else:
            if token.strip(' \t') == '\n':
                pythonCode.append(pythonLine + token)
                pythonLine = ''
            else:
                pythonLine += token
开发者ID:thingywhat,项目名称:PotionWars,代码行数:32,代码来源:parse.py

示例10: warn_about_none_ast

    def warn_about_none_ast(self, node, module_path, lineno):
        """
        Returns an AST issuing a warning if the value of node is `None`.
        This is used to warn the user when asserting a function that asserts
        internally already.
        See issue #3191 for more details.
        """

        # Using parse because it is different between py2 and py3.
        AST_NONE = ast.parse("None").body[0].value
        val_is_none = ast.Compare(node, [ast.Is()], [AST_NONE])
        send_warning = ast.parse(
            """
from _pytest.warning_types import PytestWarning
from warnings import warn_explicit
warn_explicit(
    PytestWarning('asserting the value None, please use "assert is None"'),
    category=None,
    filename={filename!r},
    lineno={lineno},
)
            """.format(
                filename=module_path.strpath, lineno=lineno
            )
        ).body
        return ast.If(val_is_none, send_warning, [])
开发者ID:lmregus,项目名称:Portfolio,代码行数:26,代码来源:rewrite.py

示例11: test_get_docstring

    def test_get_docstring(self):
        node = ast.parse('def foo():\n  """line one\n  line two"""')
        self.assertEqual(ast.get_docstring(node.body[0]),
                         'line one\nline two')

        node = ast.parse('async def foo():\n  """spam\n  ham"""')
        self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
开发者ID:mancoast,项目名称:cpython,代码行数:7,代码来源:test_ast.py

示例12: get_changes

	def get_changes(self):
		if not self.text_before: return
		tree_before = ast.parse(self.text_before)
		tree = ast.parse(self.text)
		if ast.dump(tree)==ast.dump(tree_before): print('status','no changes to the script')
		else: 
			print('status','executing changes to %s'%self.file)
			# identify changed nodes in the tree and execute
			# note that this feature reruns any changed child of the script parent
			#! track line numbers are report to the user?
			tree_before,tree = [[self.CodeChunk(i,index=ii) for ii,i in 
				enumerate(ast.iter_child_nodes(ast.parse(t)))]
				for t in [self.text_before,self.text]]
			intersect = set.intersection(set(tree),set(tree_before))
			novel = list(set.difference(set(tree),intersect))
			novel_linenos = set([i.this.lineno for i in novel])
			class CodeSurgery(ast.NodeTransformer):
				def visit(self, node):
					if hasattr(node,'lineno') and node.lineno not in novel_linenos: 
						return ast.parse('last_lineno = %d'%node.lineno).body[0]
					else: return ast.NodeTransformer.generic_visit(self,node)
			code_ready = ast.fix_missing_locations(CodeSurgery().visit(ast.parse(self.text)))
			# run the remainder
			out = self.namespace
			#! exec to eval for python <2.7.15
			eval(compile(code_ready,filename='<ast>',mode='exec'),out,out)
开发者ID:bradleyrp,项目名称:factory,代码行数:26,代码来源:reexec.py

示例13: OnApply

    def OnApply(self, event):
        """Event handler for Apply button"""

        # See if we have valid python
        try:
            ast.parse(self.macros)
        except:
            # Grab the traceback and print it for the user
            s = StringIO()
            e = exc_info()
            # usr_tb will more than likely be none because ast throws
            #   SytnaxErrorsas occurring outside of the current
            #   execution frame
            usr_tb = get_user_codeframe(e[2]) or None
            print_exception(e[0], e[1], usr_tb, None, s)
            post_command_event(self.parent, self.MacroErrorMsg,
                               err=s.getvalue())
            success = False
        else:
            self.result_ctrl.SetValue('')
            post_command_event(self.parent, self.MacroReplaceMsg,
                               macros=self.macros)
            post_command_event(self.parent, self.MacroExecuteMsg)
            success = True

        event.Skip()
        return success
开发者ID:catherinedevlin,项目名称:pyspread,代码行数:27,代码来源:_dialogs.py

示例14: compare_strings

def compare_strings(a,b):
    t1 = ast.parse(a)
    t2 = ast.parse(b)
    comp = Compare()
    val = comp.compare(t1,t2)
    d = comp.d
    return val
开发者ID:EconForge,项目名称:dolang,代码行数:7,代码来源:pattern.py

示例15: _analyzeGens

def _analyzeGens(top, absnames):
    genlist = []
    for g in top:
        if isinstance(g, _UserCode):
            tree = g
        elif isinstance(g, (_AlwaysComb, _AlwaysSeq, _Always)):
            f = g.func
            s = inspect.getsource(f)
            s = _dedent(s)
            tree = ast.parse(s)
            #print ast.dump(tree)
            tree.sourcefile  = inspect.getsourcefile(f)
            tree.lineoffset = inspect.getsourcelines(f)[1]-1
            tree.symdict = f.func_globals.copy()
            tree.callstack = []
            # handle free variables
            tree.nonlocaldict = {}
            if f.func_code.co_freevars:
                for n, c in zip(f.func_code.co_freevars, f.func_closure):
                    obj = _cell_deref(c)
                    if isinstance(g, _AlwaysComb):
                        if not ( isinstance(obj, (int, long, EnumType,_Signal)) or \
                                 _isMem(obj) or _isTupleOfInts(obj)
                               ):
                            info =  "File %s, line %s: " % (tree.sourcefile, tree.lineoffset)
                            print type(obj)
                            raise ConversionError(_error.UnsupportedType, n, info)
                    tree.symdict[n] = obj
                    # currently, only intbv as automatic nonlocals (until Python 3.0)
                    if isinstance(obj, intbv):
                        tree.nonlocaldict[n] = obj
            tree.name = absnames.get(id(g), str(_Label("BLOCK"))).upper()
            v = _FirstPassVisitor(tree)
            v.visit(tree)
            if isinstance(g, _AlwaysComb):
                v = _AnalyzeAlwaysCombVisitor(tree, g.senslist)
            elif isinstance(g, _AlwaysSeq):
                v = _AnalyzeAlwaysSeqVisitor(tree, g.senslist, g.reset, g.sigregs, g.varregs)
            else:
                v = _AnalyzeAlwaysDecoVisitor(tree, g.senslist)
            v.visit(tree)
        else: # @instance
            f = g.gen.gi_frame
            s = inspect.getsource(f)
            s = _dedent(s)
            tree = ast.parse(s)
            # print ast.dump(tree)
            tree.sourcefile = inspect.getsourcefile(f)
            tree.lineoffset = inspect.getsourcelines(f)[1]-1
            tree.symdict = f.f_globals.copy()
            tree.symdict.update(f.f_locals)
            tree.nonlocaldict = {}
            tree.callstack = []
            tree.name = absnames.get(id(g), str(_Label("BLOCK"))).upper()
            v = _FirstPassVisitor(tree)
            v.visit(tree)
            v = _AnalyzeBlockVisitor(tree)
            v.visit(tree)
        genlist.append(tree)
    return genlist
开发者ID:Alisa-lisa,项目名称:uni-stuff,代码行数:60,代码来源:_analyze.py


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