diff --git a/stdnum/pl/nip.py b/stdnum/pl/nip.py index bfc7506e..537356d8 100644 --- a/stdnum/pl/nip.py +++ b/stdnum/pl/nip.py @@ -45,10 +45,15 @@ def compact(number: str) -> str: return number +_weights = (6, 5, 7, 2, 3, 4, 5, 6, 7, -1) + + def checksum(number: str) -> int: """Calculate the checksum.""" - weights = (6, 5, 7, 2, 3, 4, 5, 6, 7, -1) - return sum(w * int(n) for w, n in zip(weights, number)) % 11 + total = 0 + for w, n in zip(_weights, number): + total += w * (ord(n) - 48) + return total % 11 def validate(number: str) -> str: diff --git a/stdnum/pl/pesel.py b/stdnum/pl/pesel.py index 38ff544e..e7d1fb33 100644 --- a/stdnum/pl/pesel.py +++ b/stdnum/pl/pesel.py @@ -88,12 +88,16 @@ def get_gender(number: str) -> str: return 'M' +_weights = (1, 3, 7, 9, 1, 3, 7, 9, 1, 3) + + def calc_check_digit(number: str) -> str: """Calculate the check digit for organisations. The number passed should not have the check digit included.""" - weights = (1, 3, 7, 9, 1, 3, 7, 9, 1, 3) - check = sum(w * int(n) for w, n in zip(weights, number)) - return str((10 - check) % 10) + total = 0 + for w, n in zip(_weights, number): + total += w * (ord(n) - 48) + return str((10 - total) % 10) def validate(number: str) -> str: