import frills.debugging as fd
import numpy as np
import pickle
import ast


# to recover all items that were iteratively saved to a pkl file.
def smart_load_pickle(filepath):
    with open(filepath, "rb") as file: 
        results = []
        try:
            while True:
                results.append(pickle.load(file))
        except EOFError:
            pass
    return results



def interpret_array_str(array_str, use_type=float):
    report_error = lambda: fd.printx("Cannot interpret array string")
    if isinstance(array_str, str):
        try:
            return [use_type(x) for x in ast.literal_eval(array_str)]
        except:
            try:
                return np.array([
                    use_type(x) for x in array_str.strip("[]").split()
                    ])
            except:
                report_error()       
    else:
        report_error() 
