Exercise 2: Pickling Data

Perform an expensive calculation and save its results.

We'll cover the following

Problem statement

Let’s pretend that it takes the foobar(some_big_data) function a very long time to calculate the results, and the results are always the same. Call the function only if it hasn’t been called before and pickle the results for the future!

Task

Verify that the pickled file, cache.p, exists by trying to open it and initialize a variable with its content. If the file doesn’t exist, call the function, foobar(), calculate the result, pickle it as cache.p, and assign it to the same variable.

Note: Your code will be slow when you run it for the first time. It’ll run much faster after that.

Get hands-on with 1200+ tech skills courses.