Decorators¶
@rule¶
Create a simple predicate from a function that takes one argument and returns bool.
Returns: Predicate[T]
@rule_args¶
Create a parameterized predicate factory. Call the result with arguments to get a Predicate[T].
Returns: PredicateFactory[T]
@rule_args
def credit_above(user: User, threshold: int) -> bool:
return user.credit_score > threshold
check = credit_above(700) # Predicate[User]
@pipe¶
Create a simple transform from a function that takes one argument and returns the transformed value.
Returns: Transform[T]
@pipe_args¶
Create a parameterized transform factory.
Returns: TransformFactory[T]
@vrule¶
Create a validating rule with an error message. The error string can reference ctx for template formatting, or be a callable (ctx) -> str.
Returns: ValidatingPredicate[T]
@vrule(error="User {ctx.name} must be an admin")
def is_admin(user):
return user.is_admin
@vrule(error=lambda u: f"{u.name} is banned!")
def not_banned(user):
return not user.is_banned
@vrule_args¶
Create a parameterized validating rule. Extra arguments are available in the error template.
Returns: Factory producing ValidatingPredicate[T]
@vrule_args(error="Account must be older than {days} days")
def account_older_than(user, days):
return user.account_age_days > days
@async_rule¶
Create an async predicate.
Returns: AsyncPredicate[T]
@async_rule_args¶
Create a parameterized async predicate factory.
Returns: AsyncPredicateFactory[T]
@async_pipe¶
Create an async transform.
Returns: AsyncTransform[T]
@async_pipe_args¶
Create a parameterized async transform factory.
Returns: AsyncTransformFactory[T]
@async_vrule¶
Create an async validating rule with error message.
Returns: AsyncValidatingPredicate[T]
@async_vrule_args¶
Create a parameterized async validating rule.
Returns: Factory producing AsyncValidatingPredicate[T]
@cached_rule¶
@cached_rule
def predicate_name(ctx: T) -> bool: ...
@cached_rule(key=lambda ctx: ctx.id)
def predicate_name(ctx: T) -> bool: ...
Create a rule with result caching. Results are cached within a use_cache() scope.
Optionally provide a key function to control the cache key.
Returns: CachedPredicate[T]
@async_cached_rule¶
@async_cached_rule
async def predicate_name(ctx: T) -> bool: ...
@async_cached_rule(key=lambda ctx: ctx.id)
async def predicate_name(ctx: T) -> bool: ...
Create an async rule with result caching.
Returns: AsyncCachedPredicate[T]