Skip to content

Instantly share code, notes, and snippets.

@ichisadashioko
Created February 10, 2023 15:24
Show Gist options
  • Save ichisadashioko/5313d1d78c78a020720c003d5725782a to your computer and use it in GitHub Desktop.
Save ichisadashioko/5313d1d78c78a020720c003d5725782a to your computer and use it in GitHub Desktop.
make object pickle friendly
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