Created
February 10, 2023 15:24
-
-
Save ichisadashioko/5313d1d78c78a020720c003d5725782a to your computer and use it in GitHub Desktop.
make object pickle friendly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def make_obj_pickle_friendly(obj): | |
try: | |
pickle.dumps(obj) | |
return obj | |
except Exception: | |
if isinstance(obj, dict): | |
return { | |
key: make_obj_pickle_friendly(value) | |
for key, value in obj.items() | |
} | |
elif isinstance(obj, list): | |
return [ | |
make_obj_pickle_friendly(value) | |
for value in obj | |
] | |
elif isinstance(obj, tuple): | |
return tuple( | |
make_obj_pickle_friendly(value) | |
for value in obj | |
) | |
else: | |
return repr(obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment