Parse 16-24, 65+ or 65-+ into inclusive bounds.
Source code in may/utils/age_bands.py
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 | def parse_age_band(label: str, *, strict: bool = False) -> Optional[Tuple[int, int]]:
"""Parse ``16-24``, ``65+`` or ``65-+`` into inclusive bounds."""
try:
if not isinstance(label, str):
raise ValueError
if label.endswith("+") and "-" not in label:
return int(label[:-1]), OPEN_BAND_MAX
parts = label.split("-")
if len(parts) != 2:
raise ValueError
low = int(parts[0])
high = OPEN_BAND_MAX if parts[1] == "+" else int(parts[1])
return low, high
except (TypeError, ValueError):
if strict:
raise ValueError(f"Unrecognized age band: {label!r}") from None
return None
|