|
1 | | -from typing import Optional, Dict |
| 1 | +from enum import Enum |
| 2 | +from typing import Optional |
2 | 3 |
|
3 | 4 |
|
4 | | -class EpisodeResultType: |
| 5 | +class EpisodeResultType(Enum): |
5 | 6 | NO_RESULT = (20311, "No Result") |
6 | 7 | NORMAL = (20312, "Normal (No Abnormalities Found)") |
7 | 8 | LOW_RISK_ADENOMA = (20314, "Low-risk Adenoma") |
@@ -37,80 +38,38 @@ class EpisodeResultType: |
37 | 38 | NULL = (0, "Null") |
38 | 39 | NOT_NULL = (0, "Not Null") |
39 | 40 |
|
40 | | - _all_types = [ |
41 | | - NO_RESULT, |
42 | | - NORMAL, |
43 | | - LOW_RISK_ADENOMA, |
44 | | - INTERMEDIATE_RISK_ADENOMA, |
45 | | - HIGH_RISK_ADENOMA, |
46 | | - CANCER_DETECTED, |
47 | | - ABNORMAL, |
48 | | - CANCER_NOT_CONFIRMED, |
49 | | - HIGH_RISK_FINDINGS, |
50 | | - LNPCP, |
51 | | - BOWEL_SCOPE_NON_PARTICIPATION, |
52 | | - FOBT_INADEQUATE_PARTICIPATION, |
53 | | - DEFINITIVE_NORMAL_FOBT_OUTCOME, |
54 | | - DEFINITIVE_ABNORMAL_FOBT_OUTCOME, |
55 | | - HIGH_RISK_FINDINGS_SURVEILLANCE_NON_PARTICIPATION, |
56 | | - LNPCP_SURVEILLANCE_NON_PARTICIPATION, |
57 | | - HIGH_RISK_SURVEILLANCE_NON_PARTICIPATION, |
58 | | - INTERMEDIATE_RISK_SURVEILLANCE_NON_PARTICIPATION, |
59 | | - LYNCH_NON_PARTICIPATION, |
60 | | - ANY_SURVEILLANCE_NON_PARTICIPATION, |
61 | | - NULL, |
62 | | - NOT_NULL, |
63 | | - ] |
| 41 | + def __init__(self, valid_value_id: int, description: str): |
| 42 | + self._id = valid_value_id |
| 43 | + self._description = description |
64 | 44 |
|
65 | | - _descriptions: Dict[str, "EpisodeResultType"] = {} |
66 | | - _lowercase_descriptions: Dict[str, "EpisodeResultType"] = {} |
67 | | - _valid_value_ids: Dict[int, "EpisodeResultType"] = {} |
| 45 | + @property |
| 46 | + def id(self) -> int: |
| 47 | + """Return the valid value ID.""" |
| 48 | + return self._id |
68 | 49 |
|
69 | | - def __init__(self, valid_value_id: int, description: str): |
70 | | - self.valid_value_id = valid_value_id |
71 | | - self.description = description |
| 50 | + @property |
| 51 | + def description(self) -> str: |
| 52 | + """Return the description.""" |
| 53 | + return self._description |
72 | 54 |
|
73 | 55 | @classmethod |
74 | | - def _init_types(cls): |
75 | | - if not cls._descriptions: |
76 | | - for valid_value_id, description in cls._all_types: |
77 | | - instance = cls(valid_value_id, description) |
78 | | - cls._descriptions[description] = instance |
79 | | - cls._lowercase_descriptions[description.lower()] = instance |
80 | | - # Only map the first occurrence of each valid_value_id |
81 | | - if valid_value_id not in cls._valid_value_ids: |
82 | | - cls._valid_value_ids[valid_value_id] = instance |
| 56 | + def by_id(cls, valid_value_id: int) -> Optional["EpisodeResultType"]: |
| 57 | + """Find an EpisodeResultType by its ID (returns first match if duplicates).""" |
| 58 | + return next((m for m in cls if m.id == valid_value_id), None) |
83 | 59 |
|
84 | 60 | @classmethod |
85 | 61 | def by_description(cls, description: str) -> Optional["EpisodeResultType"]: |
86 | | - cls._init_types() |
87 | | - return cls._descriptions.get(description) |
| 62 | + """Find an EpisodeResultType by its description (case-sensitive).""" |
| 63 | + return next((m for m in cls if m.description == description), None) |
88 | 64 |
|
89 | 65 | @classmethod |
90 | 66 | def by_description_case_insensitive( |
91 | 67 | cls, description: str |
92 | 68 | ) -> Optional["EpisodeResultType"]: |
93 | | - cls._init_types() |
94 | | - return cls._lowercase_descriptions.get(description.lower()) |
95 | | - |
96 | | - @classmethod |
97 | | - def by_valid_value_id(cls, valid_value_id: int) -> Optional["EpisodeResultType"]: |
98 | | - cls._init_types() |
99 | | - return cls._valid_value_ids.get(valid_value_id) |
100 | | - |
101 | | - def get_id(self) -> int: |
102 | | - return self.valid_value_id |
103 | | - |
104 | | - def get_description(self) -> str: |
105 | | - return self.description |
106 | | - |
107 | | - def __eq__(self, other): |
108 | | - if isinstance(other, EpisodeResultType): |
109 | | - return ( |
110 | | - self.valid_value_id == other.valid_value_id |
111 | | - and self.description == other.description |
112 | | - ) |
113 | | - return False |
| 69 | + """Find an EpisodeResultType by its description (case-insensitive).""" |
| 70 | + description = description.lower() |
| 71 | + return next((m for m in cls if m.description.lower() == description), None) |
114 | 72 |
|
115 | | - def __repr__(self): |
116 | | - return f"EpisodeResultType({self.valid_value_id}, '{self.description}')" |
| 73 | + def __str__(self) -> str: |
| 74 | + """Return a string representation of the EpisodeResultType.""" |
| 75 | + return f"{self.name} ({self.id}: {self.description})" |
0 commit comments