Composition pattern
Composition pattern parsing and manipulation for households.
This module handles household composition patterns like ">=2 >=0 2 0" which specify the required number of people in each age category.
CompositionPattern
dataclass
Represents a household composition pattern.
Example: ">=2 >=0 2 <=2" means: - 2 or more people in category 0 (Kids) - 0 or more people in category 1 (Young Adults) - exactly 2 people in category 2 (Adults) - 2 or fewer people in category 3 (Old Adults)
Source code in may/residence/composition_pattern.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
__post_init__()
Initialize cached properties.
Source code in may/residence/composition_pattern.py
81 82 83 84 | |
demote_once(priority_order)
Attempt to demote this pattern by reducing requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
priority_order
|
List[int]
|
List of category indices in order of demotion priority |
required |
Returns:
| Type | Description |
|---|---|
Optional[CompositionPattern]
|
New CompositionPattern with reduced requirements, or None if can't demote |
Source code in may/residence/composition_pattern.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
demote_to_count(cat_idx, target_count)
Demote a specific category directly to a target count.
This is more efficient than calling demote_once multiple times, as it jumps directly to the available count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cat_idx
|
int
|
Category index to demote |
required |
target_count
|
int
|
Target count to demote to (usually the available count) |
required |
Returns:
| Type | Description |
|---|---|
Optional[CompositionPattern]
|
New CompositionPattern with the category set to >=target_count, or None if can't demote |
Source code in may/residence/composition_pattern.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
from_string(pattern)
classmethod
Parse a composition pattern string (with object-level caching).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Pattern string like ">=2 >=0 2 <=2" |
required |
Returns:
| Type | Description |
|---|---|
CompositionPattern
|
CompositionPattern object |
Source code in may/residence/composition_pattern.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
get_max_count(category_idx)
Get maximum allowed count for a category (None if unlimited).
Source code in may/residence/composition_pattern.py
95 96 97 98 99 100 101 102 103 104 105 | |
get_min_count(category_idx)
Get minimum required count for a category.
Source code in may/residence/composition_pattern.py
86 87 88 89 90 91 92 93 | |
is_flexible(category_idx)
Check if a category has flexible (>= or <=) requirement.
Source code in may/residence/composition_pattern.py
107 108 109 110 111 112 | |
min_household_size()
Calculate minimum household size required (with caching).
Source code in may/residence/composition_pattern.py
114 115 116 117 118 | |
promote_once(priority_order)
Attempt to promote this pattern by relaxing requirements to allow more people.
Promotion converts
- "0" (exact) -> ">=0" (flexible, allow any)
- "N" (exact) -> ">=N" (flexible, allow N or more)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
priority_order
|
List[int]
|
List of category indices in order of promotion priority |
required |
Returns:
| Type | Description |
|---|---|
Optional[CompositionPattern]
|
New CompositionPattern with relaxed requirements, or None if can't promote |
Source code in may/residence/composition_pattern.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
to_string()
Get current pattern as string (with lazy caching).
Source code in may/residence/composition_pattern.py
335 336 337 338 339 | |
validate_against_rules(validation_rules, category_name_to_idx)
Validate this pattern against a list of validation rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validation_rules
|
List[Dict]
|
List of rule dicts from config |
required |
category_name_to_idx
|
Dict[str, int]
|
Mapping from category name to index |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if pattern passes all rules, False otherwise |
Source code in may/residence/composition_pattern.py
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 | |