Skip to content

Instantly share code, notes, and snippets.

@wolfospealain
Last active October 20, 2021 09:59
Show Gist options
  • Save wolfospealain/71cbad22b4de37879a327df19f85c183 to your computer and use it in GitHub Desktop.
Save wolfospealain/71cbad22b4de37879a327df19f85c183 to your computer and use it in GitHub Desktop.
Validate PPSN (Ireland)
import logging
logging.basicConfig(level=logging.INFO)
def calculate_check(text):
total = 0
for i in range(0,7):
total += int(text[i]) * (8-i)
if len(text) == 9:
total += ("ABCDEFGHIJKLMNOPQRSTU".find(text[8])+1)*9
remainder = total % 23
character = "ABCDEFGHIJKLMNOPQRSTUV"[remainder-1]
logging.info(character)
return character
def valid_ppsn(text):
text = text.upper()
# test for string length 8 or 9
if len(text) != 8 and len(text) != 9:
logging.info("Fails length test.")
return False
# test characters 1 to 7 are digits
if not text[:7].isdigit():
logging.info("Fails the digit test.")
return False
# test character 8 is a letter in A to V
if text[7] not in "ABCDEFGHIJKLMNOPQRSTUV":
logging.info("Fails the first letter test.")
return False
# test character 9 (if exists) is in W, H, A
if len(text) == 9:
if text[8] not in "AHW":
logging.info("Fails the second letter test.")
return False
# test checksum
if text[7] != calculate_check(text):
logging.info("Fails checksum.")
return False
logging.info("Passes all tests.")
return True
@GaddMaster
Copy link

Validate PPS number format, not validate PPS. Its misleading I feel

@wolfospealain
Copy link
Author

Validate PPS number format, not validate PPS. Its misleading I feel

Validation is the process of checking something against specifications. In this case the specifications of PPSN format. What is a usecase for validate PPSN (which is a code format) that isn't validating the number format?

@DestyNova
Copy link

"Validate PPS number" means "check that this is a valid PPS number", not "check that this PPS number has been assigned". I think most people would expect the former, not the latter, so I don't think this is misleading. Same thing applies for IBAN number checkers etc -- they don't connect to every bank worldwide to check if the number is associated with an actual bank account (consider the privacy implications of that).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment