Skip to content

Instantly share code, notes, and snippets.

@EricCousineau-TRI
Last active October 25, 2024 15:50
Show Gist options
  • Save EricCousineau-TRI/a765ae4e13782c9e7ed7c860ac97aa76 to your computer and use it in GitHub Desktop.
Save EricCousineau-TRI/a765ae4e13782c9e7ed7c860ac97aa76 to your computer and use it in GitHub Desktop.
pickle.dump vs. yaml.dump vs. pydrake yaml_dump, accidental serialization
from enum import Enum
import io
import pickle
import numpy as np
import torch
import yaml
from pydrake.common.yaml import yaml_dump
def pickle_dump(obj):
f = io.BytesIO()
pickle.dump(obj, f)
return f.getvalue()
class MyEnum(Enum):
Value = 1
np_value = np.array([1.0, 2.0])
torch_value = torch.tensor([3.0, 4.0])
messy = {
"enum": MyEnum.Value,
"np": np_value,
"torch": torch_value,
}
cleaned = {
"enum": messy["enum"].value,
"np": messy["np"].tolist(),
"torch": messy["torch"].numpy().tolist(),
}
print("pickle.dump")
size = len(pickle_dump(messy))
print(f" size: {size}")
size = len(pickle_dump(cleaned))
print(f" size (cleaned): {size}")
print()
print("yaml.dump")
size = len(yaml.dump(messy))
print(f" size: {size}")
size = len(yaml.dump(cleaned))
print(f" size (cleaned): {size}")
print()
print("pydrake yaml_dump")
try:
size = len(yaml_dump(messy))
print(f" size: {size}")
except Exception as e:
print(e)
size = len(yaml_dump(cleaned))
print(f" size (cleaned): {size}")
"""
Output:
pickle.dump
size: 609
size (cleaned): 82
yaml.dump
size: 1113
size (cleaned): 43
pydrake yaml_dump
('cannot represent an object', <MyEnum.Value: 1>)
size (cleaned): 41
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment