Kompoz¶
Composable Predicate & Transform Combinators for Python
Kompoz lets you build complex validation rules and data pipelines using intuitive Python operators. Instead of nested if/else statements, write declarative, composable logic:
from dataclasses import dataclass
from kompoz import rule, rule_args
@dataclass
class User:
name: str
is_admin: bool = False
is_active: bool = True
is_banned: bool = False
account_age_days: int = 0
credit_score: int = 500
@rule
def is_admin(user):
return user.is_admin
@rule
def is_active(user):
return user.is_active
@rule_args
def account_older_than(user, days):
return user.account_age_days > days
# Combine with operators - reads like English!
can_access = is_admin | (is_active & account_older_than(30))
# Use it
ok, _ = can_access.run(user)
Features¶
- Operator Overloading --- Use
&(and),|(or),~(not),>>(then) for intuitive composition - Conditional Branching ---
if_then_else()and ternary?:for explicit control flow - Decorator Syntax --- Clean
@ruleand@rule_argsdecorators - Parameterized Rules ---
account_older_than(30)creates reusable predicates - Validation with Errors ---
@vrule/@async_vruledecorators collect all error messages - Expression DSL --- Human-readable rule expressions with AND/OR/NOT/IF/THEN/ELSE
- Async Support --- Full async/await support with tracing, validation, and parallel execution
- Caching ---
@cached_ruleanduse_cache()to memoize expensive predicates - Time-Based Rules ---
during_hours(),on_weekdays(),after_date(), and more - Error Tracking --- Transforms track exceptions via
last_errorattribute - Retry with Observability --- Built-in retry logic with hooks for monitoring
- Zero Dependencies --- Core library has no external dependencies
Quick Install¶
Next Steps¶
- Installation -- Python version requirements and extras
- Quick Start -- Define, compose, and run rules in 5 minutes
- API Reference -- Full reference of all public symbols