Exercises#
For these exercises, write classes to be used as decorators and make sure to provide all the functionality required. (Some exercises mention class properties. If you don’t know what they are, skip those bullet points and implement only the others.)
cache#
Write a class decorator cache that provides a cache for the function that is decorated.
Assume the function decorated only accepts positional arguments that are hashable.
The class decorator must provide the following functionality:
the attribute
cacheshould let the user inspect the cache;the attribute
hitsshould count how many times the cache was hit (that is, values from the cache were used);the attribute
missesshould count how many times the cache did not contain the value that was required; andthe method
clear_cacheshould clear the cache and reset the attributeshits/misses.
profiler#
Write a class decorator profiler that keeps track of the time spent inside the decorated function.
The class decorator must provide the following functionality:
the attribute
callsshould count how many times the function was called;the attribute
total_timeshould keep track of how much time, in total, was spent inside the function;the property
avg_timeshould return the average time spent on the function per call; andthe method
resetshould reset everything.
history#
Write a class decorator history(maxsize) that keeps track of the history of values returned by a given function.
The class decorator must provide the following functionality:
the argument
maxsizedetermines the maximum history size, which should never be exceeded;the attribute
historyshould let the user inspect the history as an iterable (doesn’t have to be a list);the property
most_recentshould return the most recent return value from that function;the property
oldestshould return the oldest item in the history; andthe method
clear_historyshould clear the function history.