def load_hickled_object(filepath): ''' Loads an object from the filesystem. Prepends path to SimpleML HDF5 directory before loading. ONLY pass in a relative filepath from that location ''' hickle_file = join(HDF5_FILESTORE_DIRECTORY, filepath) return hickle.load(hickle_file)
def test_serialize(self): obj = TestSerializationClass("test_serialize", 1, 2, 3, other=["abc"]) filepath = f"{self.__class__.__name__}-test_serialize-{RANDOM_RUN}" self.assertFalse(isfile(join(TEMP_DIRECTORY, filepath))) data = HickleFileSerializer.serialize(obj, filepath=filepath, format_directory="", format_extension="") self.assertTrue(isfile(join(TEMP_DIRECTORY, filepath))) self.assertEqual(data["filepath"], filepath) self.assertEqual(data["source_directory"], "system_temp") deserialized = hickle.load(join(TEMP_DIRECTORY, filepath)) self.assertEqual(obj, deserialized)
def test_dump_object(self): obj = TestSerializationClass("test_dump_object", 1, 2, 3, other=["abc"]) filepath = join( TEMP_DIRECTORY, f"{self.__class__.__name__}-test_dump_object-{RANDOM_RUN}") self.assertFalse(isfile(filepath)) HicklePersistenceMethods.dump_object(obj, filepath=filepath) self.assertTrue(isfile(filepath)) deserialized = hickle.load(filepath) self.assertEqual(obj, deserialized)
def test_dump_object_without_overwrite(self): obj = TestSerializationClass("test_dump_object_without_overwrite", 1, 2, 3, other=["abc"]) obj2 = TestSerializationClass("overwrite", "a", 2, 3, other=["abc"]) filepath = join( TEMP_DIRECTORY, f"{self.__class__.__name__}-test_dump_object_without_overwrite-{RANDOM_RUN}", ) HicklePersistenceMethods.dump_object(obj, filepath=filepath) self.assertTrue(isfile(filepath)) # try overwrite HicklePersistenceMethods.dump_object(obj2, filepath=filepath, overwrite=False) deserialized = hickle.load(filepath) self.assertEqual(obj, deserialized) self.assertNotEqual(obj2, deserialized)
def load_object(filepath: str, **kwargs) -> Any: """ Loads an object from the filesystem. """ return hickle.load(filepath, **kwargs)