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.2-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.2-cp314-cp314-macosx_14_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.14macOS 14.0+ ARM64

cel_bind-0.8.2-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.2-cp313-cp313-macosx_14_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

cel_bind-0.8.2-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.2-cp312-cp312-macosx_14_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

cel_bind-0.8.2-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.2-cp311-cp311-macosx_14_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

cel_bind-0.8.2-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.2-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.2-cp314-cp314-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for cel_bind-0.8.2-cp314-cp314-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 2d180a8c4df498c8878279a88fa1657968c55d9e125edf5b793283f3528af196
MD5 7dc2575b7cb5ba642dfedf5aee44f471
BLAKE2b-256 d806dcbb653e60018010145227f2dd029c67981c5faf46ca236654389f172727

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cel_bind-0.8.2-cp314-cp314-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 e1572bc5a9dd063eb06df73fc3875b5ec1a227cda79c712ca24005650c7ee33d
MD5 f803b632f0528de97a74ee9dee4aab75
BLAKE2b-256 d5133452df3cc02d6178722bd87633a9de84cc355f0d422dfb931ec97fe4ab00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cel_bind-0.8.2-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 00d941b2f3d88cd56e725081d12d954920330bb2785d4e0812b4b1906ed90406
MD5 808142250369b411275eb14acbd4083f
BLAKE2b-256 2ab54877846d6aca921764cc456c7b3ae9ee6d4d4afada2868902dc619bdd7f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cel_bind-0.8.2-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 d3def6a5c843b488082d060e450025c644182c1d1a3ed3f5b17f1492e6af086a
MD5 34f1ce443a0d1b66c6f287783e24d5f5
BLAKE2b-256 8bc75f2307683bcb6573e5200a2afeedcf6e5b73d51e790fd58f7d6bdb6bb06d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cel_bind-0.8.2-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 d67035c8a9691db10a1f0b7f6c391d246f3ec07fbf3adb5e6bc0e6ad51383c62
MD5 b6ab0717d3fd5d2327d874a03a288698
BLAKE2b-256 cc05922c256be8ec0d5adde9e79968df1821dbc5f5a22d4a7b85cb4d67048756

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cel_bind-0.8.2-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 5604d7e5e012d171adf936dd4c49399bf855c2f58563abed84b86c96e174ff6b
MD5 b3a063f2e0d895d6889877e01f881132
BLAKE2b-256 62798f496ba38d6118dac56d3de3bdff7af6960f5352c0229ee26ffb54a41f04

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cel_bind-0.8.2-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 75991231d7c45a2225eefd105f70b303d567010faf027bb1993bac42d7db393c
MD5 26c006f84f52154e143c1f468a7da1db
BLAKE2b-256 da8f3a355fe7e47d4c781a81a97d0fd046546b1934f94df6c4e8e98b39af335e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cel_bind-0.8.2-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 09b6870d5992a279486bd7779a03b0a2b883cbbdcff73c40f90b62a957f0638a
MD5 c851c49d401ffc593bb04278c9b194cd
BLAKE2b-256 9aa3b7b9e7a6cd0055d8e89f6b9b2e6c8fbb79fcb959df9636f1636d38eab268

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cel_bind-0.8.2-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 71709937328ae8a546f625b8e53ce30e4f13d7e9d5d5db83219c4373d87c8e88
MD5 e75284d3d6bef2254a20551d691d5d05
BLAKE2b-256 97162fbf06cc4374b53a962a054a997eef43895dcaa402d7af0d6c5af6d47277

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cel_bind-0.8.2-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 d9c50c4e3857f64bc71668fef833149e1fd26796ecd669c9ba231b6d5f41c447
MD5 619e92285ed826b3e4d0429664bc89a7
BLAKE2b-256 1950e801e137d7caa011d41d8666b8abc14c695cbe2c6556f1921d03779f5a0d

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