import pickle


def pickle_iffnn(data, fp, verbose=1):
    """
    Write data to pickle if fp is not None
    """
    if fp is not None:
        if verbose:
            if '_file' in vars(fp):
                # getting the filename of MongoDB gridfs file
                fname = vars(fp)['_file']['filename']
            else:
                # getting the filename of a os file
                fname = fp.name
            print(f'Writing {fname}')

        fp.write(pickle.dumps(data))
        return True
    return False