import pickle


def _get_data_from_rawfile(path_to_data, raw_data_id):
    """Get a HandwrittenData object that has ``raw_data_id`` from a pickle file
    ``path_to_data``.
    :returns: The HandwrittenData object if ``raw_data_id`` is in
              path_to_data, otherwise ``None``."""
    with open(path_to_data, "rb") as fp:
        loaded = pickle.load(fp)
    raw_datasets = loaded["handwriting_datasets"]
    for raw_dataset in raw_datasets:
        if raw_dataset["handwriting"].raw_data_id == raw_data_id:
            return raw_dataset["handwriting"]
    return None