• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python pypeg2.parse函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pypeg2.parse函数的典型用法代码示例。如果您正苦于以下问题:Python parse函数的具体用法?Python parse怎么用?Python parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: test_comment

	def test_comment(self):
		"""Test that simple comments are parsed correctly"""
		p = parse(self.comment, Comment)
		self.assertEqual(p, self.comment.replace("#", ""))
		
		p = parse(self.extra_white_space, Comment)
		self.assertEqual(p, self.extra_white_space.replace("#", ""))

		p = parse(self.empty_comment, Comment)
		self.assertEqual(p, self.empty_comment.replace("#", ""))
开发者ID:AlejandroFrias,项目名称:MindSketch,代码行数:10,代码来源:test_basic_parsing.py


示例2: invenio_query

    def invenio_query(pattern, index='records-hep'):

        # Enhance query first
        # for enhancer in query_enhancers():
        #     enhancer(self, **kwargs)

        try:
            query = pypeg2.parse(pattern, Main, whitespace="")

            for walker in walkers:
                query = query.accept(walker)
        except SyntaxError:
            query = MalformedQuery("")

        try:
            search_walker = ElasticSearchNoKeywordsDSL()
            query.accept(search_walker)
            query = Q('multi_match',
                      query=pattern,
                      fields=get_fields_by_index(index),
                      zero_terms_query="all")
        except QueryHasKeywords:
            query = query.accept(ElasticSearchDSL(
                current_app.config.get(
                    "SEARCH_ELASTIC_KEYWORD_MAPPING", {}
                )
            ))
        finally:
            if current_app.debug:
                current_app.logger.info(json.dumps(query.to_dict(), indent=4))
            return query
开发者ID:kaplun,项目名称:inspire-next,代码行数:31,代码来源:query_factory.py


示例3: parse_headers

def parse_headers(content_disposition):
    """Build a ContentDisposition from header values."""
    # https://bitbucket.org/logilab/pylint/issue/492/
    # pylint: disable=no-member

    # We allow non-ascii here (it will only be parsed inside of qdtext, and
    # rejected by the grammar if it appears in other places), although parsing
    # it can be ambiguous.  Parsing it ensures that a non-ambiguous filename*
    # value won't get dismissed because of an unrelated ambiguity in the
    # filename parameter. But it does mean we occasionally give
    # less-than-certain values for some legacy senders.
    content_disposition = content_disposition.decode('iso-8859-1')
    log.rfc6266.debug("Parsing Content-Disposition: {}".format(
        content_disposition))
    # Our parsing is relaxed in these regards:
    # - The grammar allows a final ';' in the header;
    # - We do LWS-folding, and possibly normalise other broken
    #   whitespace, instead of rejecting non-lws-safe text.
    # XXX Would prefer to accept only the quoted whitespace
    # case, rather than normalising everything.
    content_disposition = normalize_ws(content_disposition)
    try:
        parsed = peg.parse(content_disposition, ContentDispositionValue)
    except (SyntaxError, DuplicateParamError, InvalidISO8859Error):
        log.rfc6266.exception("Error while parsing Content-Disposition")
        return ContentDisposition()
    else:
        return ContentDisposition(disposition=parsed.dtype,
                                  assocs=parsed.params)
开发者ID:B0073D,项目名称:qutebrowser,代码行数:29,代码来源:rfc6266.py


示例4: fromIslevString

    def fromIslevString(bidstring):
        #pattern = re.compile(
        #    "(?P<bidder>V|Ø|N|S) (?P<tricks>[1-7])(?P<strain>UT|SP|HJ|RU|KL) *(?P<dbl>[PDR]*)")
        #match = pattern.match(bidstring)
        #print('bidstring >{}<'.format(bidstring))
        #if match:
        #    player = Seat.fromDKId(match.group("bidder"))
        #    tricks = match.group("tricks")
        #    strain = Strain.fromDKString(match.group("strain"))
        #    if not(match.group("dbl")):
        #        dbl = 'P'
        #    else:
        #        dbl = match.group("dbl")
        #else:
        #    raise (BaseException("bid exception"))
        if len(bidstring) < 3:
            #hack to deal with games that wont get played
            return Bid()

        res = parse(bidstring, parsing.Bid)
        if hasattr(res, 'not_played'):
            return Bid()
        elif hasattr(res, 'passed'):
            return Bid(strain = Strain.fromId('P'))
        else:
            res = res.played
            player = Seat.fromDKId(res.seat)
            tricks = int(res.tricks)
            strain = Strain.fromDKString(res.strain)
            if res.dbl == '':
                dbl = 'P'
            else:
                dbl = res.dbl
            return Bid(player, tricks, strain, dbl)
开发者ID:ib-m-j,项目名称:dev,代码行数:34,代码来源:bridgecore.py


示例5: parse_command

    def parse_command(self, command_string=None):

        class DateTime(str):
            grammar = optional([self.date_time_re, self.date_re, self.time_re, self.date_functions_re, self.user_macros_re])

        class AnniversaryName(str):
            grammar = self.anniversary_name_re

        class Add(str):
            grammar = self.add_command_re

        class Delete(str):
            grammar = self.delete_command_re

        class Edit(str):
            grammar = self.edit_command_re

        class Commands(List):
            grammar = [
                (attr("delete", Delete), attr("anniversaryName", AnniversaryName)),
                (attr("edit", Edit), attr("anniversaryName", AnniversaryName), attr("dateTime", DateTime)),
                (attr("add", Add), attr("anniversaryName", AnniversaryName), attr("dateTime", DateTime))
            ]

        return parse(command_string, Commands)
开发者ID:MuppetGate,项目名称:Alfred-Workflows-DateCalculator,代码行数:25,代码来源:macros_parser.py


示例6: test_multiline_comment

	def test_multiline_comment(self):
		p = parse(self.multiline, some(Comment))
		expected = map(str, self.multiline.split("\n"))
		hashes = len(expected) * ["#"]
		blanks = len(expected) * [""]
		expected = map(str.replace, expected, hashes, blanks)
		self.assertEqual(p, expected)
开发者ID:AlejandroFrias,项目名称:MindSketch,代码行数:7,代码来源:test_basic_parsing.py


示例7: parse_headers

 def parse_headers(content_disposition):
     """
     Build a ContentDisposition from header values.
     
     @param content_disposition contents of the disposition header
     @type bytes
     """
     # We allow non-ascii here (it will only be parsed inside of qdtext, and
     # rejected by the grammar if it appears in other places), although
     # parsing it can be ambiguous.  Parsing it ensures that a non-ambiguous
     # filename* value won't get dismissed because of an unrelated ambiguity
     # in the filename parameter. But it does mean we occasionally give
     # less-than-certain values for some legacy senders.
     content_disposition = content_disposition.decode('iso-8859-1')
     
     # Our parsing is relaxed in these regards:
     # - The grammar allows a final ';' in the header;
     # - We do LWS-folding, and possibly normalise other broken
     #   whitespace, instead of rejecting non-lws-safe text.
     # XXX Would prefer to accept only the quoted whitespace
     # case, rather than normalising everything.
     content_disposition = normalize_ws(content_disposition)
     try:
         parsed = peg.parse(content_disposition, ContentDispositionValue)
     except (SyntaxError, DuplicateParamError, InvalidISO8859Error):
         return ContentDisposition()
     else:
         return ContentDisposition(disposition=parsed.dtype,
                                   assocs=parsed.params)
开发者ID:testmana2,项目名称:test,代码行数:29,代码来源:E5RFC6266.py


示例8: test_code_snippets_with_comments

	def test_code_snippets_with_comments(self):
		for (language, snippet) in self.code_snippets:
			code_snippet = self.gen_code_snippet(snippet, language, self.comments)
			p = parse(code_snippet, CodeSnippet)
			self.assertEqual(p, snippet)
			self.assertEqual(map(compose, p.comments), self.comments)
			self.assertEqual(p.language, language)
开发者ID:AlejandroFrias,项目名称:MindSketch,代码行数:7,代码来源:test_basic_parsing.py


示例9: test_program_diff_env

    def test_program_diff_env(self):
        """Test simple 2 line program with different initial conditions."""
        simple_lines = \
            """
            y = x + 3;
            z = y + 5;
            """
        ast = parse(simple_lines, p.Program)
        prog = ast.to_simple()

        env_expected = dict(x=Number(2), y=Number(5), z=Number(10))
        env = dict(x=Number(2))
        env2 = prog.evaluate(env)
        self.assertEqual(len(env_expected), len(env2))
        for x in env_expected.keys():
            self.assertEqual(env_expected[x], env2[x])

        # show that we can reuse the program

        env_expected = dict(x=Number(9), y=Number(12), z=Number(17))
        env2['x'] = Number(9)
        env3 = prog.evaluate(env2)
        self.assertEqual(len(env_expected), len(env3))
        for x in env_expected.keys():
            self.assertEqual(env_expected[x], env3[x])
开发者ID:JeNeSuisPasDave,项目名称:lang-simple,代码行数:25,代码来源:test_parsing_simple.py


示例10: invenio_query

    def invenio_query(pattern, search):

        try:
            query = pypeg2.parse(pattern, Main, whitespace='')

            for walker in walkers:
                query = query.accept(walker)

        except SyntaxError:
            query = MalformedQuery("")

        try:
            search_walker = ElasticSearchNoKeywordsDSL()
            query.accept(search_walker)
            query = Q('multi_match',
                      query=pattern,
                      fields=search.default_fields(),
                      zero_terms_query="all")
        except QueryHasKeywords:
            query = query.accept(ElasticSearchDSL(
                current_app.config.get(
                    "SEARCH_ELASTIC_KEYWORD_MAPPING", {}
                )
            ))
        finally:
            return query
开发者ID:fschwenn,项目名称:inspire-next,代码行数:26,代码来源:query_factory.py


示例11: test_if_tx1yx2y

    def test_if_tx1yx2y(self):
        """Test parsing if statement if (x > y) {...} else {...}."""
        expected = "if (x > y)\n"
        expected += "{\n"
        expected += "  x = 1;\n"
        expected += "  y = x * 3;\n"
        expected += "}\n"
        expected += "else\n"
        expected += "{\n"
        expected += "  x = 2;\n"
        expected += "  y = 3 * x;\n"
        expected += "}"
        ast = parse(expected, p.If)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual(expected, c)
        self.assertEqual(
            If(
                GreaterThan(Variable('x'), Variable('y')),
                Sequence(
                    Assign('x', Number(1)),
                    Assign('y', Multiply(Variable('x'), Number(3)))),
                Sequence(
                    Assign('x', Number(2)),
                    Assign('y', Multiply(Number(3), Variable('x'))))),
            e)
开发者ID:JeNeSuisPasDave,项目名称:lang-simple,代码行数:27,代码来源:test_parsing_simple.py


示例12: test_assign_nxt

    def test_assign_nxt(self):
        """Test parsing assignment statement."""
        ast = parse("x = !true;", p.Assign)
        c = compose(ast, indent="  ", autoblank=False)
        s = ast.to_simple()

        self.assertEqual("x = !true;", c)
        self.assertEqual(Assign('x', Not(Boolean(True))), s)
开发者ID:JeNeSuisPasDave,项目名称:lang-simple,代码行数:8,代码来源:test_parsing_simple.py


示例13: test_assign_x12

    def test_assign_x12(self):
        """Test parsing assignment statement."""
        ast = parse("x = 1 + 2;", p.Assign)
        c = compose(ast, indent="  ", autoblank=False)
        s = ast.to_simple()

        self.assertEqual("x = 1 + 2;", c)
        self.assertEqual(Assign('x', Add(Number(1), Number(2))), s)
开发者ID:JeNeSuisPasDave,项目名称:lang-simple,代码行数:8,代码来源:test_parsing_simple.py


示例14: test_or_nxy

    def test_or_nxy(self):
        """Test parsing logical or expression !x || y."""
        ast = parse("!x || y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("!x || y", c)
        self.assertEqual(Or(Not(Variable('x')), Variable('y')), e)
开发者ID:JeNeSuisPasDave,项目名称:lang-simple,代码行数:8,代码来源:test_parsing_simple.py


示例15: test_number_fp_1

    def test_number_fp_1(self):
        """Test parsing floating point literal value 1.23."""
        ast = parse("1.23", p.term_expression)
        c = compose(ast, indent="  ", autoblank=False)
        n = ast.to_simple()

        self.assertEqual("1.23", c)
        self.assertEqual(Number(1.23), n)
开发者ID:JeNeSuisPasDave,项目名称:lang-simple,代码行数:8,代码来源:test_parsing_simple.py


示例16: test_lessthan_2m1m

    def test_lessthan_2m1m(self):
        """Test parsing less than expression -2 - -1."""
        ast = parse("-2 < -1", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("-2 < -1", c)
        self.assertEqual(LessThan(Number(-2), Number(-1)), e)
开发者ID:JeNeSuisPasDave,项目名称:lang-simple,代码行数:8,代码来源:test_parsing_simple.py


示例17: test_not_x

    def test_not_x(self):
        """Test parsing logical negation expression !x."""
        ast = parse("!x", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("!x", c)
        self.assertEqual(Not(Variable('x')), e)
开发者ID:JeNeSuisPasDave,项目名称:lang-simple,代码行数:8,代码来源:test_parsing_simple.py


示例18: test_subtraction_xy

    def test_subtraction_xy(self):
        """Test parsing subtraction expression x - y."""
        ast = parse("x - y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("x - y", c)
        self.assertEqual(Subtract(Variable('x'), Variable('y')), e)
开发者ID:JeNeSuisPasDave,项目名称:lang-simple,代码行数:8,代码来源:test_parsing_simple.py


示例19: test_and_xny

    def test_and_xny(self):
        """Test parsing logical and expression x && !y."""
        ast = parse("x && !y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("x && !y", c)
        self.assertEqual(And(Variable('x'), Not(Variable('y'))), e)
开发者ID:JeNeSuisPasDave,项目名称:lang-simple,代码行数:8,代码来源:test_parsing_simple.py


示例20: test_and_1my

    def test_and_1my(self):
        """Test parsing logical and expression -1 && y."""
        ast = parse("-1 && y", p.Expression)
        c = compose(ast, indent="  ", autoblank=False)
        e = ast.to_simple()

        self.assertEqual("-1 && y", c)
        self.assertEqual(And(Number(-1), Variable('y')), e)
开发者ID:JeNeSuisPasDave,项目名称:lang-simple,代码行数:8,代码来源:test_parsing_simple.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python builder.AtomicCmdBuilder类代码示例发布时间:2022-05-27
下一篇:
Python pypeg2.compose函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap