11import  inspect 
22from  functools  import  wraps 
33
4+ from  osbot_utils .utils .Misc  import  str_md5 
5+ 
6+ #from osbot_utils.utils.Dev import pprint 
7+ 
8+ CACHE_ON_SELF_KEY_PREFIX  =  'cache_on_self' 
9+ CACHE_ON_SELF_TYPES       =  [int , float , bytearray , bytes , bool ,
10+                             complex , str ]
11+ 
12+ # not supported for now (need to understand side effect, ) 
13+ # - set, dict, range,, tuple, list      : cloud have inner objects 
14+ # - memoryview                          : returns unique memory location value 
15+ 
416
517def  cache_on_self (function ):
618    """ 
@@ -12,8 +24,46 @@ def wrapper(*args, **kwargs):
1224            raise  Exception ("In Method_Wrappers.cache_on_self could not find self" )
1325
1426        self  =  args [0 ]                                              # get self 
15-         cache_id  =  f'osbot_cache_return_value__ { function . __name__ } '   # generate cache_id 
27+         cache_id  =  cache_on_self__get_cache_in_key ( function ,  args ,  kwargs ) 
1628        if  hasattr (self , cache_id ) is  False :                        # check if return_value has been set 
1729            setattr (self , cache_id , function (* args , ** kwargs ))      # invoke function and capture the return value 
1830        return  getattr (self , cache_id )                              # return the return value 
19-     return  wrapper 
31+     return  wrapper 
32+ 
33+ def  cache_on_self__args_to_str (args ):
34+     args_values_as_str  =  '' 
35+     if  args :
36+         for  arg  in  args :
37+             if  type (arg ) in  CACHE_ON_SELF_TYPES :
38+                 args_values_as_str  +=  str (arg )
39+     return  args_values_as_str 
40+ 
41+ def  cache_on_self__kwargs_to_str (kwargs ):
42+     kwargs_values_as_str  =  '' 
43+     if  kwargs :
44+         for  key ,value  in  kwargs .items ():
45+             if  type (value ) in  CACHE_ON_SELF_TYPES :
46+                 kwargs_values_as_str  +=  f'{ key }  :{ value }  |' 
47+     return  kwargs_values_as_str 
48+ 
49+ def  cache_on_self__get_cache_in_key (function , args = None , kwargs = None ):
50+         key_name    =  function .__name__ 
51+         args_md5    =  '' 
52+         kwargs_md5  =  '' 
53+         args_values_as_str    =  cache_on_self__args_to_str (args )
54+         kwargs_values_as_str  =  cache_on_self__kwargs_to_str (kwargs )
55+         if  args_values_as_str :
56+             args_md5  =  str_md5 (args_values_as_str )
57+         if  kwargs_values_as_str :
58+             kwargs_md5  =  str_md5 (kwargs_values_as_str )
59+         return  f'{ CACHE_ON_SELF_KEY_PREFIX }  _{ key_name }  _{ args_md5 }  _{ kwargs_md5 }  ' 
60+ 
61+         # class_name = self_obj.__class__.__name__ 
62+         # 
63+         # function_name = function_obj.__name__ 
64+         # if params: 
65+         #     params_as_string = '_'.join(str(x) for x in params).replace('/',' ') 
66+         #     params_md5       = str_md5(params_as_string) 
67+         #     return f'{class_name}_{function_name}_{params_md5}.gz' 
68+         # else: 
69+         #     return f'{class_name}_{function_name}.gz' 
0 commit comments