Simple and safe expression evaluator
Project description
safex
Safe and simple python expression evaluator.
Installation
pip install safex
Usage
from safex import eval_expression
# Binary operators
assert eval_expression("1 + 2 * 12 / 3") == 9
assert eval_expression("a + b", {"a": 1, "b": 2}) == 3
# Unary operators
assert eval_expression("not True") == False
assert eval_expression("not False") == True
# Comparisons
assert eval_expression("1 < 2 < 3") == True
# Name lookups
assert eval_expression("a", {"a": 1}) == 1
assert eval_expression("inc(x)", {"inc": lambda x: x + 1, "x": 1}) == 2
# Lists and dicts
assert eval_expression("[0, 1, 2, 3, 4, 5][0]") == 0
assert eval_expression("{'a': 1, 'b': 2}['a']") == 1
# Lambdas, map, filter
assert (
eval_expression(
"list(filter(lambda x: x % 2 == 1, map(lambda x: x * 3, a)))",
{"a": [0, 1, 2, 3, 4, 5]},
)
== [3, 9, 15]
)
assert eval_expression("list(filter(lambda x: x % 2 == 1, range(6)))") == [1, 3, 5]
assert eval_expression("list(filter(lambda x: x % 2 == 1, range(6)))") == [1, 3, 5]
assert eval_expression("list(sorted(range(5), reverse=True))") == [4, 3, 2, 1, 0]
assert eval_expression("(lambda *args: sum(args))(1, 2, 3)") == 6
# Attributes
assert eval_expression("person.name", {"person": person}) == "test"
assert eval_expression("person.address.city", {"person": person}) == "city"
event = {
'type': 'user_added',
'payload': {
'name': 'test',
'age': 17,
'emails': [
{'type': 'primary', 'email': 'test@test.com'},
{'type': 'secondary', 'email': 'test2@test.com'}
]
}
}
assert eval_expression(
"event['type'] == 'user_added' and event['payload']['age'] < 18", {"event": event}) == True
assert eval_expression(
"event['payload']['emails'][0]['email']", {"event": event}) == 'test@test.com'
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
safex-0.7.0.tar.gz
(4.2 kB
view details)
Built Distribution
safex-0.7.0-py3-none-any.whl
(4.0 kB
view details)
File details
Details for the file safex-0.7.0.tar.gz
.
File metadata
- Download URL: safex-0.7.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.9.7 Linux/5.8.0-1042-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3526105f40ae12f04b2f58f93cd1bd2c962913b1f322eec28a4a038cb92c10a8 |
|
MD5 | 7733e32e8b3d4284f4a4b0b00785cc6f |
|
BLAKE2b-256 | 2831b04ea0a9827df89461ee496ab813892af5946a68494ebdeda4eeec7574c7 |
File details
Details for the file safex-0.7.0-py3-none-any.whl
.
File metadata
- Download URL: safex-0.7.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.9.7 Linux/5.8.0-1042-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ef85d54360425eb75ce2b6a4faa22188ef9e0d81496370d6f5fd0d08b244221 |
|
MD5 | 38345ff9232adbb01f4d2b2159321708 |
|
BLAKE2b-256 | 7000d4218a58d411a85170676140411fba0028e0ed5d75854f94bc2f25bd9ecb |