Imperium是一个python包,允许您轻松计算python表达式
imperium的Python项目详细描述
帝国
Imperium is a python package that allows you to easily evaluate python expressions
安装
pip install imperium
使用
^{pr2}$要访问给定主题中的数据,请使用上面所示的“$subject”键。在
检查主题是否具有属性
fromimperium.evaluatorimportExpressionobj={'name':'iPhone','model':'11 Pro','price':1299.90,'state':'new'}someother_obj={'name':'John Doe'}expr=Expression()res=expr.evaluate('exists("$subject.price", $subject)',subject=obj,source=someother_obj)# REMINDER: "$subject" key let's you access the object that you passed to the evaluate method (obj in this case)print(res)# Output: True
测试正则表达式
fromimperium.evaluatorimportExpressionobj={'name':'iPhone','model':'11 Pro','price':1299.90,'state':'new'}buyer={'fullname':'John Doe'}# Passing the name attribute of the subject# REMINDER: "$subject" let's you access the object/subject passed to the evaluate method (obj in this case)# and "$source" let's you access the optional source object passed to the evaluate method expression="exists('$subject.name', $subject) && matches('IPHONE', $subject['name'], 'i')"expr=Expression()res=expr.evaluate(expression,subject=obj,source=buyer)print(res)# Output: True
Imperium有内置函数来简化某些操作/验证。在
Function | Argument(s) | Description |
---|---|---|
exists() | key (Ex: $subject.price), $subject | Checks if the given attribute/key exists in the given subject. |
matches() | regex, value, flag (i or m) | Tests a regular expression |
date() | datestring, format | datestring and format are both optional, provide them to the function if you want to convert a date string to date object. If these two arguments are left emtpy, the function returns date object with current date and time. |
date_modify() | date, operation (+ | -), valuetype (days | months | years), value: int | This function can add and subtract days, months and years from a date object. |
- 项目
标签: