Class for more convenient calculation of excluded or included fields on values.
| 481 | |
| 482 | |
| 483 | class ValueItems(Representation): |
| 484 | """ |
| 485 | Class for more convenient calculation of excluded or included fields on values. |
| 486 | """ |
| 487 | |
| 488 | __slots__ = ('_items', '_type') |
| 489 | |
| 490 | def __init__(self, value: Any, items: Union['AbstractSetIntStr', 'MappingIntStrAny']) -> None: |
| 491 | items = self._coerce_items(items) |
| 492 | |
| 493 | if isinstance(value, (list, tuple)): |
| 494 | items = self._normalize_indexes(items, len(value)) |
| 495 | |
| 496 | self._items: 'MappingIntStrAny' = items |
| 497 | |
| 498 | def is_excluded(self, item: Any) -> bool: |
| 499 | """ |
| 500 | Check if item is fully excluded. |
| 501 | |
| 502 | :param item: key or index of a value |
| 503 | """ |
| 504 | return self.is_true(self._items.get(item)) |
| 505 | |
| 506 | def is_included(self, item: Any) -> bool: |
| 507 | """ |
| 508 | Check if value is contained in self._items |
| 509 | |
| 510 | :param item: key or index of value |
| 511 | """ |
| 512 | return item in self._items |
| 513 | |
| 514 | def for_element(self, e: 'IntStr') -> Optional[Union['AbstractSetIntStr', 'MappingIntStrAny']]: |
| 515 | """ |
| 516 | :param e: key or index of element on value |
| 517 | :return: raw values for element if self._items is dict and contain needed element |
| 518 | """ |
| 519 | |
| 520 | item = self._items.get(e) |
| 521 | return item if not self.is_true(item) else None |
| 522 | |
| 523 | def _normalize_indexes(self, items: 'MappingIntStrAny', v_length: int) -> 'DictIntStrAny': |
| 524 | """ |
| 525 | :param items: dict or set of indexes which will be normalized |
| 526 | :param v_length: length of sequence indexes of which will be |
| 527 | |
| 528 | >>> self._normalize_indexes({0: True, -2: True, -1: True}, 4) |
| 529 | {0: True, 2: True, 3: True} |
| 530 | >>> self._normalize_indexes({'__all__': True}, 4) |
| 531 | {0: True, 1: True, 2: True, 3: True} |
| 532 | """ |
| 533 | |
| 534 | normalized_items: 'DictIntStrAny' = {} |
| 535 | all_items = None |
| 536 | for i, v in items.items(): |
| 537 | if not (isinstance(v, Mapping) or isinstance(v, AbstractSet) or self.is_true(v)): |
| 538 | raise TypeError(f'Unexpected type of exclude value for index "{i}" {v.__class__}') |
| 539 | if i == '__all__': |
| 540 | all_items = self._coerce_value(v) |
no outgoing calls
no test coverage detected