微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python pyparsing 模块-operatorPrecedence() 实例源码

Python pyparsing 模块,operatorPrecedence() 实例源码

我们从Python开源项目中,提取了以下2代码示例,用于说明如何使用pyparsing.operatorPrecedence()

项目:imcsdk    作者:CiscoUcs    | 项目源码 | 文件源码
def parse_filter_str(self, filter_str):
        """
        method to parse filter string
        """

        prop = pp.WordStart(pp.alphas) + pp.Word(pp.alphanums +
                                                 "_").setResultsName("prop")
        value = (pp.QuotedString("'") | pp.QuotedString('"') | pp.Word(
            pp.printables, excludeChars=",")).setResultsName("value")
        types_ = pp.oneOf("re eq ne gt ge lt le").setResultsName("types")
        flags = pp.oneOf("C I").setResultsName("flags")
        comma = pp.Literal(',')
        quote = (pp.Literal("'") | pp.Literal('"')).setResultsName("quote")

        type_exp = pp.Group(pp.Literal("type") + pp.Literal(
            "=") + quote + types_ + quote).setResultsName("type_exp")
        flag_exp = pp.Group(pp.Literal("flag") + pp.Literal(
            "=") + quote + flags + quote).setResultsName("flag_exp")

        semi_expression = pp.Forward()
        semi_expression << pp.Group(pp.Literal("(") +
                                    prop + comma + value +
                                    pp.Optional(comma + type_exp) +
                                    pp.Optional(comma + flag_exp) +
                                    pp.Literal(")")
                                    ).setParseAction(
            self.parse_filter_obj).setResultsName("semi_expression")

        expr = pp.Forward()
        expr << pp.operatorPrecedence(semi_expression, [
            ("not", 1, pp.opAssoc.RIGHT, self.not_operator),
            ("and", 2, pp.opAssoc.LEFT, self.and_operator),
            ("or", self.or_operator)
        ])

        result = expr.parseString(filter_str)
        return result
项目:ucscsdk    作者:CiscoUcs    | 项目源码 | 文件源码
def parse_filter_str(self, self.or_operator)
        ])

        result = expr.parseString(filter_str)
        return result

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐