| 32 | |
| 33 | |
| 34 | def check_json_format(obj: Any, token_safe: bool = True) -> Any: |
| 35 | if obj is None or isinstance(obj, (int, float, str, complex)): # bool is a subclass of int |
| 36 | return obj |
| 37 | if isinstance(obj, bytes): |
| 38 | return '<<<bytes>>>' |
| 39 | if isinstance(obj, (torch.dtype, torch.device)): |
| 40 | obj = str(obj) |
| 41 | return obj[len('torch.'):] if obj.startswith('torch.') else obj |
| 42 | |
| 43 | if isinstance(obj, Sequence): |
| 44 | res = [] |
| 45 | for x in obj: |
| 46 | res.append(check_json_format(x, token_safe)) |
| 47 | elif isinstance(obj, Mapping): |
| 48 | res = {} |
| 49 | for k, v in obj.items(): |
| 50 | if token_safe and isinstance(k, str) and '_token' in k and isinstance(v, str): |
| 51 | res[k] = None |
| 52 | else: |
| 53 | res[k] = check_json_format(v, token_safe) |
| 54 | else: |
| 55 | if token_safe: |
| 56 | unsafe_items = {} |
| 57 | for k, v in obj.__dict__.items(): |
| 58 | if '_token' in k: |
| 59 | unsafe_items[k] = v |
| 60 | setattr(obj, k, None) |
| 61 | res = repr(obj) |
| 62 | # recover |
| 63 | for k, v in unsafe_items.items(): |
| 64 | setattr(obj, k, v) |
| 65 | else: |
| 66 | res = repr(obj) # e.g. function, object |
| 67 | return res |
| 68 | |
| 69 | |
| 70 | def _get_version(work_dir: str) -> int: |