stdnum.luhn¶
The Luhn and Luhn mod N algorithms.
The Luhn algorithm is used to detect most accidental errors in various identification numbers.
>>> validate('7894')
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> checksum('7894')
6
>>> calc_check_digit('7894')
'9'
>>> validate('78949')
'78949'
An alternative alphabet can be provided to use the Luhn mod N algorithm. The default alphabet is ‘0123456789’.
>>> validate('1234', alphabet='0123456789abcdef')
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> checksum('1234', alphabet='0123456789abcdef')
14
- stdnum.luhn.calc_check_digit(number, alphabet='0123456789')¶
Calculate the extra digit that should be appended to the number to make it a valid number.
- stdnum.luhn.checksum(number, alphabet='0123456789')¶
Calculate the Luhn checksum over the provided number. The checksum is returned as an int. Valid numbers should have a checksum of 0.
- stdnum.luhn.is_valid(number, alphabet='0123456789')¶
Check if the number passes the Luhn checksum.
- stdnum.luhn.validate(number, alphabet='0123456789')¶
Check if the number provided passes the Luhn checksum.