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
Item access (lists, dicts)
from dataclasses import dataclass
from koalify import F
@dataclass
class Event:
name: str
tags: list[str]
metadata: dict[str, str]
event = Event(name="deploy", tags=["prod", "urgent"], metadata={"region": "eu-west-1"})
# List indexing
(F.tags[0] == "prod")(event) # True
# Dict key access
(F.metadata["region"] == "eu-west-1")(event) # True
# Mix with attribute access and composition
is_urgent_prod = (F.tags[0] == "prod") & (F.tags[1] == "urgent")
is_urgent_prod(event) # 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 and indexing: F.a[0], F.a["k"]) |
== != > >= < <= |
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_namereturns aFieldRef— a lightweight path descriptor- Comparison operators (
==,>,.in_(), etc.) produceCriterionobjects - Criteria compose with
&,|, and~(flattening nested groups automatically) - Calling a criterion resolves field values at runtime via
getattrand[]
Works with dataclasses, Pydantic models, namedtuples, or any object with attributes.
Item access (F.tags[0], F.data["key"]) delegates to the resolved value's __getitem__, so standard IndexError / KeyError exceptions propagate naturally.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file koalify-1.1.0.tar.gz.
File metadata
- Download URL: koalify-1.1.0.tar.gz
- Upload date:
- Size: 4.7 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b690abf36aa6dbd4836ce92c291d8f2edf46d0cf96a8c42261270fbba51224d3
|
|
| MD5 |
f485a4d8c5bc5141a9b39ce987c85efd
|
|
| BLAKE2b-256 |
0ca2a1992d65f17c85a44ff6d02eb2c19a9ce44e8637c9585df0f2cb46f93434
|
File details
Details for the file koalify-1.1.0-py3-none-any.whl.
File metadata
- Download URL: koalify-1.1.0-py3-none-any.whl
- Upload date:
- Size: 6.2 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e9bb642035a9d15195edbfb5d852439061a8607ed7b309c642d75cd904ab21e
|
|
| MD5 |
44d758a7acd34ec8207d514379cefd60
|
|
| BLAKE2b-256 |
80e8dbcf28dce0649f0958eb74235f735ef6a442fb7d5fdeb8830babac38bb00
|