Assign Rule Checking
Rule checking can be done by adding rules_all and/or rules_any to assign() method.
Rule checking ensures a value of **kwargs values matches all rules before assign to
current instance of class.
All rules
In the following example attribute speed can be a positive float or int zero.
All other values will result in an error. Arg rules_all of assign()
method validates as True only if all IRule match.
Trying to assign any other type or value results in an error.
Custom Rule for a maximum int value of 100
import kwhelp.rules as rules
class RuleIntMax100(rules.IRule):
def validate(self) -> bool:
if self.field_value > 100:
if self.raise_errors:
raise ValueError(
f"Arg error: '{self.key}' must be equal or less than 100")
return False
return True
from kwhelp import rules, KwargsHelper
class MyClass:
def __init__(self, **kwargs):
kw = KwargsHelper(originator=self, obj_kwargs={**kwargs}, field_prefix="")
kw.assign(key="speed", rules_all=[
rules.RuleIntPositive,
RuleIntMax100
])
Assign int.
>>> myclass = MyClass(speed = 12)
>>> print(myclass.speed)
12
Assign int greater then 100.
>>> myclass = MyClass(speed = 126)
kwhelp.exceptions.RuleError: RuleError: Argument: 'speed' failed validation.
Rule 'RuleIntMax100' Failed validation.
Expected all of the following rules to match: RuleIntPositive, RuleIntMax100.
Inner Error Message: ValueError: Arg error: 'speed' must be equal or less than 100
Assign int negative.
>>> myclass = MyClass(speed = -3)
kwhelp.exceptions.RuleError: RuleError: Argument: 'speed' failed validation.
Rule 'RuleIntPositive' Failed validation.
Expected all of the following rules to match: RuleIntPositive, RuleIntMax100.
Inner Error Message: ValueError: Arg error: 'speed' must be a positive int value
Assign float.
>>> myclass = MyClass(speed = 22.4)
kwhelp.exceptions.RuleError: RuleError: Argument: 'speed' failed validation.
Rule 'RuleIntPositive' Failed validation.
Expected all of the following rules to match: RuleIntPositive, RuleIntMax100.
Inner Error Message: TypeError: Argument Error: 'speed' is expecting type of 'int'. Got type of 'float'
Any rules
In the following example attribute speed can be a positive float or int zero.
All other values will result in an error. Arg rules_all of assign()
method validates as True only if all IRule match.
Trying to assign any other type or value results in an error.
from kwhelp import rules, KwargsHelper
class MyClass:
def __init__(self, **kwargs):
kw = KwargsHelper(originator=self, obj_kwargs={**kwargs}, field_prefix="")
kw.assign(key="speed", rules_any=[
rules.RuleFloatPositive,
rules.RuleIntZero
])
Assign int.
>>> myclass = MyClass(speed = 123.55)
>>> print(myclass.speed)
123.55
Assign int zero.
>>> myclass = MyClass(speed = 0)
>>> print(myclass.speed)
0
Assign float zero.
>>> myclass = MyClass(speed = 0.0)
>>> print(myclass.speed)
0.0
Assign int negative.
>>> myclass = MyClass(speed = -123)
kwhelp.exceptions.RuleError: RuleError: Argument: 'speed' failed validation.
Rule 'RuleFloatPositive' Failed validation.
Expected at least one of the following rules to match: RuleFloatPositive, RuleIntZero.
Inner Error Message: TypeError: Argument Error: 'speed' is expecting type of 'float'. Got type of 'int'
Assign str.
>>> myclass = MyClass(speed="a")
kwhelp.exceptions.RuleError: RuleError: Argument: 'speed' failed validation.
Rule 'RuleFloatPositive' Failed validation.
Expected at least one of the following rules to match: RuleFloatPositive, RuleIntZero.
Inner Error Message: TypeError: Argument Error: 'speed' is expecting type of 'float'. Got type of 'str'