用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/