How to use Python annotations to realize detailed interpretation of cached data
This article mainly introduces "how to use Python annotations to achieve detailed explanation of cached data". In daily operation, I believe many people have doubts about how to use Python annotations to achieve detailed explanation of cached data. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to use Python annotations to achieve detailed explanation of cached data." Next, please follow the editor to study!
Background
Every time you load the data, you have to re-Load. You want to develop the cache mechanism by adding annotations, and you don't have to write code for each cache.
Disadvantages: currently only one return value is supported, although it can be turned into a dictionary, it has met personal needs and has no motivation to change (dog head).
Take it and use it
Create a new file Cache.py
Class Cache: def _ init__ (self, cache_path='.', nocache=False): self.cache_path = cache_path self.cache = not nocache def _ call__ (self, func): @ wraps (func) def wrapper (* args, * * kwargs): s = f'{func.__code__.co_filename}. {func.__name__}' '.join (list (args [1:]) + [f'{k} = {v} 'for k V in kwargs.items ()]) md5 = hashlib.md5 () md5.update (s.encode ('utf-8')) cache_file = f' {self.cache_path} / {md5.hexdigest ()}'if self.cache and os.path.exists (cache_file): print ('Loading from cache') return pickle.load (open (cache_file) 'rb') else: if not os.path.exists (self.cache_path): os.makedirs (self.cache_path) data = func (* args, * * kwargs) pickle.dump (data, file=open (cache_file) 'wb')) print (f'Dump finished {cache_file}') return data return wrapperfrom. Cache import Cache@Cache (root_path, nocache=True) def load_data (self, inpath): return 'Wulasides Practical process
For the first time, simply inherit the parent class
Class Cache (object): def _ _ init__ (self, cache_path=None): self.cache_path = cache_path if cache_path else'. Self.cache_path = f'{self.cache_path} / cache' self.data = self.load_cache () def load_cache (self): if os.path.exists (self.cache_path): print ('Loading from cache') return pickle.load (open (self.cache_path) 'rb')) else: return None def save_cache (self): pickle.dump (self.data, file=open (self.cache_path,' wb')) print (f'Dump finished {self.cache_path}') class Filter4Analyzer (Cache): def _ init__ (self, rootpath Datapath): super (). _ _ init__ (rootpath) self.root_path = rootpath if self.data is None: self.data = self.load_data (datapath) self.save_cache ()
Just inherit the Cache class, but there are many limitations, for example, you can only specify a parameter to be cache, for example, you have to write saved code in Filter4Analyzer.
Next, python nested decorators to improve the problem
From functools import wrapsimport hashlibdef cached (cache_path): def wrapperper (func): @ wraps (func) def wrapper (* args, * * kwargs): s = f'{func.__code__.co_filename}. {func.__name__}'+', '.join (args [1:]) s + =', '.join (list (args [1:])) + [f' {k} = {v} 'for k V in kwargs.items ()]) md5 = hashlib.md5 () md5.update (s.encode ('utf-8')) cache_file = f' {cache_path} / {md5.hexdigest ()}'if cache_path else'. / cache' if os.path.exists (cache_file): print ('Loading from cache') return pickle.load (open (cache_file) 'rb') else: if not os.path.exists (cache_path): os.makedirs (cache_path) data = func (* args, * * kwargs) pickle.dump (data, file=open (cache_file) 'wb')) print (f'Dump finished {cache_file}') return data return wrapper return wrapperperclass Tester: @ cached (cache_path='./workpath_test') def test (self, data_path): return ['hiahia'] simplifies the code class Cache: def _ _ init__ (self, cache_path='.') through decorator classes Nocache=False): self.cache_path = cache_path self.cache = not nocache def _ _ call__ (self, func): @ wraps (func) def wrapper (* args, * * kwargs): s = f'{func.__code__.co_filename}. {func.__name__}'s + =' '.join (list (args [1:]) + [f'{k} = {v} 'for k V in kwargs.items ()]) md5 = hashlib.md5 () md5.update (s.encode ('utf-8')) cache_file = f' {self.cache_path} / {md5.hexdigest ()}'if self.cache and os.path.exists (cache_file): print ('Loading from cache') return pickle.load (open (cache_file) 'rb') else: if not os.path.exists (self.cache_path): os.makedirs (self.cache_path) data = func (* args, * * kwargs) pickle.dump (data, file=open (cache_file) 'wb')) print (f'Dump finished {cache_file}') return data return wrapper so far The study on "how to use Python annotations to achieve detailed interpretation of cached data" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!