9e8db857f4
git-subtree-dir: utils_v2 git-subtree-split: 3be5145c7a4cfede04d753324dfae31ace913c98
123 lines
4.7 KiB
Python
123 lines
4.7 KiB
Python
"""
|
|
|
|
AUTHOR:
|
|
|
|
Khushal P Soonderji
|
|
|
|
DATE:
|
|
|
|
Saturday 26th Oct. 2024.
|
|
|
|
OBJECTIVE:
|
|
|
|
To provide a way to convert any input data to serialized bytes, and back.
|
|
|
|
REFERENCES:
|
|
|
|
N/A
|
|
|
|
DOWNLOADS:
|
|
|
|
N/A
|
|
|
|
"""
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** IMPORT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# To make sibling directories accessible for imports:
|
|
import sys
|
|
sys.path.append(".")
|
|
sys.path.append("..")
|
|
|
|
# To work with pickling:
|
|
import pickle
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MACROS / ONE-TIME INIT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** VARIABLES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** FUNCTIONS ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** CLASSES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
class PickleSerializer:
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
Use this serializer when working with custom python objects. This is meant to be fully flexible, but efficiency
|
|
is not guaranteed.
|
|
"""
|
|
|
|
pass
|
|
|
|
@staticmethod
|
|
def serialize(data):
|
|
|
|
"""
|
|
Serializes the data that is given to it.
|
|
:param data: The data to serialize.
|
|
:return: The bytes representing the data.
|
|
"""
|
|
|
|
return pickle.dumps(data)
|
|
|
|
@staticmethod
|
|
def deserialize(data):
|
|
|
|
"""
|
|
Deserializes the bytes that are given to it.
|
|
:param data: The bytes to deserialize.
|
|
:return: The data from the bytes that described it.
|
|
"""
|
|
|
|
return pickle.loads(data)
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MAIN PROGRAM ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|