用python编写的规则引擎。

rule的Python项目详细描述


规则

用python编写的规则引擎。

规则是列表表达式的json/yaml字符串或python对象。 表达式类似于[op, arg0, arg1, ..., argn],而op是运算符, 而arg0..n是运算符的参数。任何参数都可以是另一个表达式。

为了便于编写,将尝试将第一个参数解析为上下文参数。 或者,您可以使用特殊的var运算符来指示上下文参数。

用法

>>> from rule import Rule
>>>
>>> context = dict(a=1, world='hello')
>>> Rule(['=', ['var', 'a'], 1]).match(context)
True
>>> Rule("['=', ['var', 'a'], 1]").match(context)
True
>>> Rule(['=', 'a', 1]).match(context)
True
>>> Rule(['=', 'hello', 'hello']).match(context)
True
>>> Rule(['=', 'world', 'hello']).match(context)
True
>>> Rule(['<', 'a', 10]).match(context)
True
>>> Rule(['=', ['>', 'a', 10], False]).match(context)
True
>>>
>>> context = dict(ldap_id='Harry', hosts='sa,sb,sc', reason='hehe', nologin=False,
...                group='wheel,sysadmin,platform', package='dev-python/sa-tools',
...                branch='release', cc='Tony,Mike',)
>>> Rule(['contains', 'hosts', 'sa,']).match(context)
True
>>> Rule(['contains', ['split', 'hosts', ','], 'sa']).match(context)
True
>>> Rule(['in', 'branch', 'master', 'release']).match(context)
True
>>> Rule(['=', 'ldap_id', 'Harry']).match(context)
True
>>> Rule(['match', 'package', 'dev-python/*']).match(context)
True
>>> Rule(['regex', 'package', 'dev-python/.*']).match(context)
True
>>> Rule(['is', 'nologin', False]).match(context)
True

有关更多支持的运算符,请参见rule/op.py

许可证

http://tclh123.mit-license.org/

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Eclipse内存分析器(MAT):不显示当前正在运行的进程   java Apache Velocity:转义字符不能作为关联数组键用于PHP   不截断零的java格式十进制输出   在另一个类文件中调用时返回空值的java getter   java集合获取连接   java解析json使用Gson登录系统应用程序强制关闭   java DelferredResult带有两个请求的ajax请求   java可降低功耗,同时应使用无线   java BoxLayout无法共享错误?   java如何使用计时器制作闹钟   java使用OAuth2保护RESTWeb服务:一般原则   java在一个jframe上显示多个图像和按钮