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


Python Context.serialize方法代码示例

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


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

示例1: Repl

# 需要导入模块: from context import Context [as 别名]
# 或者: from context.Context import serialize [as 别名]

#.........这里部分代码省略.........
            print ()
            while True:
                line = self.prompt(message)
                self.eval(line)

        except (KeyboardInterrupt, EOFError):
            if self.context.variables:
                self.exit_prompt()

    def prompt(self, message):
        return prompt(
            message,
            history=self.history,
            lexer=MatlabLexer,
            completer=self.get_word_completer(),
            display_completions_in_columns=True,
            mouse_support=True
        )

    def exit_prompt(self):
        try:
            while True:
                option = self.prompt("Save the workspace? (y/n) ").lower()
                if option == 'y':
                    self.save()
                    break
                elif option == 'n':
                    raise KeyboardInterrupt
        except (KeyboardInterrupt, EOFError):
            print (Fore.YELLOW)
            print (" Workspace not saved")
            print ()

    def eval(self, line):
        if line.startswith("save "):
            filename = line.replace("save ", "")
            self.save(filename)
            return

        if line.startswith("load "):
            filename = line.replace("load ", "")
            self.load(filename)
            return

        tokens = Lexer.lex(line)
        try:
            parse_tree = Parser(tokens).parse()
            if parse_tree:
                output = self.context.evaluate(parse_tree)
                if output:
                    print(Fore.GREEN + output)

        except MatlabetteError as e:
            print(Fore.RED)
            print(" Error: " + e.message)
            print()

    def load(self, filename):
        print ()
        try:
            with open(filename, 'r') as f:
                print(Fore.BLUE + " Loading workspace from '{}'"
                      .format(os.path.abspath(filename)))
                input_line = f.readline()
                while input_line:
                    self.eval(input_line)
                    input_line = f.readline()
                print(Fore.BLUE + " Done")
        except IOError:
            print(Fore.RED + " Error: failed to open '{}'"
                  .format(os.path.abspath(filename)))
        print ()

    def load_default(self):
        if os.path.isfile(workspace_file):
            self.load(workspace_file)
        else:
            print (Fore.YELLOW)
            print(" Default workspace doesn't exist. To create it, type save")
            print ()

    def save(self, filename=workspace_file):
        print()
        try:
            with open(filename, 'w') as f:
                f.write(self.context.serialize())
                print(Fore.BLUE + " Workspace saved to '{}'"
                      .format(os.path.abspath(filename)))
        except IOError:
            print(Fore.RED + " Error: failed to open '{}'"
                  .format(os.path.abspath(filename)))
        print()

    @staticmethod
    def exit():
        raise KeyboardInterrupt

    @staticmethod
    def help():
        return """
开发者ID:andela-hthuo,项目名称:bc-6-matlabette,代码行数:104,代码来源:repl.py


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