import pickle


def load_compounds(filename):
    """
    Load Materials Project database saved in .pckl file and check it has
    expected form.
    args:
        filename (str) - name of file containing data
    output:
        list of dicts of data
    """
    print("Loading Compounds....")
    file = open(filename, 'rb')
    all_compounds_data = pickle.load(file)
    all_compounds = []
    print("Cleaning data....")
    for compound in all_compounds_data:
        if None not in compound.values():
            all_compounds.append(compound)

    print("Checking data....")
    discrepancy = 0
    for compound in all_compounds:
        if compound['nsites'] != sum(compound['unit_cell_formula'].values()):
            discrepancy+=1
    assert discrepancy == 0, str(discrepancy) + \
            " compounds have n_sites that don't match their formulas."
    return all_compounds