-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroup.py
More file actions
32 lines (26 loc) · 1.19 KB
/
group.py
File metadata and controls
32 lines (26 loc) · 1.19 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
import json
from tools import BaseData
from typing import List
from patient import Patient
class Group(BaseData):
def __init__(self, name: str = "", patients: List[Patient] = None, ID: str = ""):
super().__init__(ID)
self.name = name
self.patients = patients if patients is not None else []
def to_json_data(self) -> dict:
json_data = super().to_json_data()
json_data['Name'] = self.name
json_data['Patients'] = [patient.ID for patient in self.patients]
return json_data
def to_json_file(self, json_file):
with open(json_file, "w") as f:
json.dump(self.to_json_data(), f, indent=2)
@classmethod
def from_json_file(cls, json_file, project_patients: List[Patient] = None) -> 'Group':
with open(json_file, "r") as f:
return cls.from_json_data(json.load(f), project_patients)
@classmethod
def from_json_data(cls, json_data: dict, project_patients: List[Patient] = None) -> 'Group':
return cls(json_data["Name"],
[next(patient for patient in project_patients if patient.ID == patient_ID) for patient_ID in json_data["Patients"]],
json_data["ID"])