serializer.py
1 """ 2 Serializer module 3 """ 4 5 from .errors import SerializeError 6 from .factory import SerializeFactory 7 8 9 class Serializer: 10 """ 11 Methods to serialize and deserialize data. 12 """ 13 14 @staticmethod 15 def load(path): 16 """ 17 Loads data from path. This method first tries to load the default serialization format. 18 If that fails, it will fallback to pickle format for backwards-compatability purposes. 19 20 Note that loading pickle files requires the env variable `ALLOW_PICKLE=True`. 21 22 Args: 23 path: data to load 24 25 Returns: 26 data 27 """ 28 29 try: 30 return SerializeFactory.create().load(path) 31 except SerializeError: 32 # Backwards compatible check for pickled data 33 return SerializeFactory.create("pickle").load(path) 34 35 @staticmethod 36 def save(data, path): 37 """ 38 Saves data to path. 39 40 Args: 41 data: data to save 42 path: output path 43 """ 44 45 # Save using default serialization method 46 SerializeFactory.create().save(data, path)