Original code copyright (C) 2009-2022 Rudolf Cardinal (email@example.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.
generate_nhs_number_from_first_9_digits(first9digits: str) → Optional[int]¶
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).
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.
generate_random_nhs_number(official_test_range: bool = True) → int¶
Returns a random valid NHS number, as an
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.
is_test_nhs_number(n: int) → bool¶
Is this number both valid and in the official test range (starting 999)? See reference above.
is_valid_nhs_number(n: int) → bool¶
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
nhs_check_digit(ninedigits: Union[str, List[Union[str, int]]]) → int¶
Calculates an NHS number check digit.
Parameters: ninedigits – string or list Returns: check digit
- Multiply each of the first nine digits by the corresponding
digit weighting (see
- Sum the results.
- Take remainder after division by 11.
- Subtract the remainder from 11
- 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.
- Multiply each of the first nine digits by the corresponding digit weighting (see
nhs_number_from_text_or_none(s: str) → Optional[int]¶
Returns a validated NHS number (as an integer) from a string, or
Noneif 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.
test_nhs_rng(n: int = 100) → None¶
Tests the NHS random number generator.