@@ -0,0 +1,2883 @@
+# nhs.py - functions for handling United Kingdom NHS numbers%0A#%0A# Copyright (C) 2012-2015 Arthur de Jong%0A#%0A# This library is free software; you can redistribute it and/or%0A# modify it under the terms of the GNU Lesser General Public%0A# License as published by the Free Software Foundation; either%0A# version 2.1 of the License, or (at your option) any later version.%0A#%0A# This library is distributed in the hope that it will be useful,%0A# but WITHOUT ANY WARRANTY; without even the implied warranty of%0A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU%0A# Lesser General Public License for more details.%0A#%0A# You should have received a copy of the GNU Lesser General Public%0A# License along with this library; if not, write to the Free Software%0A# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA%0A# 02110-1301 USA%0A%0A%22%22%22NHS (United Kingdom National Health Service patient identifier).%0A%0AThe NHS number is used by the NHS (National Health Service) and its partners%0Ato uniquely identify patients. The number is used in England, Wales and the%0AIsle of Man. The number is assigned at birth and consists of 10 digits where%0Athe final digit is a check digit.%0A%0AMore information:%0A%0A* https://en.wikipedia.org/wiki/NHS_number%0A* http://www.nhs.uk/NHSEngland/thenhs/records/nhs-number/%0A* http://systems.hscic.gov.uk/nhsnumber%0A* http://www.datadictionary.nhs.uk/data_dictionary/attributes/n/nhs/nhs_number_de.asp%0A%0A%3E%3E%3E validate('943-476-5870')%0A'9434765870'%0A%3E%3E%3E validate('9434765871')  # invalid check digit%0ATraceback (most recent call last):%0A    ...%0AInvalidChecksum: ...%0A%3E%3E%3E format('9434765870')%0A'943 476 5870'%0A%22%22%22%0A%0Afrom stdnum.util import clean%0Afrom stdnum.exceptions import *%0A%0A%0Adef compact(number):%0A    %22%22%22Convert the number to the minimal representation. This strips the%0A    number of any valid separators and removes surrounding whitespace.%22%22%22%0A    return clean(number, ' -').strip()%0A%0A%0Adef checksum(number):%0A    %22%22%22Calculate the checksum. The checksum is only used for the 9 digits%0A    of the number and the result can either be 0 or 42.%22%22%22%0A    return sum(i * int(n) for i, n in enumerate(reversed(number), 1)) %25 11%0A%0A%0Adef validate(number):%0A    %22%22%22Checks to see if the number provided is valid. This checks the length%0A    and check digit.%22%22%22%0A    number = compact(number)%0A    if not number.isdigit():%0A        raise InvalidFormat()%0A    if len(number) != 10:%0A        raise InvalidLength()%0A    if checksum(number) != 0:%0A        raise InvalidChecksum()%0A    return number%0A%0A%0Adef is_valid(number):%0A    %22%22%22Checks to see if the number provided is valid. This checks the length%0A    and check digit.%22%22%22%0A    try:%0A        return bool(validate(number))%0A    except ValidationError:%0A        return False%0A%0A%0Adef format(number, separator=' '):%0A    %22%22%22Reformat the passed number to the standard format.%22%22%22%0A    number = compact(number)%0A    return separator.join((number%5B0:3%5D, number%5B3:6%5D, number%5B6:%5D))%0A

Add United Kingdom NHS number