<s> from __future__ import absolute_import , division , print_function <EOL> from . callbacks import Callback <EOL> from timeit import default_timer <EOL> from numbers import Number <EOL> import sys <EOL> overhead = sys . getsizeof ( <NUM_LIT> ) * <NUM_LIT:4> + sys . getsizeof ( ( ) ) * <NUM_LIT:4> <EOL> class Cache ( Callback ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cache , * args , ** kwargs ) : <EOL> try : <EOL> import cachey <EOL> except ImportError as ex : <EOL> raise ImportError ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( ex = str ( ex ) ) ) <EOL> self . _nbytes = cachey . nbytes <EOL> if isinstance ( cache , Number ) : <EOL> cache = cachey . Cache ( cache , * args , ** kwargs ) <EOL> else : <EOL> assert not args and not kwargs <EOL> self . cache = cache <EOL> self . starttimes = dict ( ) <EOL> def _start ( self , dsk ) : <EOL> self . durations = dict ( ) <EOL> overlap = set ( dsk ) & set ( self . cache . data ) <EOL> for key in overlap : <EOL> dsk [ key ] = self . cache . data [ key ] <EOL> def _pretask ( self , key , dsk , state ) : <EOL> self . starttimes [ key ] = default_timer ( ) <EOL> def _posttask ( self , key , value , dsk , state , id ) : <EOL> duration = default_timer ( ) - self . starttimes [ key ] <EOL> deps = state [ '<STR_LIT>' ] [ key ] <EOL> if deps : <EOL> duration += max ( self . durations . get ( k , <NUM_LIT:0> ) for k in deps ) <EOL> self . durations [ key ] = duration <EOL> nb = self . _nbytes ( value ) + overhead + sys . getsizeof ( key ) * <NUM_LIT:4> <EOL> self . cache . put ( key , value , cost = duration / nb / <NUM_LIT> , nbytes = nb ) <EOL> def _finish ( self , dsk , state , errored ) : <EOL> self . starttimes . clear ( ) <EOL> self . durations . clear ( ) </s>
