diff --git a/hypy_utils/__init__.py b/hypy_utils/__init__.py index 3191493..b7393dc 100644 --- a/hypy_utils/__init__.py +++ b/hypy_utils/__init__.py @@ -1,7 +1,6 @@ from __future__ import annotations - -__version__ = "1.0.6" +__version__ = "1.0.7" import dataclasses import hashlib diff --git a/hypy_utils/serializer.py b/hypy_utils/serializer.py new file mode 100644 index 0000000..736ba28 --- /dev/null +++ b/hypy_utils/serializer.py @@ -0,0 +1,23 @@ +import io +import pickle + + +def pickle_encode(obj: any, protocol=None, fix_imports=True) -> bytes: + """ + Encode object to pickle bytes + + >>> by = pickle_encode({'meow': 565656}) + >>> pickle_decode(by) + {'meow': 565656} + """ + with io.BytesIO() as bio: + pickle.dump(obj, bio, protocol=protocol, fix_imports=fix_imports) + return bio.getvalue() + + +def pickle_decode(by: bytes) -> any: + """ + Decode pickle bytes to object + """ + with io.BytesIO(by) as bio: + return pickle.load(bio)