-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedical-data-validator.py
More file actions
59 lines (57 loc) · 1.65 KB
/
medical-data-validator.py
File metadata and controls
59 lines (57 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#Lists storing dictionaries
medical_records = [
{
'patient_id': 'P1001',
'age': 34,
'gender': 'Female',
'diagnosis': 'Hypertension',
'medications': ['Lisinopril'],
'last_visit_id': 'V2301',
},
{
'patient_id': 'p1002',
'age': 47,
'gender': 'male',
'diagnosis': 'Type 2 Diabetes',
'medications': ['Metformin', 'Insulin'],
'last_visit_id': 'v2302',
},
{
'patient_id': 'P1003',
'age': 29,
'gender': 'female',
'diagnosis': 'Asthma',
'medications': ['Albuterol'],
'last_visit_id': 'v2303',
},
{
'patient_id': 'p1004',
'age': 56,
'gender': 'Male',
'diagnosis': 'Chronic Back Pain',
'medications': ['Ibuprofen', 'Physical Therapy'],
'last_visit_id': 'V2304',
}
]
def validate(data):
is_sequence = isinstance(data, (list, tuple))
if not is_sequence:
print('Invalid format: expected a list or tuple.')
return False
is_invalid = False
key_set = set(
['patient_id', 'age', 'gender', 'diagnosis', 'medications', 'last_visit_id']
)
for index, dictionary in enumerate(data):
if not isinstance(dictionary, dict):
print(f'Invalid format: expected a dictionary at position {index}.')
is_invalid = True
if set(dictionary.keys()) != key_set:
print(
f'Invalid format: {dictionary} at position {index} has missing and/or invalid keys.'
)
is_invalid = True
if is_invalid:
return False
print('Valid format')
return True