anonymize_6.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. def get_pseudonym(nhs_number):
  2. global GENERATE_NEW
  3. pseudo = LOOKUP.get(nhs_number)
  4. if pseudo is not None:
  5. return pseudo
  6. if GENERATE_NEW is not True:
  7. print("I have encountered a new NHS number (%d) with no pseudonym.\n"
  8. "Should I generate new ones for any new NHS numbers I find "
  9. "from now on?" % nhs_number)
  10. response = raw_input("type y or n:")
  11. if response == 'y':
  12. GENERATE_NEW = True
  13. else:
  14. print("In that case, I will exit now.")
  15. exit()
  16. while True:
  17. digits = []
  18. s = ''
  19. tot = 0
  20. for i in range(9):
  21. if i == 0:
  22. digit = random.randint(1, 9)
  23. else:
  24. digit = random.randint(0, 9)
  25. digits.append(digit)
  26. s += str(digit)
  27. tot += digit * (10 - i) # (10 - i) is the weighting factor
  28. checksum = 11 - (tot % 11)
  29. if checksum == 11:
  30. checksum = 0
  31. if checksum != 10: # 10 is an invalid nhs number
  32. s += str(checksum)
  33. pseudo = int(s)
  34. LOOKUP[nhs_number] = pseudo
  35. return pseudo