|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import inspect |
| 4 | +import itertools |
4 | 5 | import re |
5 | 6 | import textwrap |
6 | 7 | from typing import TYPE_CHECKING, Any, TypeVar |
@@ -36,6 +37,132 @@ def validate_date_range(start: datetime.date, end: datetime.date): |
36 | 37 | raise ValueError(f"The start date {start} must be before the end date {end}.") |
37 | 38 |
|
38 | 39 |
|
| 40 | +def get_re_pattern_for_all_time_units_and_groupings( |
| 41 | + supported_groupings: tuple[str, ...], supported_time_units: tuple[str, ...] |
| 42 | +) -> re.Pattern: |
| 43 | + """Get a regex pattern for time units and groupings. |
| 44 | +
|
| 45 | + The pattern matches strings in any of these formats: |
| 46 | + - <base_name> (may contain underscores) |
| 47 | + - <base_name>_<time_unit> |
| 48 | + - <base_name>_<aggregation> |
| 49 | + - <base_name>_<time_unit>_<aggregation> |
| 50 | +
|
| 51 | + Parameters |
| 52 | + ---------- |
| 53 | + supported_groupings |
| 54 | + The supported groupings. |
| 55 | + supported_time_units |
| 56 | + The supported time units. |
| 57 | +
|
| 58 | + Returns |
| 59 | + ------- |
| 60 | + pattern |
| 61 | + The regex pattern. |
| 62 | + """ |
| 63 | + units = "".join(supported_time_units) |
| 64 | + groupings = "|".join(supported_groupings) |
| 65 | + return re.compile( |
| 66 | + f"(?P<base_name>.*?)" |
| 67 | + f"(?:_(?P<time_unit>[{units}]))?" |
| 68 | + f"(?:_(?P<aggregation>{groupings}))?" |
| 69 | + f"$" |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +def get_re_pattern_for_specific_time_units_and_groupings( |
| 74 | + base_name: str, |
| 75 | + supported_time_units: tuple[str, ...], |
| 76 | + supported_groupings: tuple[str, ...], |
| 77 | +) -> re.Pattern: |
| 78 | + """Get a regex for a specific base name with optional time unit and aggregation. |
| 79 | +
|
| 80 | + The pattern matches strings in any of these formats: |
| 81 | + - <specific_base_name> |
| 82 | + - <specific_base_name>_<time_unit> |
| 83 | + - <specific_base_name>_<aggregation> |
| 84 | + - <specific_base_name>_<time_unit>_<aggregation> |
| 85 | +
|
| 86 | + Parameters |
| 87 | + ---------- |
| 88 | + base_name |
| 89 | + The specific base name to match. |
| 90 | + supported_time_units |
| 91 | + The supported time units. |
| 92 | + supported_groupings |
| 93 | + The supported groupings. |
| 94 | +
|
| 95 | + Returns |
| 96 | + ------- |
| 97 | + pattern |
| 98 | + The regex pattern. |
| 99 | + """ |
| 100 | + units = "".join(supported_time_units) |
| 101 | + groupings = "|".join(supported_groupings) |
| 102 | + return re.compile( |
| 103 | + f"(?P<base_name>{re.escape(base_name)})" |
| 104 | + f"(?:_(?P<time_unit>[{units}]))?" |
| 105 | + f"(?:_(?P<aggregation>{groupings}))?" |
| 106 | + f"$" |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +def all_variations_of_base_name( |
| 111 | + base_name: str, |
| 112 | + supported_time_conversions: list[str], |
| 113 | + supported_groupings: list[str], |
| 114 | + create_conversions_for_time_units: bool, |
| 115 | +) -> set[str]: |
| 116 | + """Get possible derived function names given a base function name. |
| 117 | +
|
| 118 | + Examples |
| 119 | + -------- |
| 120 | + >>> all_variations_of_base_name( |
| 121 | + base_name="income", |
| 122 | + supported_time_conversions=["y", "m"], |
| 123 | + supported_groupings=["hh"], |
| 124 | + create_conversions_for_time_units=True, |
| 125 | + ) |
| 126 | + {'income_m', 'income_y', 'income_hh_y', 'income_hh_m'} |
| 127 | +
|
| 128 | + >>> all_variations_of_base_name( |
| 129 | + base_name="claims_benefits", |
| 130 | + supported_time_conversions=["y", "m"], |
| 131 | + supported_groupings=["hh"], |
| 132 | + create_conversions_for_time_units=False, |
| 133 | + ) |
| 134 | + {'claims_benefits_hh'} |
| 135 | +
|
| 136 | + Parameters |
| 137 | + ---------- |
| 138 | + base_name |
| 139 | + The base function name. |
| 140 | + supported_time_conversions |
| 141 | + The supported time conversions. |
| 142 | + supported_groupings |
| 143 | + The supported groupings. |
| 144 | + create_conversions_for_time_units |
| 145 | + Whether to create conversions for time units. |
| 146 | +
|
| 147 | + Returns |
| 148 | + ------- |
| 149 | + The names of all potential targets based on the base name. |
| 150 | + """ |
| 151 | + result = set() |
| 152 | + if create_conversions_for_time_units: |
| 153 | + for time_unit in supported_time_conversions: |
| 154 | + result.add(f"{base_name}_{time_unit}") |
| 155 | + for time_unit, aggregation in itertools.product( |
| 156 | + supported_time_conversions, supported_groupings |
| 157 | + ): |
| 158 | + result.add(f"{base_name}_{time_unit}_{aggregation}") |
| 159 | + else: |
| 160 | + result.add(base_name) |
| 161 | + for aggregation in supported_groupings: |
| 162 | + result.add(f"{base_name}_{aggregation}") |
| 163 | + return result |
| 164 | + |
| 165 | + |
39 | 166 | class KeyErrorMessage(str): |
40 | 167 | """Subclass str to allow for line breaks in KeyError messages.""" |
41 | 168 |
|
|
0 commit comments