I've not had much exposure with good tests. So this focuses on the first codeblock.
- I think
*_TABLEisn't that useful. InsteadPERMUATTIONSPERMUTATIONSandINVERSElooks nicer to me. - Given that
calculateandvalidateare almost duplicate functions you should probably define a private helper to handle the common code.
class VarhoeffVerhoeff:
...
@classmethod
def _find_check_digit(cls, digits):
check_digit = 0
for i, digit in digits:
col_idx = cls.PERMUTATIONS[i % 8][int(digit)]
check_digit = cls.MULTIPLICATIONS[check_digit][col_idx]
return check_digit
@classmethod
def calculate(cls, input_: str) -> str:
"""Calculate the check digit using Verhoeff's algorithm"""
check_digit = cls._find_check_digit(enumerate(reversed(input_), 1))
return str(cls.INVERSES[check_digit])
@classmethod
def validate(cls, input_: str) -> bool:
"""Validate the check digit using Verhoeff's algorithm"""
check_digit = cls._find_check_digit(enumerate(reversed(input_)))
return cls.INVERSES[check_digit] == 0