Iterate across keys and optionally extra iterables. If key is missing, exception is raised if `allow_missing_keys==False` (default). If `allow_missing_keys==True`, key is skipped. Args: data: data that the transform will be applied to extra_iterables
(self, data: Mapping[Hashable, Any], *extra_iterables: Iterable | None)
| 466 | raise NotImplementedError(f"Subclass {self.__class__.__name__} must implement this method.") |
| 467 | |
| 468 | def key_iterator(self, data: Mapping[Hashable, Any], *extra_iterables: Iterable | None) -> Generator: |
| 469 | """ |
| 470 | Iterate across keys and optionally extra iterables. If key is missing, exception is raised if |
| 471 | `allow_missing_keys==False` (default). If `allow_missing_keys==True`, key is skipped. |
| 472 | |
| 473 | Args: |
| 474 | data: data that the transform will be applied to |
| 475 | extra_iterables: anything else to be iterated through |
| 476 | """ |
| 477 | # if no extra iterables given, create a dummy list of Nones |
| 478 | ex_iters = extra_iterables or [[None] * len(self.keys)] |
| 479 | |
| 480 | # loop over keys and any extra iterables |
| 481 | _ex_iters: list[Any] |
| 482 | for key, *_ex_iters in zip(self.keys, *ex_iters): |
| 483 | # all normal, yield (what we yield depends on whether extra iterables were given) |
| 484 | if key in data: |
| 485 | yield (key,) + tuple(_ex_iters) if extra_iterables else key |
| 486 | elif not self.allow_missing_keys: |
| 487 | raise KeyError( |
| 488 | f"Key `{key}` of transform `{self.__class__.__name__}` was missing in the data and allow_missing_keys==False." |
| 489 | ) |
| 490 | |
| 491 | def first_key(self, data: dict[Hashable, Any]): |
| 492 | """ |