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


Python ast.Interactive方法代码示例

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


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

示例1: parse

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Interactive [as 别名]
def parse(self, code, file_name, interactive=False):
        self.lexer.input(code)
        tree = self.parser.parse(lexer = self.lexer, debug=False)
        if errors:
            first_error = None
            for line, msg in errors:
                if line == -1:
                    print('{}\t{}'.format(file_name, msg))
                else:
                    print('{}:{}\t{}'.format(file_name, line, msg))
            del errors[:]
            self.parser.restart()
            self.lexer = IndentLexer()
            raise SyntaxError
        if interactive:
            return ast.Interactive(tree)
        else:
            return ast.Module(tree) 
开发者ID:yaksok,项目名称:yaksok,代码行数:20,代码来源:yacc.py

示例2: eval_block

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Interactive [as 别名]
def eval_block(code, namespace=None, filename="<string>"):
    """
    Execute a multi-line block of code in the given namespace

    If the final statement in the code is an expression, return
    the result of the expression.
    """
    tree = ast.parse(code, filename="<ast>", mode="exec")
    if namespace is None:
        namespace = {}
    catch_display = _CatchDisplay()

    if isinstance(tree.body[-1], ast.Expr):
        to_exec, to_eval = tree.body[:-1], tree.body[-1:]
    else:
        to_exec, to_eval = tree.body, []

    for node in to_exec:
        compiled = compile(Module([node], []), filename=filename, mode="exec")
        exec(compiled, namespace)

    with catch_display:
        for node in to_eval:
            compiled = compile(
                ast.Interactive([node]), filename=filename, mode="single"
            )
            exec(compiled, namespace)

    return catch_display.output 
开发者ID:altair-viz,项目名称:altair,代码行数:31,代码来源:execeval.py

示例3: test_module

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Interactive [as 别名]
def test_module(self):
        m = ast.Interactive([ast.Expr(ast.Name("x", ast.Store()))])
        self.mod(m, "must have Load context", "single")
        m = ast.Expression(ast.Name("x", ast.Store()))
        self.mod(m, "must have Load context", "eval") 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:7,代码来源:test_ast.py

示例4: _run_ast_nodes

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Interactive [as 别名]
def _run_ast_nodes(self, nodelist, name):
        output_name = None
        if not nodelist:
            return True, output_name

        if isinstance(nodelist[-1], _assign_nodes):
            asg = nodelist[-1]
            if isinstance(asg, ast.Assign) and len(asg.targets) == 1:
                target = asg.targets[0]
            elif isinstance(asg, _single_targets_nodes):
                target = asg.target
            else:
                target = None
            if isinstance(target, ast.Name):
                output_name = target.id
                nnode = ast.Expr(ast.Name(target.id, ast.Load()))
                ast.fix_missing_locations(nnode)
                nodelist.append(nnode)

        if isinstance(nodelist[-1], ast.Expr):
            to_run_exec, to_run_interactive = nodelist[:-1], nodelist[-1:]
        else:
            to_run_exec, to_run_interactive = nodelist, []

        try:
            mod = ast.Module(to_run_exec)
            code = compile(mod, name, 'exec')
            if self._run_code(code):
                return True, output_name

            for node in to_run_interactive:
                mod = ast.Interactive([node])
                code = compile(mod, name, 'single')
                if self._run_code(code):
                    return True, output_name
        except BaseException:
            return True, output_name

        return False, output_name 
开发者ID:jupytercalpoly,项目名称:reactivepy,代码行数:41,代码来源:execute.py

示例5: interpret_code

# 需要导入模块: import ast [as 别名]
# 或者: from ast import Interactive [as 别名]
def interpret_code(code):

    # # Setting this var lets pip be used from a thread other than its original import
    # import threading
    # _log_state = threading.local()
    # if not hasattr(_log_state, 'indentation'):
    #     _log_state.indentation = 0

    try:
        sys.stdout.can_omit = True
        # The input is first parsed as ast, then if the last statement
        # is an Expr compiled partially in single mode. This means
        # that the last statement output is printed, as in the normal
        # Python interpreter

        components = ast.parse(code).body

        # print('components are', components)

        # exec all but the last ast component in exec mode
        if len(components) > 1:
            for component in components[:-1]:
                c = compile(ast.Module([component]), '<stdin>', mode='exec')
                exec(c, user_locals, user_globals)

        # if the last ast component is an Expr, compile in single mode to print it
        if isinstance(components[-1], ast.Expr):
            c = compile(ast.Interactive([components[-1]]), '<stdin>', mode='single')
        else:
            c = compile(ast.Module([components[-1]]), '<stdin>', mode='exec')
        exec(c, user_locals, user_globals)

    except KeyboardInterrupt as e:
        print('')
        traceback.print_exc()
        osc.sendMsg(b'/interpreter', [b'keyboard_interrupted'], port=send_port,
                    typehint='b')
    except Exception as e:
        traceback.print_exc()
    finally:
        sys.stdout.can_omit = False

    complete_execution()

    # instructions = ast.parse(code)
    # if isinstance(instructions.body[-1], ast.Expr):
    #     exec('print({})'.format(instructions.body[-1].value.id)) 
开发者ID:inclement,项目名称:Pyonic-interpreter,代码行数:49,代码来源:interpreter.py


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