Skip to content

Commit 8044806

Browse files
Andyg79mepr1
andauthored
Feature/bcss 20437 fit kit gen util markdown and docstring (#54)
<!-- markdownlint-disable-next-line first-line-heading --> ## Description <!-- Describe your changes in detail. --> Added docstrings to fit_kit_generation.py and created FitKitGeneration.md ## Context <!-- Why is this change required? What problem does it solve? --> These changes make the code more accessible and readable for anyone using these utils. ## Type of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply. --> - [x] Refactoring (non-breaking change) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would change existing functionality) - [ ] Bug fix (non-breaking change which fixes an issue) ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I am familiar with the [contributing guidelines](https://github.com/nhs-england-tools/playwright-python-blueprint/blob/main/CONTRIBUTING.md) - [x] I have followed the code style of the project - [ ] I have added tests to cover my changes (where appropriate) - [x] I have updated the documentation accordingly - [ ] This PR is a result of pair or mob programming --- ## Sensitive Information Declaration To ensure the utmost confidentiality and protect your and others privacy, we kindly ask you to NOT including [PII (Personal Identifiable Information) / PID (Personal Identifiable Data)](https://digital.nhs.uk/data-and-information/keeping-data-safe-and-benefitting-the-public) or any other sensitive data in this PR (Pull Request) and the codebase changes. We will remove any PR that do contain any sensitive information. We really appreciate your cooperation in this matter. - [x] I confirm that neither PII/PID nor sensitive data are included in this PR and the codebase changes. --------- Co-authored-by: mepr1 <[email protected]>
1 parent f05e5a4 commit 8044806

File tree

2 files changed

+115
-12
lines changed

2 files changed

+115
-12
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Utility Guide: Fit Kit Generation
2+
3+
The Fit Kit Generation Utility provides methods to generate and manage FIT test kits for testing purposes.
4+
5+
## Table of Contents
6+
7+
- [Utility Guide: Fit Kit Generation](#utility-guide-fit-kit-generation)
8+
- [Table of Contents](#table-of-contents)
9+
- [Using the Fit Kit Generation Utility](#using-the-fit-kit-generation-utility)
10+
- [Required Arguments](#required-arguments)
11+
- [Fit Kit Generation Specific Functions](#fit-kit-generation-specific-functions)
12+
- [Example Usage](#example-usage)
13+
14+
## Using the Fit Kit Generation Utility
15+
16+
To use the Fit Kit Generation Utility, import the `fit_kit_generation` module, from the `utils` directory, into your test file and call its methods from within your tests, as required. To generate the fit kit IDs, the only method required is `create_fit_id_df`. This will return a `dataframe` with all the transformations already taking place.
17+
18+
## Required Arguments
19+
20+
The methods in this utility require specific arguments. Below is a summary of the required arguments for key methods:
21+
22+
- `generate_kit`: Requires `batch_id` (int) and `kit_type` (str).
23+
24+
## Fit Kit Generation Specific Functions
25+
26+
The Fit Kit Generation Utility includes methods for generating, validating, and managing FIT test kits. These methods are designed to streamline the process of creating test kits for various scenarios. Below are some key functions:
27+
28+
1. **`create_fit_id_df(tk_type_id: int, hub_id: int, no_of_kits: int) -> DataFrame`**
29+
Creates a DataFrame containing FIT kit IDs based on the provided parameters.
30+
- **Arguments**:
31+
- `tk_type_id` (int): The type ID of the test kit.
32+
- `hub_id` (int): The hub ID associated with the kits.
33+
- `no_of_kits` (int): The number of kits to retrieve.
34+
- **Returns**: A pandas DataFrame with the FIT kit IDs.
35+
36+
2. **`calculate_check_digit(kit_id: str) -> str`**
37+
Calculates and appends a check digit to the given kit ID.
38+
- **Arguments**:
39+
- `kit_id` (str): The kit ID to process.
40+
- **Returns**: The kit ID with the appended check digit.
41+
42+
3. **`convert_kit_id_to_fit_device_id(kit_id: str) -> str`**
43+
Converts a kit ID into a FIT Device ID.
44+
- **Arguments**:
45+
- `kit_id` (str): The kit ID to convert.
46+
- **Returns**: The corresponding FIT Device ID.
47+
48+
## Example Usage
49+
50+
```python
51+
from utils.fit_kit_generation import create_fit_id_df, calculate_check_digit, convert_kit_id_to_fit_device_id
52+
53+
def example_usage() -> None:
54+
# Example inputs
55+
tk_type_id = 1
56+
hub_id = 101
57+
no_of_kits_to_retrieve = 2
58+
59+
# Calling the fit_kit_df method will do the following:
60+
# Step 1: Retrieve and process FIT kit data
61+
# Step 2: Calculate a check digit for a single kit ID
62+
# Step 3: Convert a kit ID to a FIT Device ID
63+
64+
fit_kit_df = create_fit_id_df(tk_type_id, hub_id, no_of_kits_to_retrieve)
65+
print("Processed FIT Kit DataFrame:")
66+
print(fit_kit_df)
67+
68+
example_usage()

utils/fit_kit_generation.py

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,25 @@ def create_fit_id_df(
88
tk_type_id: int, hub_id: int, no_of_kits_to_retrieve: int
99
) -> pd.DataFrame:
1010
"""
11-
The first step here is to get the relevant test data for compartment 2
12-
Then it calculates the check digit for each kit id retrieved
13-
Finally it adds the final part on the end (expiry date + random characters)
11+
This function retrieves test kit data from the database for the specified compartment (using the 'get_kit_id_from_db' function from 'oracle_specific_functions.py').
12+
It then calculates a check digit for each retrieved kit ID and appends it to the kit ID.
13+
Finally, it generates a FIT Device ID by appending an expiry date and a fixed suffix to the kit ID.
14+
15+
For example:
16+
Given the following inputs:
17+
tk_type_id = 1, hub_id = 101, no_of_kits_to_retrieve = 2
18+
The function retrieves two kit IDs from the database, e.g., ["ABC123", "DEF456"].
19+
It calculates the check digit for each kit ID, resulting in ["ABC123-K", "DEF456-M"].
20+
Then, it generates the FIT Device IDs, e.g., ["ABC123-K122512345/KD00001", "DEF456-M122512345/KD00001"].
21+
22+
Args:
23+
tk_type_id (int): The type ID of the test kit.
24+
hub_id (int): The ID of the hub from which to retrieve the kits.
25+
no_of_kits_to_retrieve (int): The number of kits to retrieve from the database.
26+
27+
Returns:
28+
pd.DataFrame: A DataFrame containing the processed kit IDs, including the calculated check digit
29+
and the final formatted FIT Device ID.
1430
"""
1531
df = get_kit_id_from_db(tk_type_id, hub_id, no_of_kits_to_retrieve)
1632
df["fit_device_id"] = df["kitid"].apply(calculate_check_digit)
@@ -20,11 +36,23 @@ def create_fit_id_df(
2036

2137
def calculate_check_digit(kit_id: str) -> str:
2238
"""
23-
This function used used to calculate the check digit of a kit ID
24-
It calculates the check digit by getting the sum of the location of each character in the kit id
25-
Then it divides the sum by 43 and gets the remainder from this
26-
It then searches the string "char_string" to find the index of the remainder
27-
The character found is then the check digit
39+
Calculates the check digit for a given kit ID.
40+
41+
The check digit is determined by summing the positions of each character in the kit ID
42+
within a predefined character set. The remainder of the sum divided by 43 is used to
43+
find the corresponding character in the character set, which becomes the check digit.
44+
45+
For example:
46+
Given the kit ID "ABC123", the positions of the characters in the predefined
47+
character set are summed. If the total is 123, the remainder when divided by 43
48+
is 37. The character at position 37 in the character set is "K". The resulting
49+
kit ID with the check digit appended would be "ABC123-K".
50+
51+
Args:
52+
kit_id (str): The kit ID to calculate the check digit for.
53+
54+
Returns:
55+
str: The kit ID with the calculated check digit appended.
2856
"""
2957
logging.info(f"Calculating check digit for kit id: {kit_id}")
3058
total = 0
@@ -37,10 +65,17 @@ def calculate_check_digit(kit_id: str) -> str:
3765

3866
def convert_kit_id_to_fit_device_id(kit_id: str) -> str:
3967
"""
40-
This is used to add the expiry date to the end of the Kit ID
41-
This by setting the month to december
42-
And the year is set to 1 year in the future.
43-
E.g. if the current date is 06/24 the expiry date will be set to 12/25
68+
Converts a Kit ID into a FIT Device ID by appending an expiry date and a fixed suffix.
69+
70+
The expiry date is calculated by setting the month to December and the year to one year
71+
in the future based on the current date. For example, if the current date is June 2024,
72+
the expiry date will be set to December 2025.
73+
74+
Args:
75+
kit_id (str): The Kit ID to be converted.
76+
77+
Returns:
78+
str: The generated FIT Device ID in the format "{kit_id}12{next_year}12345/KD00001".
4479
"""
4580
logging.info(f"Generating FIT Device ID from: {kit_id}")
4681
today = datetime.now()

0 commit comments

Comments
 (0)