73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 | class TNM(BaseModel):
prefix: Optional[Prefix] = None
tumour: Optional[Tumour] = None
tumour_specification: Optional[Specification] = None
tumour_suffix: Optional[str] = None
node: Optional[Node] = None
node_specification: Optional[Specification] = None
node_suffix: Optional[str] = None
metastasis: Optional[Metastasis] = None
resection_completeness: Optional[int] = None
version: Optional[str] = None
version_year: Optional[int] = None
@validator("*", pre=True)
def coerce_o(cls, v):
if isinstance(v, str):
v = v.replace("o", "0")
return v
@validator("version_year")
def validate_year(cls, v):
if v is None:
return v
if v < 40:
v += 2000
elif v < 100:
v += 1900
return v
def norm(self) -> str:
norm = []
if self.prefix is not None:
norm.append(str(self.prefix))
if (
(self.tumour is not None)
| (self.tumour_specification is not None)
| (self.tumour_suffix is not None)
):
norm.append(f"T{str(self.tumour or '')}")
norm.append(f"{str(self.tumour_specification or '')}")
norm.append(f"{str(self.tumour_suffix or '')}")
if (
(self.node is not None)
| (self.node_specification is not None)
| (self.node_suffix is not None)
):
norm.append(f"N{str(self.node or '')}")
norm.append(f"{str(self.node_specification or '')}")
norm.append(f"{str(self.node_suffix or '')}")
if self.metastasis is not None:
norm.append(f"M{self.metastasis}")
if self.resection_completeness is not None:
norm.append(f"R{self.resection_completeness}")
if self.version is not None and self.version_year is not None:
norm.append(f" ({self.version.upper()} {self.version_year})")
return "".join(norm)
def dict(
self,
*,
include: Union["AbstractSetIntStr", "MappingIntStrAny"] = None,
exclude: Union["AbstractSetIntStr", "MappingIntStrAny"] = None,
by_alias: bool = False,
skip_defaults: bool = None,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
) -> "DictStrAny":
"""
Generate a dictionary representation of the model,
optionally specifying which fields to include or exclude.
"""
if skip_defaults is not None:
warnings.warn(
f"""{self.__class__.__name__}.dict(): "skip_defaults"
is deprecated and replaced by "exclude_unset" """,
DeprecationWarning,
)
exclude_unset = skip_defaults
d = dict(
self._iter(
to_dict=True,
by_alias=by_alias,
include=include,
exclude=exclude,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
)
)
set_keys = set(d.keys())
for k in set_keys.intersection(
{
"prefix",
"tumour",
"node",
"metastasis",
"tumour_specification",
"node_specification",
"tumour_suffix",
"node_suffix",
}
):
v = d[k]
if isinstance(v, TnmEnum):
d[k] = v.value
return d
|