1234567891011121314151617181920212223242526272829303132333435363738394041 |
- import pickle
- from io import BytesIO
- import joblib
- import torch
- class JoblibSerializer:
- @staticmethod
- def dumps(obj) -> bytes:
- s = BytesIO()
- joblib.dump(obj, s)
- return s.getvalue()
- @staticmethod
- def loads(buf: bytes):
- return joblib.load(BytesIO(buf))
- class PickleSerializer:
- @staticmethod
- def dumps(obj) -> bytes:
- return pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL)
- @staticmethod
- def loads(buf: bytes):
- return pickle.loads(buf)
- class PytorchSerializer:
- @staticmethod
- def dumps(obj) -> bytes:
- s = BytesIO()
- torch.save(obj, s, pickle_protocol=pickle.HIGHEST_PROTOCOL)
- return s.getvalue()
- @staticmethod
- def loads(buf: bytes):
- return torch.load(BytesIO(buf))
|