Skip to main content

A compact predicate DSL for matching criteria against any object

Project description

koalify

A compact predicate DSL for matching criteria against any Python object. Zero runtime dependencies.

Installation

pip install koalify

Quick Start

from koalify import F, all_of, any_of

# Build rules with Python operators
is_eligible = (
    (F.status == "active")
    & (F.age >= 18)
    & (F.role.in_({"admin", "moderator", "editor"}))
    & F.score.between(50, 100)
)

# Evaluate against any object with attributes
is_eligible(user)  # True / False

# Nested fields
lives_in_london = F.address.city == "London"

# Compose with OR / NOT
can_access = is_eligible | (lives_in_london & ~(F.status == "banned"))

# Dynamic composition from a list
conditions = [F.status == "active", F.age >= 18]
rule = all_of(*conditions)

Examples

Dataclasses

from dataclasses import dataclass
from koalify import F, all_of

@dataclass
class Order:
    product: str
    quantity: int
    price: float
    fulfilled: bool

needs_review = (F.quantity > 100) & (F.price >= 500) & (F.fulfilled == False)

order = Order(product="Widget", quantity=200, price=750.0, fulfilled=False)
needs_review(order)  # True

Pydantic

from pydantic import BaseModel
from koalify import F, any_of

class Address(BaseModel):
    city: str
    country: str

class Customer(BaseModel):
    name: str
    tier: str
    address: Address

is_priority = (F.tier.in_({"gold", "platinum"})) | (F.address.country == "US")

customer = Customer(name="Alice", tier="gold", address=Address(city="London", country="UK"))
is_priority(customer)  # True

Dynamic rule composition

from koalify import F, all_of

def build_filter(min_age: int | None = None, status: str | None = None, roles: set[str] | None = None):
    criteria = []
    if min_age is not None:
        criteria.append(F.age >= min_age)
    if status is not None:
        criteria.append(F.status == status)
    if roles is not None:
        criteria.append(F.role.in_(roles))
    return all_of(*criteria) if criteria else lambda _: True

user_filter = build_filter(min_age=18, roles={"admin", "editor"})

API

Symbol Description
F.field Reference a field (supports nesting: F.a.b.c)
== != > >= < <= Comparison operators on FieldRef
.in_(values) Set membership
.between(lo, hi) Inclusive range check
& AND (flattens nested ANDs)
| OR (flattens nested ORs)
~ NOT
all_of(*criteria) AND from a list
any_of(*criteria) OR from a list

How It Works

F.field_name returns a FieldRef. Comparison operators on FieldRef produce Criterion objects. Criteria compose with &, |, and ~. Calling a criterion resolves field values via getattr — works with dataclasses, Pydantic models, namedtuples, or any object with attributes.

License

MIT

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

koalify-0.2.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

koalify-0.2.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file koalify-0.2.2.tar.gz.

File metadata

  • Download URL: koalify-0.2.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.12.13 Linux/6.14.0-1017-azure

File hashes

Hashes for koalify-0.2.2.tar.gz
Algorithm Hash digest
SHA256 61fc2439d54d9bee0e7dcc2a95c1122ced1ab39bcb514a36e953b8ea4ab87f18
MD5 476aca51ce494c1f10e8a3524570f1b4
BLAKE2b-256 250e42a3cc624e72f2fe70a1d8e971c43789367075913bf4c3faeab675df0a84

See more details on using hashes here.

File details

Details for the file koalify-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: koalify-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.12.13 Linux/6.14.0-1017-azure

File hashes

Hashes for koalify-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fe936b2c54000a0fd1a717b0bb735e3c09b10678511a55e996a792bda9730b2d
MD5 69af5681a82eeb3a2079144caac84695
BLAKE2b-256 4a29cc71e2c2955efd6d6c8b3ffe1bcfe92419ea4e8b3352be847094b6a2c7da

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