Skip to main content

No project description provided

Project description

Common Expression Language (Python)

The Common Expression Language (CEL) is a non-Turing complete language designed for simplicity, speed, safety, and portability. CEL's C-like syntax looks nearly identical to equivalent expressions in C++, Go, Java, and TypeScript. CEL is ideal for lightweight expression evaluation when a fully sandboxed scripting language is too resource intensive.

// Check whether a resource name starts with a group name.
resource.name.startsWith("/groups/" + auth.claims.group)
// Determine whether the request is in the permitted time window.
request.time - resource.age < duration("24h")
// Check whether all resource names in a list match a given filter.
auth.claims.email_verified && resources.all(r, r.startsWith(auth.claims.email))

Installing

pip install cel-bind

Overview

Determine the variables and functions you want to provide to CEL. Parse and check an expression to make sure it's valid. Then evaluate the output AST against some input.

Environment setup

Let's expose name and group variables to CEL:

import cel

env = {
    "name": cel.StringType(),
    "group": cel.StringType(),
}

Parse and Check

The parsing phase indicates whether the expression is syntactically valid and expands any macros present within the environment. Parsing and checking are more computationally expensive than evaluation, and it is recommended that expressions be parsed and checked ahead of time.

The parse and check phases are combined for convenience into the compile_to_checked_expr step:

pool = cel.DescriptorPool()
compiler = cel.Compiler(pool, env, None)
try:
    checked_expr = compiler.compile_to_checked_expr('name.startsWith("/groups/" + group)')
except Exception as e:
    logging.fatal("compiling error", exc_info=True)
    exit(1)

Evaluate

Build the expression plan with build_expression_plan and reuse it for multiple evaluations of the same expression:

interpreter = cel.Interpreter(pool, env, None)
expr_plan = interpreter.build_expression_plan(checked_expr)

res = interpreter.evaluate(expr_plan, {
    "name": "/groups/acme.co/documents/secret-stuff",
    "group": "acme.co"
})

print(res) # True

Objects

cel-bind supports the use of structured objects in its CEL type-checking and evaluation. First, declare the object type and add it to a cel.DescriptorPool:

schema = """
{
  "type": "Person",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    },
    "hobbies": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  },
  "required": ["name", "age"]
}
"""


pool = cel.DescriptorPool()
person_type = pool.add_json_schema("person", schema)

Then, use the returned object type when declaring your CEL environment:

env = {
    "person": person_type,
}

compiler = cel.Compiler(pool, env, None)
checked_expr = compiler.compile_to_checked_expr('person.age > 18')

Currently, only JSON schema is supported when declaring objects.

Extending CEL

Extension functions can be written in Python and then made available to CEL by registering it to a cel.FunctionRegistry:

def is_hiker(person):
    return "hiking" in person["hobbies"]

function_registry = cel.FunctionRegistry()
function_registry.add_function(
    "is_hiker",
    is_hiker,
    cel.BoolType(), # return type
    [person_type], # arguments type
)

Then use it in your CEL expression:

env = {
    "person": person_type,
}

compiler = cel.Compiler(pool, env, function_registry)
try:
    checked_expr = compiler.compile_to_checked_expr('person.age > 18 && is_hiker(person)')
except Exception as e:
    logging.fatal("compiling error", exc_info=True)
    exit(1)

interpreter = cel.Interpreter(pool, env, function_registry)
expr_plan = interpreter.build_expression_plan(checked_expr)

res = interpreter.evaluate(expr_plan, {
    "person": {
        "name": "Alice",
        "age": 20,
        "hobbies": ["cooking", "hiking"]
    }
})
print(res) # True

Type checking

Compiler.compile_to_checked_expr will raise an Exception if type checking fails. Some examples of of type checking errors are:

  • undefined object fields

    RuntimeError: ERROR: :1:7: undefined field 'last_name' not found in struct 'person_pkg.person'
    | person.last_name
    | ......^
    
  • undeclared reference:

    RuntimeError: ERROR: :1:1: undeclared reference to 'undeclared_var' (in container '')
    | undeclared_var > 5
    | ^
    
  • functions applied to incorrect types:

    RuntimeError: ERROR: :1:12: found no matching overload for '_==_' applied to '(int, string)'
    | person.age == "5"
    | ...........^
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cel_bind-0.8.4-cp314-cp314-manylinux_2_35_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.35+ x86-64

cel_bind-0.8.4-cp314-cp314-macosx_14_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.14macOS 14.0+ ARM64

cel_bind-0.8.4-cp313-cp313-manylinux_2_35_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.35+ x86-64

cel_bind-0.8.4-cp313-cp313-macosx_14_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

cel_bind-0.8.4-cp312-cp312-manylinux_2_35_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.35+ x86-64

cel_bind-0.8.4-cp312-cp312-macosx_14_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

cel_bind-0.8.4-cp311-cp311-manylinux_2_35_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.35+ x86-64

cel_bind-0.8.4-cp311-cp311-macosx_14_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

cel_bind-0.8.4-cp310-cp310-manylinux_2_35_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

cel_bind-0.8.4-cp310-cp310-macosx_14_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.10macOS 14.0+ ARM64

File details

Details for the file cel_bind-0.8.4-cp314-cp314-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for cel_bind-0.8.4-cp314-cp314-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 414cbd31e4d349bbd5ed36ae84454a68fbc2a7cba5c995fc6640c5313a8aee24
MD5 2d2d9257075025c1c04b4526066e51d7
BLAKE2b-256 ac3768a5c024e024a5dcaa8544622d3717fcf1af383cb28f96e1d876ee7d2031

See more details on using hashes here.

File details

Details for the file cel_bind-0.8.4-cp314-cp314-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for cel_bind-0.8.4-cp314-cp314-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 89460869dad1f6b77285f124f77a3177bb3fe3ef824eaee11b948ab49cfa0c7a
MD5 1c0dfc41d99411ef44cbebae46b3d556
BLAKE2b-256 80e31f8f4e518a83a35f18a771cdaadfe33320cb90780e78c932526aacd3a9e0

See more details on using hashes here.

File details

Details for the file cel_bind-0.8.4-cp313-cp313-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for cel_bind-0.8.4-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 955229fe456c33dc1870ccaf8798924c2caea0c0be7fcf2c06d245c1490c04a7
MD5 08244acc612875acdb18320495ab250e
BLAKE2b-256 4ec862e697b5c6322e6e3d53ea8aa595dfdf2aa23e6a8dd5ee1468ad9d089794

See more details on using hashes here.

File details

Details for the file cel_bind-0.8.4-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for cel_bind-0.8.4-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 ef19125abd9dbd7e38b6492dcb97477b1c11eabdb1a0b9e907bd601e1de4676f
MD5 ee7c84bfa77ed1186456adfc95ac08aa
BLAKE2b-256 f032a1145f56428eaa6840c4d12f0d4e96e6bf4a442ce75eac9459e2f5bc3d96

See more details on using hashes here.

File details

Details for the file cel_bind-0.8.4-cp312-cp312-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for cel_bind-0.8.4-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 d91296f6f9672d29000eda68cafcfbcf6be4089444ecb632804a0c3892757a38
MD5 37f321c8c785591d764352a69619afb5
BLAKE2b-256 2101491fb3841eac59d98f8b2967818441463b41646fc987989732a39f49ee61

See more details on using hashes here.

File details

Details for the file cel_bind-0.8.4-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for cel_bind-0.8.4-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 2e8a8436fd280b4cf9e7a3a84c652331b25d194c7109117fb6ce0d558ba329a6
MD5 4956c7dcbea98ac26cac6b5145ae0d02
BLAKE2b-256 12a40ad7d1a615476242f211216d12603ae93171c276ea60074bd2ff5ba98773

See more details on using hashes here.

File details

Details for the file cel_bind-0.8.4-cp311-cp311-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for cel_bind-0.8.4-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 4a8f842e06c83cec7bb130cd72838fcac5d690e067ed35d1fd8ca802f4f4b653
MD5 8d3e90f8ead6c1ded32b760d52178530
BLAKE2b-256 39cc0a091d7d7aae157eaccf153594a06f11428e6a4e95af4ce5a36a3115a962

See more details on using hashes here.

File details

Details for the file cel_bind-0.8.4-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for cel_bind-0.8.4-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 f11106ad08e42e7673baae4272fe9e3c10f78ef3947c61c8cf616e5cf31d7c9a
MD5 f427e15fd009eda4dfa51a19d57091bc
BLAKE2b-256 efb23aedcca82b263bba50f243314614f1c62b886089c0e68816866ab849cabc

See more details on using hashes here.

File details

Details for the file cel_bind-0.8.4-cp310-cp310-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for cel_bind-0.8.4-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 766b8d3d3a66d470059c339fae322f19b6622e2fea6c6ead1be75615bb92dcb5
MD5 5e2fa0dda7fab7ef97f66674ca219b62
BLAKE2b-256 2ac1db596aeb8a93b3e5dbea65dca4319ce0aa74353177f0422fe8faa435f87b

See more details on using hashes here.

File details

Details for the file cel_bind-0.8.4-cp310-cp310-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for cel_bind-0.8.4-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 8162c9d426e817b0e013db77bf445ec7792f1adce003db9362c9dc7c0f7ef3a8
MD5 794781c13ee8d3d7534c36e11dc269ce
BLAKE2b-256 eff75f78db412cf15ae34abfa83e5e28889e0691cdfae432964e7697c6a61666

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page