Build expressions, serialize them as JSON, and evaluate them in Python
Project description
xpress - Build expressions, serialize them as JSON, and evaluate them in Python
xpress is inspired by JsonLogic but aims to terser at the cost of reduced features. It ONLY supports logical operators.
Virtues
xpress follows similar principles as JsonLogic
- Terse(er).
- Readable. As close to infix expressions as possible
- Consistent. 3-tuple expressions
["operand", "operator", "operand"]
joined byAND
and/orOR
- Secure. We never
eval()
- Flexible. Easy to add new operators, easy to build complex structures
Limitations
- Only logical operators are supported.
- Unary operators are not supported.
Examples
Simple
xpress.evaluate([1, "==", 1])
# True
This is a simple rule, equivalent to 1 == 1. A few things about the format:
- The operator is always at the 2nd position(index: 1) in the expression 3-tuple. There is only one operator per expression.
- The operands are either a literal, a variable or an array of literals and/or variables.
- Each value can be a string, number, boolean, array (non-associative), or null
Compound
Here we’re beginning to nest rules.
xpress.evaluate([[3, ">", 1], "and", [1, "<", 3]])
# True
In an infix language (like Python) this could be written as:
( (3 > 1) and (1 < 3) )
Data-Driven
Obviously these rules aren’t very interesting if they can only take static literal data. Typically xpress will be called with a rule object and a data object. You can use the var operator to get attributes of the data object. Here’s a complex rule that mixes literals and data. The pie isn’t ready to eat unless it’s cooler than 110 degrees, and filled with apples.
rules = [["var:temp", "<", 110], "and", ["var:pie.filling", "==", "apple"]]
data = { "temp" : 100, "pie" : { "filling" : "apple" } };
xpress.evaluate(rules, data);
# True
Always and Never
Sometimes the rule you want to process is “Always” or “Never.” If the first parameter passed to jsonLogic is a non-object, non-associative-array, it is returned immediately.
# Always
xpress.evaluate(True, data_will_be_ignored)
# True
# Never
xpress.evaluate(False, i_wasnt_even_supposed_to_be_here)
# False
Supported Operations
Accessing Data
var
Logical and Boolean Operators
==
===
!=
!==
or
and
Numeric Operators
>
,>=
,<
and<=
Array Operators
in
String Operators
in
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file xpression-0.1.0.tar.gz
.
File metadata
- Download URL: xpression-0.1.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.63.0 importlib-metadata/4.10.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c706be2eccbf552aaa5b472a06b3a405f1843c4ac72963e46f5606eaa2ba86b6 |
|
MD5 | 3b50bcbd75432c0bc5c38228dd7ef13b |
|
BLAKE2b-256 | bd0afd359114c2414edf95c991022260ac3bf8748435922dc9dce653548f34f5 |
File details
Details for the file xpression-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: xpression-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.63.0 importlib-metadata/4.10.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb3741ffef5437a1adfdcd9e2b750adeb907c6661e00545984089ee60a3b86db |
|
MD5 | d026d557ede77f5543758925dfb856d4 |
|
BLAKE2b-256 | ad921eebd9e272179b6ab3892d8dd8b1f1061e2988871e6f009f6e414bf57ccf |