<s> """<STR_LIT>""" <EOL> from functools import wraps <EOL> import hashlib <EOL> import logging <EOL> import random <EOL> import os <EOL> import string <EOL> import traceback <EOL> from joblib import numpy_pickle <EOL> CACHE_PATH = '<STR_LIT>' <EOL> if not os . path . exists ( CACHE_PATH ) : <EOL> os . mkdir ( CACHE_PATH ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> def default_cache_key ( * args , ** kwargs ) : <EOL> return str ( args ) + str ( sorted ( kwargs . items ( ) ) ) <EOL> class DontCache ( Exception ) : <EOL> pass <EOL> def cached ( cache_key = default_cache_key , cache_path = None ) : <EOL> def cached ( func ) : <EOL> @ wraps ( func ) <EOL> def wrapper ( * args , ** kwargs ) : <EOL> try : <EOL> key = str ( cache_key ( * args , ** kwargs ) ) . encode ( '<STR_LIT:ascii>' ) <EOL> except DontCache : <EOL> return func ( * args , ** kwargs ) <EOL> hashed_key = hashlib . sha1 ( key ) . hexdigest ( ) [ : <NUM_LIT:8> ] <EOL> filename = os . path . join ( <EOL> cache_path or CACHE_PATH , <EOL> '<STR_LIT>' . format ( <EOL> func . __module__ , func . __name__ , hashed_key ) ) <EOL> if os . path . exists ( filename ) : <EOL> filesize = os . path . getsize ( filename ) <EOL> size = "<STR_LIT>" % ( filesize / ( <NUM_LIT> * <NUM_LIT> ) ) <EOL> logger . debug ( "<STR_LIT>" . format ( filename , size ) ) <EOL> return numpy_pickle . load ( filename ) <EOL> else : <EOL> logger . debug ( "<STR_LIT>" . format ( filename ) ) <EOL> value = func ( * args , ** kwargs ) <EOL> tmp_filename = '<STR_LIT>' . format ( <EOL> filename , <EOL> '<STR_LIT>' . join ( random . sample ( string . ascii_letters , <NUM_LIT:4> ) ) , <EOL> ) <EOL> try : <EOL> numpy_pickle . dump ( value , tmp_filename , compress = <NUM_LIT:9> ) <EOL> os . rename ( tmp_filename , filename ) <EOL> except Exception : <EOL> logger . exception ( <EOL> "<STR_LIT>" . format ( <EOL> filename ) ) <EOL> return value <EOL> wrapper . uncached = func <EOL> return wrapper <EOL> return cached </s>
