生成表达式的代码如下:

import json

rule = '[{"templateId":"1","fieldName": "product_code","fieldOperator","contains","fieldValue":"ZAKB"}]'
parsed_rule = json.loads(rule)
expression = "root." + parsed_rule[0]["fieldName"] + "." + parsed_rule[0]["fieldOperator"] + "(" + "\"" + parsed_rule[0]["fieldValue"] + "\"" + ")"
print(expression)

利用该表达式判断某个对象是否符合条件的代码如下:

class Product:
    def __init__(self, code):
        self.code = code

product = Product("ZAKB1234")

if eval(expression, {"root": product}):
    print("Product matches the rule")
else:
    print("Product does not match the rule")

在这个示例中,我们定义了一个Product类,其中包含一个code属性。我们使用这个类的实例来测试规则表达式。通过eval()函数,我们将规则表达式应用于Product实例来判断其是否符合规则。在这个例子中,Product实例的code属性包含字符串"ZAKB",所以规则表达式会返回True

jaxl对templateId1fieldName product_codefieldOperatorcontainsfieldValueZAKB规则解析生成表达式rootproduct_codecontainsZAKB具体代码怎么实现?以及利用该表达式判断某个对象是否符合条件

原文地址: https://www.cveoy.top/t/topic/g7tB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录