[+] Pickle utils

This commit is contained in:
Azalea (on HyDEV-Daisy)
2022-04-11 01:43:05 -04:00
parent 5f75d9e8f1
commit 7f1eb2b443
2 changed files with 24 additions and 2 deletions
+1 -2
View File
@@ -1,7 +1,6 @@
from __future__ import annotations
__version__ = "1.0.6"
__version__ = "1.0.7"
import dataclasses
import hashlib
+23
View File
@@ -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)