Decorators
Decorators can be used to validate functions.
Decorator Index
- AcceptedTypes Usage
- ArgsLen Usage
- ArgsMinMax Usage
- DefaultArgs Usage
- RequireArgs Usage
- ReturnRuleAll usage
- ReturnRuleAny Usage
- ReturnType Usage
- RuleCheckAll Usage
- RuleCheckAllKw Usage
- RuleCheckAny Usage
- RuleCheckAnyKw Usage
- SubClass Usage
- SubClasskKw Usage
- TypeCheck Usage
- TypeCheckKw Usage
- Callcounter Usage
- Calltracker Usage
- Singleton Usage
Example
The following example ensures all function args are a positive int
or a positive float
.
from kwhelp.decorator import RuleCheckAny
import kwhelp.rules as rules
@RuleCheckAny(rules.RuleIntPositive, rules.RuleFloatPositive)
def speed_msg(speed, limit, **kwargs) -> str:
if limit > speed:
msg = f"Current speed is '{speed}'. You may go faster as the limit is '{limit}'."
elif speed == limit:
msg = f"Current speed is '{speed}'. You are at the limit."
else:
msg = f"Please slow down limit is '{limit}' and you are currenlty going '{speed}'."
if 'hours' in kwargs:
msg = msg + f" Current driving hours is '{kwargs['hours']}'"
return msg