Skip to main content

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'

Metadata

Release files for safex 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for safex 0.7.0
File Size Uploaded
safex-0.7.0.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for safex 0.7.0
File Interpreter ABI Platform
safex-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / safex-0.7.0.tar.gz

Download URL safex-0.7.0.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3526105f40ae12f04b2f58f93cd1bd2c962913b1f322eec28a4a038cb92c10a8
BLAKE2b-256 checksum
How to use checksums
2831b04ea0a9827df89461ee496ab813892af5946a68494ebdeda4eeec7574c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.7 Linux/5.8.0-1042-azure

Release files / safex-0.7.0-py3-none-any.whl

Download URL safex-0.7.0-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ef85d54360425eb75ce2b6a4faa22188ef9e0d81496370d6f5fd0d08b244221
BLAKE2b-256 checksum
How to use checksums
7000d4218a58d411a85170676140411fba0028e0ed5d75854f94bc2f25bd9ecb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.7 Linux/5.8.0-1042-azure

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page