def _filter(mols, expression, soft_fail=0):
    """Filter molecule by a generic expression, such as `mol.logp > 1`."""
    out = []
    for mol in mols:
        if isinstance(expression, list):
            fail = 0
            for e in expression:
                if not eval(e):
                    fail += 1
                if fail > soft_fail:
                    break
            if fail <= soft_fail:
                out.append(mol)
        else:
            if eval(expression):
                out.append(mol)
    return out