data_handler.py 1006 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
"""
General IO for pickle database operations.
"""

import pickle


def load_database(file_location: str):
    """
    Attempts to load the pickle database at the given file location
    :param file_location: String location of file to be loaded.
    :return: The loaded pickle database.
    """
Kyle Anderson's avatar
Kyle Anderson committed
14 15 16 17 18 19
    file_content = {}
    try:
        with open(file_location, "rb") as database:
            file_content = pickle.load(database)
    except (FileNotFoundError, EOFError):
        file_content = {}
20 21 22 23 24 25 26 27 28 29
    return file_content


def write_database(output_file: str, database_content: dict) -> None:
    """
    Writes the dictionary database to the given file location
    :param output_file: The location of the file to be outputted on.
    :param database_content: The database content to be written to the file.
    :return: None
    """
Kyle Anderson's avatar
Kyle Anderson committed
30 31
    if output_file and database_content and database_content is not None:
        with open(output_file, "wb") as output:
Kyle Anderson's avatar
Kyle Anderson committed
32
            pickle.dump(database_content, output)