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


Python Scanner.peek方法代码示例

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


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

示例1: kwargs

# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import peek [as 别名]
class Parser:
    # todo: think of option kwargs (perhaps fuzzy json parse?)
    def __init__(self, **kwargs):
        """
        :param kwargs:
            simple_value: default True, if set to False uses JSONValueModel
            file_name: name of the file to parse
            io: string io, pass if file_name is None
            predict_references: default False, if true will try to predict links in data model
        :return:
        """
        self.simple_value = kwargs.get("simple_value", True)
        self.predict_references = kwargs.get("predict_references", False)
        file_name = kwargs.get("file_name", None)
        io = kwargs.get("io", None)

        self.parsed_string = u""
        self.result = {}
        self.scanner = Scanner(file_name)
        if file_name is None and io is not None:
            self.scanner.file_input = io

    def parse_json(self):
        """
        parses json loaded by scanner
        raises TokenMismatchException or UnexpectedTokenException
        :return: parsed dictionary of JSONValueModel
        """

        self.result = self.__parse_object()

    def __parse_value(self):
        """
        Parses value, which can be string, number, array, object, null, true, false
        :return: parsed value (list, dict, unicode, int, float, bool, None)
        """
        char = self.scanner.peek(1)

        if char == u'{':
            return self.__parse_object()
        elif char == u'[':
            return self.__parse_array()
        elif char == u'n':
            return self.__parse_null()
        elif char == u't':
            return self.__parse_bool()
        elif char == u'f':
            return self.__parse_bool()
        elif char == u'"':
            return self.__parse_string()
        else:
            return self.__parse_number()

    def __parse_object(self):
        """
        Parses object
        :return: parsed dictionary
        """
        result = {}
        self.__advance_requiring(TokenType.lbrace)
        try:
            result = self.__parse_members()
        except UnexpectedTokenException:
            pass
        self.__advance_requiring(TokenType.rbrace)

        return result

    def __parse_array(self):
        """
        Parses array of values
        :return: parsed list
        """
        result = []
        self.__advance_requiring(TokenType.lsquare)
        try:
            result = self.__parse_elements()
        except UnexpectedTokenException:
            pass
        self.__advance_requiring(TokenType.rsquare)

        return result

    def __parse_members(self):
        """
        Parses members of potential object
        :return: dictionary with members
        """
        result = {}
        while True:
            try:
                name, value = self.__parse_pair()
                if self.simple_value is False:
                    result[name] = JSONValueModel(name, value, predict_references=self.predict_references)
                else:
                    result[name] = value
            except UnexpectedTokenException:
                raise UnexpectedTokenException
            try:
                self.__advance_requiring(TokenType.comma)
#.........这里部分代码省略.........
开发者ID:theddnc,项目名称:json2code,代码行数:103,代码来源:parser.py


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