wg_utilities.helpers
Sentinel
Dummy value for tripping conditions, breaking loops, all sorts!
Source code in wg_utilities/helpers/__init__.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
Iterator
An iterator that will always raise StopIteration.
Source code in wg_utilities/helpers/__init__.py
38 39 40 41 42 43 44 45 46 47 |
|
__iter__()
Return an iterator that will always raise StopIteration.
Source code in wg_utilities/helpers/__init__.py
41 42 43 |
|
__next__()
Always raise StopIteration.
Source code in wg_utilities/helpers/__init__.py
45 46 47 |
|
__bool__()
Always False, so that it can be used as a sentinel value.
Source code in wg_utilities/helpers/__init__.py
11 12 13 |
|
__call__(*_, **__)
Do nothing when called.
Source code in wg_utilities/helpers/__init__.py
15 16 |
|
__eq__(other)
Return True if the other value is also a Sentinel.
Source code in wg_utilities/helpers/__init__.py
18 19 20 |
|
__hash__()
Return a hash of the class name.
Source code in wg_utilities/helpers/__init__.py
22 23 24 |
|
__iter__()
Return an iterator that will always raise StopIteration.
Source code in wg_utilities/helpers/__init__.py
26 27 28 |
|
__ne__(other)
Return False if the other value is also a Sentinel.
Source code in wg_utilities/helpers/__init__.py
30 31 32 |
|
__repr__()
Return the class name.
Source code in wg_utilities/helpers/__init__.py
34 35 36 |
|
meta
PostInitMeta
Bases: type
Metaclass to call a post-init method after the class is instantiated.
Source code in wg_utilities/helpers/meta/post_init.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
__call__(*args, **kwargs)
Call the class and then call the post-init method.
Source code in wg_utilities/helpers/meta/post_init.py
21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
post_init
Mixin to provide __post_init__
functionality.
MissingPostInitMethodError
Bases: BadDefinitionError
Raised when a class using the PostInitMeta
metaclass does not define a __post_init__
method.
Source code in wg_utilities/helpers/meta/post_init.py
10 11 12 13 14 15 |
|
__init__(cls)
Initialize the error with the class that is missing the __post_init__
method.
Source code in wg_utilities/helpers/meta/post_init.py
13 14 15 |
|
PostInitMeta
Bases: type
Metaclass to call a post-init method after the class is instantiated.
Source code in wg_utilities/helpers/meta/post_init.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
__call__(*args, **kwargs)
Call the class and then call the post-init method.
Source code in wg_utilities/helpers/meta/post_init.py
21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
mixin
InstanceCache
Mixin class to provide instance caching functionality.
Attributes:
Name | Type | Description |
---|---|---|
cache_id_attr |
str | None
|
The name of the attribute to use in the cache ID. Defaults to None.
Must be provided if |
cache_id_func |
str | None
|
The name of the function whose return value to use in the cache ID.
Defaults to "hash". Must not be |
kwargs |
Any
|
Additional keyword arguments to pass to the superclass. |
Source code in wg_utilities/helpers/mixin/instance_cache.py
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 |
|
__init_subclass__(*, cache_id_attr=None, cache_id_func=None, **kwargs)
Initialise subclasses with instance cache and cache ID attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache_id_attr |
str | None
|
The name of the attribute to use in the cache ID. Defaults to
None. Must be provided if |
None
|
cache_id_func |
str | None
|
The name of the function whose return value to use in the cache
ID. Defaults to "hash" (and must be provided) if |
None
|
kwargs |
Any
|
Additional keyword arguments to pass to the superclass. |
{}
|
Raises:
Type | Description |
---|---|
InstanceCacheSubclassError
|
If neither |
Source code in wg_utilities/helpers/mixin/instance_cache.py
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 |
|
__post_init__()
Add the instance to the cache.
Raises:
Type | Description |
---|---|
InstanceCacheDuplicateError
|
If the instance already exists in the cache. |
Source code in wg_utilities/helpers/mixin/instance_cache.py
134 135 136 137 138 139 140 141 142 143 |
|
from_cache(cache_id_)
classmethod
Get an instance from the cache by its cache ID.
Source code in wg_utilities/helpers/mixin/instance_cache.py
145 146 147 148 149 150 151 152 |
|
has_cache_entry(cache_id_)
classmethod
Check if the cache has an entry for the given cache ID.
Source code in wg_utilities/helpers/mixin/instance_cache.py
154 155 156 157 158 |
|
InstanceCacheDuplicateError
Bases: InstanceCacheSubclassError
Raised when a key is already in the cache.
Source code in wg_utilities/helpers/mixin/instance_cache.py
21 22 23 24 25 26 27 28 29 30 |
|
InstanceCacheIdError
Bases: InstanceCacheError
Raised when there is an error handling a cache ID.
Source code in wg_utilities/helpers/mixin/instance_cache.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
instance_cache
Mixin class to provide instance caching functionality.
CacheIdGenerationError
Bases: InstanceCacheIdError
Raised when there is an error generating a cache ID.
Source code in wg_utilities/helpers/mixin/instance_cache.py
66 67 68 69 70 71 72 73 74 |
|
CacheIdNotFoundError
Bases: InstanceCacheIdError
Raised when an ID is not found in the cache.
Source code in wg_utilities/helpers/mixin/instance_cache.py
55 56 57 58 59 60 61 62 63 |
|
InstanceCacheDuplicateError
Bases: InstanceCacheSubclassError
Raised when a key is already in the cache.
Source code in wg_utilities/helpers/mixin/instance_cache.py
21 22 23 24 25 26 27 28 29 30 |
|
InstanceCacheError
Bases: WGUtilitiesError
Base class for all instance cache exceptions.
Source code in wg_utilities/helpers/mixin/instance_cache.py
13 14 |
|
InstanceCacheIdError
Bases: InstanceCacheError
Raised when there is an error handling a cache ID.
Source code in wg_utilities/helpers/mixin/instance_cache.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
InstanceCacheMixin
Mixin class to provide instance caching functionality.
Attributes:
Name | Type | Description |
---|---|---|
cache_id_attr |
str | None
|
The name of the attribute to use in the cache ID. Defaults to None.
Must be provided if |
cache_id_func |
str | None
|
The name of the function whose return value to use in the cache ID.
Defaults to "hash". Must not be |
kwargs |
Any
|
Additional keyword arguments to pass to the superclass. |
Source code in wg_utilities/helpers/mixin/instance_cache.py
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 |
|
__init_subclass__(*, cache_id_attr=None, cache_id_func=None, **kwargs)
Initialise subclasses with instance cache and cache ID attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache_id_attr |
str | None
|
The name of the attribute to use in the cache ID. Defaults to
None. Must be provided if |
None
|
cache_id_func |
str | None
|
The name of the function whose return value to use in the cache
ID. Defaults to "hash" (and must be provided) if |
None
|
kwargs |
Any
|
Additional keyword arguments to pass to the superclass. |
{}
|
Raises:
Type | Description |
---|---|
InstanceCacheSubclassError
|
If neither |
Source code in wg_utilities/helpers/mixin/instance_cache.py
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 |
|
__post_init__()
Add the instance to the cache.
Raises:
Type | Description |
---|---|
InstanceCacheDuplicateError
|
If the instance already exists in the cache. |
Source code in wg_utilities/helpers/mixin/instance_cache.py
134 135 136 137 138 139 140 141 142 143 |
|
from_cache(cache_id_)
classmethod
Get an instance from the cache by its cache ID.
Source code in wg_utilities/helpers/mixin/instance_cache.py
145 146 147 148 149 150 151 152 |
|
has_cache_entry(cache_id_)
classmethod
Check if the cache has an entry for the given cache ID.
Source code in wg_utilities/helpers/mixin/instance_cache.py
154 155 156 157 158 |
|
InstanceCacheSubclassError
Bases: InstanceCacheError
Raised when a subclass is declared incorrectly.
Source code in wg_utilities/helpers/mixin/instance_cache.py
17 18 |
|
cache_id(obj)
Get the cache ID for the given object.
Source code in wg_utilities/helpers/mixin/instance_cache.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
processor
JSONProcessor
Bases: InstanceCache
Recursively process JSON objects with user-defined callbacks.
Attributes:
Name | Type | Description |
---|---|---|
_cbs |
dict
|
A mapping of types to a list of callback functions to be executed on the values of the given type. |
identifier |
str
|
A unique identifier for the JSONProcessor instance. Defaults to the hash of the instance. |
process_subclasses |
bool
|
Whether to (also) process subclasses of the target types. Defaults to True. |
process_type_changes |
bool
|
Whether to re-proce_get_itemss values if their type is updated. Can lead to recursion errors if not handled correctly. Defaults to False. |
process_pydantic_computed_fields |
bool
|
Whether to process fields that are computed alongside the regular model fields. Defaults to False. Only applicable if Pydantic is installed. |
process_pydantic_extra_fields |
bool
|
Whether to process fields that are not explicitly
defined in the Pydantic model. Only works for models with |
process_pydantic_model_properties |
bool
|
Whether to process Pydantic model properties alongside the model fields. Defaults to False. Only applicable if Pydantic is installed. |
ignored_loc_lookup_errors |
tuple
|
Exception types to ignore when looking up locations in the JSON object. Defaults to an empty tuple. |
Source code in wg_utilities/helpers/processor/json.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
|
Break
Bases: WGUtilitiesError
Escape hatch to allow breaking out of the processing loop from within a callback.
Source code in wg_utilities/helpers/processor/json.py
237 238 |
|
CallbackDefinition
Bases: NamedTuple
A named tuple to hold the callback function and its associated data.
Attributes:
Name | Type | Description |
---|---|---|
callback |
Callback
|
The callback function to execute on the target values. |
item_filter |
ItemFilter | None
|
An optional function to use to filter target values before processing them. Defaults to None. |
allow_callback_failures |
bool
|
Whether to allow callback failures. Defaults to False. |
Source code in wg_utilities/helpers/processor/json.py
202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
__init__(_cbs=None, /, *, identifier='', process_subclasses=True, process_type_changes=False, process_pydantic_computed_fields=False, process_pydantic_extra_fields=False, process_pydantic_model_properties=False, ignored_loc_lookup_errors=())
Initialize the JSONProcessor.
Source code in wg_utilities/helpers/processor/json.py
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 |
|
callback(*, allow_mutation=True)
classmethod
Decorator to mark a function as a callback for use with the JSONProcessor.
Warning
allow_mutation
only blocks the return value from being used to update the input
object. It does not prevent the callback from mutating the input object (or other
objects passed in as arguments) in place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
allow_mutation |
bool
|
Whether the callback is allowed to mutate the input object. Defaults to True. |
True
|
Source code in wg_utilities/helpers/processor/json.py
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
|
cb(callback, item_filter=None, allow_callback_failures=False, *, allow_mutation=True)
staticmethod
Create a CallbackDefinition for the given callback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callback
|
The callback function to execute on the target values. Can take None, any, or all
of the following arguments: |
required |
item_filter |
ItemFilter | None
|
An optional function to use to filter target values before processing them. Defaults to None. Function signature is as follows:
|
None
|
allow_callback_failures |
bool
|
Whether to allow callback failures. Defaults to False. |
False
|
allow_mutation |
bool
|
Whether the callback is allowed to mutate the input object. Defaults to True. |
True
|
Source code in wg_utilities/helpers/processor/json.py
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 |
|
process(obj, /, __depth=0, __processed_models=None, **kwargs)
Recursively process a JSON object with the registered callbacks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Mapping[K, object] | Sequence[object]
|
The JSON object to process. |
required |
kwargs |
Any
|
Any additional keyword arguments to pass to the callback(s). |
{}
|
Source code in wg_utilities/helpers/processor/json.py
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
|
process_anything(obj, **kwargs)
Process anything that can be processed.
Source code in wg_utilities/helpers/processor/json.py
686 687 688 |
|
process_model(model, /, __depth=0, __processed_models=None, **kwargs)
Recursively process a Pydantic model with the registered callbacks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
BaseModel
|
The Pydantic model to process. |
required |
kwargs |
Any
|
Any additional keyword arguments to pass to the callback(s). |
{}
|
Source code in wg_utilities/helpers/processor/json.py
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 |
|
register_callback(target_type, callback_def)
Register a new callback for use when processing any JSON objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_type |
type
|
The type of the values to be processed. |
required |
callback_def |
CallbackDefinition
|
The callback definition to register. |
required |
Source code in wg_utilities/helpers/processor/json.py
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 |
|
register_custom_getter(target_type, getter, *, add_to_processable_type_list=True)
Register a custom getter for use when processing any JSON objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_type |
type
|
The type of the values to be processed. |
required |
getter |
Callable
|
The custom getter to register. |
required |
add_to_processable_type_list |
bool
|
Whether to add the target type to the list of processable types. Defaults to True. |
True
|
Source code in wg_utilities/helpers/processor/json.py
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
|
register_custom_iterator(target_type, iterator, *, add_to_processable_type_list=True)
Register a custom iterator for use when processing any JSON objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_type |
type
|
The type of the values to be processed. |
required |
iterator |
Callable
|
The custom iterator to register. |
required |
add_to_processable_type_list |
bool
|
Whether to add the target type to the list of processable types. Defaults to True. |
True
|
Source code in wg_utilities/helpers/processor/json.py
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 |
|
json
Useful functions for working with JSON/dictionaries.
BaseModel
Dummy class for when pydantic is not installed.
As long as isinstance
returns False, it's all good.
Source code in wg_utilities/helpers/processor/json.py
39 40 41 42 43 |
|
CallbackNotDecoratedError
Bases: InvalidCallbackError
Raised when a callback is not decorated with @JSONProcessor.callback
.
Source code in wg_utilities/helpers/processor/json.py
114 115 116 117 118 119 120 121 122 |
|
Config
dataclass
Configuration for the JSONProcessor.
Source code in wg_utilities/helpers/processor/json.py
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 |
|
ignored_loc_lookup_errors: tuple[type[BaseException], ...]
instance-attribute
Exception types to ignore when looking up locations in the JSON object.
process_pydantic_computed_fields: bool
instance-attribute
Whether to process fields that are computed alongside the regular model fields.
process_pydantic_extra_fields: bool
instance-attribute
Whether to process fields that are not explicitly defined in the Pydantic model.
Only works for models with model_config.extra="allow"
process_pydantic_model_properties: bool
instance-attribute
Whether to process Pydantic model properties alongside the model fields.
process_subclasses: bool
instance-attribute
Whether to (also) process subclasses of the target types.
process_type_changes: bool
instance-attribute
Whether to re-process values if their type is updated.
Can lead to recursion errors if not handled correctly.
InvalidCallbackArgumentsError
Bases: InvalidCallbackError
Raised when a callback is called with extra and/or missing keyword or positional arguments.
Source code in wg_utilities/helpers/processor/json.py
87 88 89 90 91 92 93 94 95 96 97 |
|
InvalidCallbackError
Bases: BadDefinitionError
Raised for invalid callback handling.
Source code in wg_utilities/helpers/processor/json.py
78 79 80 81 82 83 84 |
|
InvalidItemFilterError
Bases: InvalidCallbackError
Raised when an item filter is not callable.
Source code in wg_utilities/helpers/processor/json.py
125 126 127 128 129 130 131 132 133 |
|
ItemFilter
Bases: Protocol
Function to filter items before processing them.
Source code in wg_utilities/helpers/processor/json.py
136 137 138 139 140 |
|
__call__(item, /, *, loc)
The function to be called on each value in the JSON object.
Source code in wg_utilities/helpers/processor/json.py
139 140 |
|
JSONProcessor
Bases: InstanceCache
Recursively process JSON objects with user-defined callbacks.
Attributes:
Name | Type | Description |
---|---|---|
_cbs |
dict
|
A mapping of types to a list of callback functions to be executed on the values of the given type. |
identifier |
str
|
A unique identifier for the JSONProcessor instance. Defaults to the hash of the instance. |
process_subclasses |
bool
|
Whether to (also) process subclasses of the target types. Defaults to True. |
process_type_changes |
bool
|
Whether to re-proce_get_itemss values if their type is updated. Can lead to recursion errors if not handled correctly. Defaults to False. |
process_pydantic_computed_fields |
bool
|
Whether to process fields that are computed alongside the regular model fields. Defaults to False. Only applicable if Pydantic is installed. |
process_pydantic_extra_fields |
bool
|
Whether to process fields that are not explicitly
defined in the Pydantic model. Only works for models with |
process_pydantic_model_properties |
bool
|
Whether to process Pydantic model properties alongside the model fields. Defaults to False. Only applicable if Pydantic is installed. |
ignored_loc_lookup_errors |
tuple
|
Exception types to ignore when looking up locations in the JSON object. Defaults to an empty tuple. |
Source code in wg_utilities/helpers/processor/json.py
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
|
Break
Bases: WGUtilitiesError
Escape hatch to allow breaking out of the processing loop from within a callback.
Source code in wg_utilities/helpers/processor/json.py
237 238 |
|
CallbackDefinition
Bases: NamedTuple
A named tuple to hold the callback function and its associated data.
Attributes:
Name | Type | Description |
---|---|---|
callback |
Callback
|
The callback function to execute on the target values. |
item_filter |
ItemFilter | None
|
An optional function to use to filter target values before processing them. Defaults to None. |
allow_callback_failures |
bool
|
Whether to allow callback failures. Defaults to False. |
Source code in wg_utilities/helpers/processor/json.py
202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
__init__(_cbs=None, /, *, identifier='', process_subclasses=True, process_type_changes=False, process_pydantic_computed_fields=False, process_pydantic_extra_fields=False, process_pydantic_model_properties=False, ignored_loc_lookup_errors=())
Initialize the JSONProcessor.
Source code in wg_utilities/helpers/processor/json.py
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 |
|
callback(*, allow_mutation=True)
classmethod
Decorator to mark a function as a callback for use with the JSONProcessor.
Warning
allow_mutation
only blocks the return value from being used to update the input
object. It does not prevent the callback from mutating the input object (or other
objects passed in as arguments) in place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
allow_mutation |
bool
|
Whether the callback is allowed to mutate the input object. Defaults to True. |
True
|
Source code in wg_utilities/helpers/processor/json.py
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
|
cb(callback, item_filter=None, allow_callback_failures=False, *, allow_mutation=True)
staticmethod
Create a CallbackDefinition for the given callback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callback
|
The callback function to execute on the target values. Can take None, any, or all
of the following arguments: |
required |
item_filter |
ItemFilter | None
|
An optional function to use to filter target values before processing them. Defaults to None. Function signature is as follows:
|
None
|
allow_callback_failures |
bool
|
Whether to allow callback failures. Defaults to False. |
False
|
allow_mutation |
bool
|
Whether the callback is allowed to mutate the input object. Defaults to True. |
True
|
Source code in wg_utilities/helpers/processor/json.py
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 |
|
process(obj, /, __depth=0, __processed_models=None, **kwargs)
Recursively process a JSON object with the registered callbacks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Mapping[K, object] | Sequence[object]
|
The JSON object to process. |
required |
kwargs |
Any
|
Any additional keyword arguments to pass to the callback(s). |
{}
|
Source code in wg_utilities/helpers/processor/json.py
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
|
process_anything(obj, **kwargs)
Process anything that can be processed.
Source code in wg_utilities/helpers/processor/json.py
686 687 688 |
|
process_model(model, /, __depth=0, __processed_models=None, **kwargs)
Recursively process a Pydantic model with the registered callbacks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
BaseModel
|
The Pydantic model to process. |
required |
kwargs |
Any
|
Any additional keyword arguments to pass to the callback(s). |
{}
|
Source code in wg_utilities/helpers/processor/json.py
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 |
|
register_callback(target_type, callback_def)
Register a new callback for use when processing any JSON objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_type |
type
|
The type of the values to be processed. |
required |
callback_def |
CallbackDefinition
|
The callback definition to register. |
required |
Source code in wg_utilities/helpers/processor/json.py
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 |
|
register_custom_getter(target_type, getter, *, add_to_processable_type_list=True)
Register a custom getter for use when processing any JSON objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_type |
type
|
The type of the values to be processed. |
required |
getter |
Callable
|
The custom getter to register. |
required |
add_to_processable_type_list |
bool
|
Whether to add the target type to the list of processable types. Defaults to True. |
True
|
Source code in wg_utilities/helpers/processor/json.py
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
|
register_custom_iterator(target_type, iterator, *, add_to_processable_type_list=True)
Register a custom iterator for use when processing any JSON objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_type |
type
|
The type of the values to be processed. |
required |
iterator |
Callable
|
The custom iterator to register. |
required |
add_to_processable_type_list |
bool
|
Whether to add the target type to the list of processable types. Defaults to True. |
True
|
Source code in wg_utilities/helpers/processor/json.py
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 |
|
LocNotFoundError
Bases: BadUsageError
, LookupError
Raised when a location is not found in the object.
Source code in wg_utilities/helpers/processor/json.py
66 67 68 69 70 71 72 73 74 75 |
|
MissingArgError
Bases: InvalidCallbackArgumentsError
Raised when a required argument is missing.
Source code in wg_utilities/helpers/processor/json.py
100 101 102 103 104 |
|
MissingKwargError
Bases: InvalidCallbackArgumentsError
Raised when a required keyword-only argument is missing.
Source code in wg_utilities/helpers/processor/json.py
107 108 109 110 111 |
|