Decorators
Decorators can be used to validate functions.
See also
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