當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。