| 617 | |
| 618 | |
| 619 | def _parse_every_object(obj, condition_func, convert_func): |
| 620 | if condition_func(obj): |
| 621 | return convert_func(obj) |
| 622 | elif type(obj) in (dict, collections.OrderedDict, list): |
| 623 | if type(obj) == list: |
| 624 | keys = range(len(obj)) |
| 625 | else: |
| 626 | keys = list(obj.keys()) |
| 627 | for key in keys: |
| 628 | if condition_func(obj[key]): |
| 629 | obj[key] = convert_func(obj[key]) |
| 630 | else: |
| 631 | obj[key] = _parse_every_object( |
| 632 | obj[key], condition_func, convert_func |
| 633 | ) |
| 634 | return obj |
| 635 | elif type(obj) == tuple: |
| 636 | return tuple( |
| 637 | _parse_every_object(list(obj), condition_func, convert_func) |
| 638 | ) |
| 639 | elif type(obj) == set: |
| 640 | return set(_parse_every_object(list(obj), condition_func, convert_func)) |
| 641 | else: |
| 642 | # Support dataclass objects - return as-is without further parsing |
| 643 | if dataclasses.is_dataclass(obj): |
| 644 | return obj |
| 645 | if isinstance(obj, Iterable) and not isinstance( |
| 646 | obj, |
| 647 | (str, np.ndarray, core.eager.Tensor, core.DenseTensor), |
| 648 | ): |
| 649 | raise NotImplementedError( |
| 650 | f"The iterable objects supported are tuple, list, dict, OrderedDict, string. But received {type(obj)}." |
| 651 | ) |
| 652 | return obj |
| 653 | |
| 654 | |
| 655 | def _parse_load_result(obj, return_numpy): |