jeudi 6 août 2015

Design for application able to calculate a function or load the result from a file

I have many files that I need to process with certain time-consuming functions f_1() ... f_n(). Because of this, I tought to save the fk_processed_output in a file every time a function f_k() ends its work, in order to avoid recalculation in future work sessions.

The program has to be able to choose dynamically if it need to calculate the result or to load it from HDD (if it was calculated in a previous time for the given input file).

What may be a good design to implement this?

I thought to implement every f_k{} like this:

def f_k():
    if (fk_processed_output.exist()):
        load_it_from_file()
    else:
        output = do_some_stuff()
    save_to_file(output)

but I don't like it very much because of the repetion of the if/else pattern for each function.

Any better or clever idea? Any other hint? Does exist any library for such a purpose?

Aucun commentaire:

Enregistrer un commentaire