Skip to content

Expressions

Therismos provides a comprehensive expression system for modeling filters and conditions as object structures using an Abstract Syntax Tree (AST) approach.

Quick Start

from therismos import F, optimize

# Define fields
age = F("age", int)
name = F("name")
status = F("status")

# Build expressions using natural Python syntax
expr = (age > 18) & (name == "Alice") | (status == "admin")

# Optimize the expression
optimized, records = optimize(expr)

# Detect contradictions
contradiction = (age < 30) & (age > 40)
result, _ = optimize(contradiction)
# result is FALSE

# Aggregate OR equality chains
multi_status = (status == "active") | (status == "pending") | (status == "completed")
result, _ = optimize(multi_status)
# result is: status IN ("active", "pending", "completed")

Expression Types

Atomic Expressions

  • Comparisons: ==, !=, <, <=, >, >=
  • Range: field.between(lower, upper) — half-open range lower <= field < upper
  • Regex matching: field.matches(pattern, flags=None)
  • Membership: field.is_in(*values) or field.is_one_of(iterable)
  • Null checking: field.is_null(), field.is_not_null()
  • Constants: TRUE, FALSE

Compound Expressions

  • AND: expr1 & expr2 or AllExpr(expr1, expr2, ...)
  • OR: expr1 | expr2 or AnyExpr(expr1, expr2, ...)
  • NOT: ~expr or NotExpr(expr)

Fields

Fields are created with F(name) or F(name, type_):

from therismos import F

age = F("age", int)         # typed field — values auto-cast to int
name = F("name")            # untyped field
score = F("score", float)

# Nested field references
profile_age = F("user.profile.age")

See Type Casting for details on automatic casting.