cardinal_pythonlib.nhs


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Support functions regarding NHS numbers, etc.

cardinal_pythonlib.nhs.generate_nhs_number_from_first_9_digits(first9digits: str) int | None[source]

Returns a valid NHS number, as an int, given the first 9 digits. The particular purpose is to make NHS numbers that look fake (rather than truly random NHS numbers which might accidentally be real).

For example:

123456789_ : no; checksum 10
987654321_ : yes, valid if completed to 9876543210
999999999_ : yes, valid if completed to 9999999999

But see also generate_random_nhs_number() with its option to use the official NHS test range.

cardinal_pythonlib.nhs.generate_random_nhs_number(official_test_range: bool = True) int[source]

Returns a random valid NHS number, as an int.

Parameters:

official_test_range – Make it start with “999”, the official NHS test range. See https://digital.nhs.uk/services/e-referral-service/document-library/synthetic-data-in-live-environments, saved at https://web.archive.org/web/20210116183039/https://digital.nhs.uk/services/e-referral-service/document-library/synthetic-data-in-live-environments.

cardinal_pythonlib.nhs.is_test_nhs_number(n: int) bool[source]

Is this number both valid and in the official test range (starting 999)? See reference above.

cardinal_pythonlib.nhs.is_valid_nhs_number(n: int) bool[source]

Validates an integer as an NHS number.

Parameters:

n – NHS number

Returns:

valid?

Checksum details are at https://web.archive.org/web/20180311083424/https://www.datadictionary.nhs.uk/version2/data_dictionary/data_field_notes/n/nhs_number_de.asp; https://web.archive.org/web/20220503215904/https://www.datadictionary.nhs.uk/attributes/nhs_number.html

cardinal_pythonlib.nhs.nhs_check_digit(ninedigits: str | List[int | str]) int[source]

Calculates an NHS number check digit.

Parameters:

ninedigits – string or list

Returns:

check digit

Method:

  1. Multiply each of the first nine digits by the corresponding digit weighting (see NHS_DIGIT_WEIGHTINGS).

  2. Sum the results.

  3. Take remainder after division by 11.

  4. Subtract the remainder from 11

  5. If this is 11, use 0 instead If it’s 10, the number is invalid If it doesn’t match the actual check digit, the number is invalid

This function will return 10 for some values, but that is invalid; it’s up to the caller to check.

cardinal_pythonlib.nhs.nhs_number_from_text_or_none(s: str) int | None[source]

Returns a validated NHS number (as an integer) from a string, or None if it is not valid.

It’s a 10-digit number, so note that database 32-bit INT values are insufficient; use BIGINT. Python will handle large integers happily.

NHS number rules: https://www.datadictionary.nhs.uk/version2/data_dictionary/data_field_notes/n/nhs_number_de.asp?shownav=0

cardinal_pythonlib.nhs.test_nhs_rng(n: int = 100) None[source]

Tests the NHS random number generator.