當前位置: 首頁>>代碼示例>>Python>>正文


Python ast.parse方法代碼示例

本文整理匯總了Python中ast.parse方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.parse方法的具體用法?Python ast.parse怎麽用?Python ast.parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在ast的用法示例。


在下文中一共展示了ast.parse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: ast

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def ast(self) -> ast.Module:  # type: ignore
        """Abstract Syntax Tree (AST) representation of the source_file.

        This is cached locally and updated if the source_file is changed.

        Returns:
            Parsed AST for the source file.

        Raises:
            TypeError: if ``source_file`` is not set.
        """
        if self._ast is None:
            if not self.source_file:
                raise TypeError("Source_file property is set to NoneType.")

            with open(self.source_file, "rb") as src_stream:
                self._ast = ast.parse(src_stream.read())
        return self._ast 
開發者ID:EvanKepner,項目名稱:mutatest,代碼行數:20,代碼來源:api.py

示例2: _format_traceback_frame

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def _format_traceback_frame(self, io, tb):  # type: (IO, ...) -> Tuple[Any]
        frame_info = inspect.getframeinfo(tb)
        filename = frame_info.filename
        lineno = frame_info.lineno
        function = frame_info.function
        line = frame_info.code_context[0]

        stripped_line = line.lstrip(" ")
        try:
            tree = ast.parse(stripped_line, mode="exec")
            formatted = self._format_tree(tree, stripped_line, io)
            formatted = (len(line) - len(stripped_line)) * " " + formatted
        except SyntaxError:
            formatted = line

        return (
            io.format("<c1>{}</c1>".format(filename)),
            "<fg=blue;options=bold>{}</>".format(lineno) if not PY2 else lineno,
            "<b>{}</b>".format(function),
            formatted,
        ) 
開發者ID:sdispater,項目名稱:clikit,代碼行數:23,代碼來源:exception_trace.py

示例3: load_test_cases

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def load_test_cases():
    base_path = os.path.dirname(__file__)
    test_case_path = os.path.join(base_path, "test_cases")
    test_case_files = os.listdir(test_case_path)

    test_cases = []

    for fname in test_case_files:
        if not fname.endswith(".py"):
            continue

        fullpath = os.path.join(test_case_path, fname)
        data = open(fullpath).read()
        tree = ast.parse(data, fullpath)
        codes, messages = extract_expected_errors(data)

        test_cases.append((tree, fullpath, codes, messages))

    return test_cases 
開發者ID:graphql-python,項目名稱:gql,代碼行數:21,代碼來源:test_flake8_linter.py

示例4: get_top_imported_names

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def get_top_imported_names(file: str) -> Set[str]:
    """Collect names imported in given file.

    We only collect top-level names, i.e. `from foo.bar import baz`
    will only add `foo` to the list.
    """
    if not file.endswith(".pyi"):
        return set()
    with open(os.path.join(file), "rb") as f:
        content = f.read()
    parsed = ast.parse(content)
    top_imported = set()
    for node in ast.walk(parsed):
        if isinstance(node, ast.Import):
            for name in node.names:
                top_imported.add(name.name.split('.')[0])
        elif isinstance(node, ast.ImportFrom):
            if node.level > 0:
                # Relative imports always refer to the current package.
                continue
            assert node.module
            top_imported.add(node.module.split('.')[0])
    return top_imported 
開發者ID:python,項目名稱:typeshed,代碼行數:25,代碼來源:migrate_script.py

示例5: _inferWaiter

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def _inferWaiter(gen):
    f = gen.gi_frame
    s = inspect.getsource(f)
    s = _dedent(s)
    root = ast.parse(s)
    root.symdict = f.f_globals.copy()
    root.symdict.update(f.f_locals)
    # print ast.dump(root)
    v = _YieldVisitor(root)
    v.visit(root)
    if v.kind == _kind.EDGE_TUPLE:
        return _EdgeTupleWaiter(gen)
    if v.kind == _kind.SIGNAL_TUPLE:
        return _SignalTupleWaiter(gen)
    if v.kind == _kind.DELAY:
        return _DelayWaiter(gen)
    if v.kind == _kind.EDGE:
        return _EdgeWaiter(gen)
    if v.kind == _kind.SIGNAL:
        return _SignalWaiter(gen)
    # default
    return _Waiter(gen) 
開發者ID:myhdl,項目名稱:myhdl,代碼行數:24,代碼來源:_Waiter.py

示例6: _makeAST

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def _makeAST(f):
    # Need to look at the flags used to compile the original function f and
    # pass these same flags to the compile() function. This ensures that
    # syntax-changing __future__ imports like print_function work correctly.
    orig_f_co_flags = f.__code__.co_flags
    # co_flags can contain various internal flags that we can't pass to
    # compile(), so strip them out here
    valid_flags = 0
    for future_feature in __future__.all_feature_names:
        feature = getattr(__future__, future_feature)
        valid_flags |= feature.compiler_flag
    s = inspect.getsource(f)
    s = _dedent(s)
    # use compile instead of ast.parse so that additional flags can be passed
    flags = ast.PyCF_ONLY_AST | (orig_f_co_flags & valid_flags)
    tree = compile(s, filename='<unknown>', mode='exec',
        flags=flags, dont_inherit=True)
    # tree = ast.parse(s)
    tree.sourcefile = inspect.getsourcefile(f)
    tree.lineoffset = inspect.getsourcelines(f)[1] - 1
    return tree 
開發者ID:myhdl,項目名稱:myhdl,代碼行數:23,代碼來源:_util.py

示例7: compile_expression

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def compile_expression(exp):
    cached = cache.get(exp)
    if cached is not None:
        return cached
    _exp = ast.parse(exp)
    nodes = [node for node in ast.walk(_exp)]
    if len(nodes) < 2 or not isinstance(nodes[1], ast.Expr):
        raise ExpressionError("%s is not Expression" % exp)
    for node in nodes:
        if isinstance(node, ast.Call):
            raise ExpressionError("Call method is forbidden")
        if isinstance(node, ast.Lambda):
            raise ExpressionError("Lambda is strongly forbidden")
    result = compile(exp, '<string>', mode='eval')
    cache[exp] = result
    return result 
開發者ID:moira-alert,項目名稱:worker,代碼行數:18,代碼來源:expression.py

示例8: explodeCode

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def explodeCode(string):
    lines = string.splitlines()
    total = len(lines)
    if total == 0:
        return [None, None]
    a = ast.parse(string)
    forms = []
    totalForms = len(a.body)
    for i in range(totalForms):
        start = a.body[i].lineno
        if i >= totalForms - 1:
            end = total
        else:
            end = a.body[i+1].lineno - 1
        forms.append(toForm(lines, {"start": start, "end": end}))
    return forms 
開發者ID:LightTable,項目名稱:Python,代碼行數:18,代碼來源:ltmain.py

示例9: _grab_ast

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def _grab_ast(repo, abspath):
  """Parses the Python file indicated by `abspath`.

  Args:
    * repo (RecipeRepo) - The repo which contains `abspath`. Used for error
      reporting.
    * abspath (str) - The absolute (native) path to the Python file to parse.

  Returns the Python AST object if the file exists and is parsable. Otherwise
  logs an error and returns None.
  """
  assert isinstance(repo, RecipeRepo), type(repo)
  relpath = os.path.relpath(abspath, repo.path)
  assert '..' not in relpath
  try:
    with open(abspath, 'rb') as f:
      return ast.parse(f.read(), relpath)
  except SyntaxError as ex:
    LOGGER.warn('skipping %s: bad syntax: %s', _to_posix(relpath), ex)
  except OSError as ex:
    LOGGER.warn('skipping %s: %s', _to_posix(relpath), ex)
  return None 
開發者ID:luci,項目名稱:recipes-py,代碼行數:24,代碼來源:cmd.py

示例10: parse_parameter

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def parse_parameter(param):
  """Parses a recipe parameter into a Doc.Parameter.

  Args:
    * param (recipe_api.Property) - The parameter to parse.

  Returns Doc.Parameter.
  """
  assert isinstance(param, recipe_api.Property), type(param)
  default = None
  if param._default is not recipe_api.PROPERTY_SENTINEL:
    default = json.dumps(param._default)

  return doc.Doc.Parameter(
    docstring=param.help,
    kind=param.kind.schema_proto() if param.kind else None,
    default_json=default) 
開發者ID:luci,項目名稱:recipes-py,代碼行數:19,代碼來源:cmd.py

示例11: compile_ast

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def compile_ast(self, source, filename = "<input>", symbol = "single"):
        # Here, we try to compile the relevant code. It may throw an
        # exception indicating that the command is not complete, in
        # which case we cannot proceed, but a full command will be
        # supplied eventually.
        compiled = code.compile_command(source, filename, symbol)
        # If the compilation succeeded, as indicated by its object not being
        # None, and no exception having occurred, parse it with AST and
        # store that.
        if compiled != None:
            self.latest_parsed = ast.parse(source, filename, symbol)
            CaptureExprs().visit(self.latest_parsed)
            # Since latest_parsed has been altered to capture values computed
            # but not assigned, store an unaltered copy for testing.
            self.clean_parsed = ast.parse(source, filename, symbol)
            
            return compile(self.latest_parsed, filename, symbol) 
開發者ID:alexander-bauer,項目名稱:swirlypy,代碼行數:19,代碼來源:Recording.py

示例12: mi_parameters

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def mi_parameters(code, count_multi=True):
    '''Given a source code snippet, compute the necessary parameters to
    compute the Maintainability Index metric. These include:

        * the Halstead Volume
        * the Cyclomatic Complexity
        * the number of LLOC (Logical Lines of Code)
        * the percent of lines of comment

    :param multi: If True, then count multiline strings as comment lines as
        well. This is not always safe because Python multiline strings are not
        always docstrings.
    '''
    ast_node = ast.parse(code)
    raw = analyze(code)
    comments_lines = raw.comments + (raw.multi if count_multi else 0)
    comments = comments_lines / float(raw.sloc) * 100 if raw.sloc != 0 else 0
    return (h_visit_ast(ast_node).volume,
            ComplexityVisitor.from_ast(ast_node).total_complexity, raw.lloc,
            comments) 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:22,代碼來源:metrics.py

示例13: easy_debug

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def easy_debug(code: str, should_exec=False, ctx=None):
    res = to_tagged_ast(parse(code).result)
    c = py_compile(res)
    print("-----------code")
    print(code)
    print("-----------Python")
    print(dis.dis(code))
    print("-----------YaPyPy")
    print(dis.dis(c))
    print("-----------astpretty")
    astpretty.pprint(ast.parse(code))
    print("----------- Python exec result")
    exec(code, ctx or {})
    print("-----------YaPyPy exec result")
    if should_exec:
        exec(c, ctx or {})
    else:
        print("\t(skip)") 
開發者ID:Xython,項目名稱:YAPyPy,代碼行數:20,代碼來源:easy_debug.py

示例14: test_warning_calls

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def test_warning_calls():
        # combined "ignore" and stacklevel error
        base = Path(numpy.__file__).parent

        for path in base.rglob("*.py"):
            if base / "testing" in path.parents:
                continue
            if path == base / "__init__.py":
                continue
            if path == base / "random" / "__init__.py":
                continue
            # use tokenize to auto-detect encoding on systems where no
            # default encoding is defined (e.g. LANG='C')
            with tokenize.open(str(path)) as file:
                tree = ast.parse(file.read())
                FindFuncs(path).visit(tree) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:18,代碼來源:test_warnings.py

示例15: evaluate

# 需要導入模塊: import ast [as 別名]
# 或者: from ast import parse [as 別名]
def evaluate(self, node, filename=None):
        """
        Evaluate a source string or node, using ``filename`` when
        displaying errors.
        """
        if isinstance(node, string_types):
            self.source = node
            kwargs = {'mode': 'eval'}
            if filename:
                kwargs['filename'] = filename
            try:
                node = ast.parse(node, **kwargs)
            except SyntaxError as e:
                s = self.get_fragment(e.offset)
                raise SyntaxError('syntax error %s' % s)
        node_type = node.__class__.__name__.lower()
        handler = self.get_handler(node_type)
        if handler is None:
            if self.source is None:
                s = '(source not available)'
            else:
                s = self.get_fragment(node.col_offset)
            raise SyntaxError("don't know how to evaluate %r %s" % (
                node_type, s))
        return handler(node) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:27,代碼來源:markers.py


注:本文中的ast.parse方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。