[docs]defread_pickled_data(filepath:str)->Any:"""Read pickled data from a file and return it. Args: filepath (str): The path to the file to read from. Returns: Any: The pickled data being read, or None if the file does not exist. """ensure_parent_directory_exists(filepath)try:withopen(filepath,"rb")asfile:returnpickle.load(file)exceptFileNotFoundError:returnNone
[docs]defensure_parent_directory_exists(filepath:str)->None:"""Create the parent directory of a file if it doesn't exist. Args: filepath (str): The path to the file. """ifdirectory:=os.path.dirname(filepath):os.makedirs(directory,exist_ok=True)else:raiseValueError("filepath must include a directory component")
[docs]defwrite_pickled_data(filepath:str,data:dict)->None:"""Write pickled data to a file. Args: filepath (str): The path to the file to write to. data (dict): The data to be pickled and written to the file. """ensure_parent_directory_exists(filepath)withopen(filepath,"wb")asfile:pickle.dump(data,file)
[docs]defdelete_pickled_data(filepath:str)->None:"""Delete a pickled file. Args: filepath (str): The path to the file to delete. """os.remove(filepath)