Collections¶
Collection classes and utilities for working with collections of data.
Classes¶
DeepChainMap¶
A recursive capable deep chain map.
Tree¶
A subclass of dict for creating tree structures from sequences.
ValidatedStr¶
A class for creating validated strings based on regex patterns.
Mixins¶
PostInitMixin¶
A mixin class for handling post-initialization tasks.
PydanticStrMixin¶
A mixin for Pydantic models that provides a custom CoreSchema for string validation.
Enums¶
BaseEnum¶
A base class for creating enums.
OrderedEnum¶
A base class for creating ordered enums.
StrEnum¶
A base class for creating string enums.
OrderedStrEnum¶
A base class for creating ordered string enums.
BaseEnum ¶
Bases: Enum
Enum extension class that makes string comparisons easier
class MyEnum(BaseEnum): BLARG = "blarg"
assert MyEnum.BLARG == "blarg"
values
classmethod
¶
values()
Return a list of all member values.
Returns:
| Type | Description |
|---|---|
list[Any]
|
List of enum member values. |
Source code in src/aibs_informatics_core/collections.py
511 512 513 514 515 516 517 518 | |
BaseEnumMeta ¶
Bases: EnumMeta
Metaclass for BaseEnum type
DeepChainMap ¶
Bases: ChainMap
A recursive subclass of ChainMap Modified based on https://github.com/neutrinoceros/deep_chainmap solution
to_dict ¶
to_dict()
Flatten all chained maps into a single dictionary.
Performs a depth-first merge of all maps, with earlier maps taking precedence over later ones.
Returns:
| Type | Description |
|---|---|
dict
|
A single merged dictionary. |
Source code in src/aibs_informatics_core/collections.py
71 72 73 74 75 76 77 78 79 80 81 82 83 | |
OrderedStrEnum ¶
Bases: str, OrderedEnum
A string enum that supports ordering based on member definition order.
values
classmethod
¶
values()
Return a list of all member values as strings.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of enum member values. |
Source code in src/aibs_informatics_core/collections.py
567 568 569 570 571 572 573 574 | |
PostInitMixin ¶
Mixin that adds __post_init__ hook support to classes.
When used with add_hook=True in __init_subclass__, automatically
calls __post_init__ after __init__ completes. This is useful for
adding validation or derived attribute computation after initialization.
PydanticStrMixin ¶
Mixin for Pydantic models that provides a custom CoreSchema for string validation.
StrEnum ¶
Bases: str, BaseEnum
An enum whose members are also strings, allowing direct string comparison.
values
classmethod
¶
values()
Return a list of all member values as strings.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of enum member values. |
Source code in src/aibs_informatics_core/collections.py
553 554 555 556 557 558 559 560 | |
Tree ¶
Bases: dict[KT, 'Tree'], Generic[KT]
A recursive dictionary for building tree structures from sequences.
Each key maps to a child Tree, forming an n-ary tree. Provides methods
to add, retrieve, and enumerate paths (sequences of keys) through the tree.
add_sequence ¶
add_sequence(*keys)
Add a path of keys to the tree, creating intermediate nodes as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys
|
KT
|
Ordered sequence of keys representing a path from root to leaf. |
()
|
Source code in src/aibs_informatics_core/collections.py
112 113 114 115 116 117 118 119 120 121 122 | |
get_sequence ¶
get_sequence(*keys)
Retrieve the subtree at the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys
|
KT
|
Ordered sequence of keys to traverse. |
()
|
Returns:
| Type | Description |
|---|---|
Optional[Tree[KT]]
|
The subtree at the end of the path, or None if the path does not exist. |
Source code in src/aibs_informatics_core/collections.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
has_sequence ¶
has_sequence(*keys)
Check whether a path of keys exists in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys
|
KT
|
Ordered sequence of keys to look up. |
()
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the path exists, False otherwise. |
Source code in src/aibs_informatics_core/collections.py
140 141 142 143 144 145 146 147 148 149 | |
to_sequences ¶
to_sequences()
Enumerate all root-to-leaf paths in the tree.
Returns:
| Type | Description |
|---|---|
list[tuple[KT, ...]]
|
A list of tuples, each representing a path from root to leaf. |
Source code in src/aibs_informatics_core/collections.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
ValidatedStr ¶
ValidatedStr(*args, **kwargs)
Bases: str, PostInitMixin, PydanticStrMixin
A string subclass with regex-based validation.
Subclasses should define a regex_pattern class variable to enforce
a specific string format. Optional min_len and max_len class
variables can constrain string length.
Attributes:
| Name | Type | Description |
|---|---|---|
regex_pattern |
Pattern
|
Compiled regex pattern used for validation. |
min_len |
int | None
|
Minimum allowed string length, or None for no limit. |
max_len |
int | None
|
Maximum allowed string length, or None for no limit. |
Placeholder for subclass to override
Source code in src/aibs_informatics_core/collections.py
302 303 304 | |
find_prefix
classmethod
¶
find_prefix(string)
Find the first regex match at the start of the string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
The string to search. |
required |
Returns:
| Type | Description |
|---|---|
S | None
|
A validated instance of the matched prefix, or None if no prefix matches. |
Source code in src/aibs_informatics_core/collections.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
find_suffix
classmethod
¶
find_suffix(string)
Find the first regex match at the end of the string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
The string to search. |
required |
Returns:
| Type | Description |
|---|---|
S | None
|
A validated instance of the matched suffix, or None if no suffix matches. |
Source code in src/aibs_informatics_core/collections.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
findall
classmethod
¶
findall(string)
Convenience method for re.findall
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[T]
|
ValidatedStr subclass |
required |
string
|
str
|
string to find patterns within |
required |
Returns:
| Type | Description |
|---|---|
list[S]
|
List of substrings matching pattern |
Source code in src/aibs_informatics_core/collections.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
get_match_groups ¶
get_match_groups()
Return the captured groups from matching the regex pattern against this string.
Returns:
| Type | Description |
|---|---|
Sequence[Any]
|
A sequence of matched groups. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If no regex pattern is defined. |
Source code in src/aibs_informatics_core/collections.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
has_regex_pattern
classmethod
¶
has_regex_pattern()
Check if this class has a user-defined regex pattern.
Returns:
| Type | Description |
|---|---|
bool
|
True if a regex pattern was explicitly provided. |
Source code in src/aibs_informatics_core/collections.py
451 452 453 454 455 456 457 458 | |
is_prefixed
classmethod
¶
is_prefixed(string)
Check if the string starts with a match of the regex pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
The string to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if a match is found at the start of the string. |
Source code in src/aibs_informatics_core/collections.py
377 378 379 380 381 382 383 384 385 386 387 | |
is_suffixed
classmethod
¶
is_suffixed(string)
Check if the string ends with a match of the regex pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
string
|
str
|
The string to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if a match is found at the end of the string. |
Source code in src/aibs_informatics_core/collections.py
405 406 407 408 409 410 411 412 413 414 415 | |
is_valid
classmethod
¶
is_valid(value)
Check if a string is a valid instance of this validated string type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The string to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the value passes validation, False otherwise. |
Source code in src/aibs_informatics_core/collections.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | |
suball
classmethod
¶
suball(string, repl)
Convenience method for running re.sub on string. If no regex pattern is defined, then return original. Args: cls (Type[T]): The ValidatedStr subclass s (str): String to find/replace repl (Union[str, Callable[[Match], str]]): replacement method Returns: string with replacements
Source code in src/aibs_informatics_core/collections.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
validate_regex_pattern
classmethod
¶
validate_regex_pattern(raise_error=True)
Validate that a regex pattern is defined for this class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raise_error
|
bool
|
If True, raise an error when no pattern is defined. If False, log a warning instead. |
True
|
Raises:
| Type | Description |
|---|---|
ValidationError
|
If |
Source code in src/aibs_informatics_core/collections.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |