欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 注解方式實(shí)現(xiàn)緩存數(shù)據(jù)詳解

 更新時(shí)間:2021年10月19日 14:35:22   作者:liuxing93619  
這篇文章主要介紹了Python 注解方式實(shí)現(xiàn)緩存數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

背景

每次加載數(shù)據(jù)都要重新Load,想通過加入的注解方式開發(fā)緩存機(jī)制,每次緩存不用寫代碼了

缺點(diǎn):目前僅支持一個(gè)返回值,雖然能弄成字典,但是已經(jīng)滿足個(gè)人需求,沒動(dòng)力改(狗頭)。

拿來即用

新建文件 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__}'
            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
from .Cache import Cache
@Cache(root_path, nocache=True)
def load_data(self, inpath):
    return 'Wula~a~a~!'

實(shí)踐過程

第一次,來個(gè)簡單的繼承父類

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()

只要繼承Cache類就可以啦,但是有很多局限,例如只能指定某個(gè)參數(shù)被cache,例如還得在Filter4Analyzer里面寫保存的代碼。

下一步,python嵌套裝飾器來改善這個(gè)問題

from functools import wraps
import hashlib
def 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 wrapperper
class Tester:
    @cached(cache_path='./workpath_test')
    def test(self, data_path):
        return ['hiahia']

通過裝飾器類簡化代碼

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__}'
            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

參考:

Python 函數(shù)裝飾器

Python函數(shù)屬性和PyCodeObject

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • 如何使用python提取字符串的中英文(正則判斷)

    如何使用python提取字符串的中英文(正則判斷)

    這篇文章主要給大家介紹了關(guān)于如何使用python提取字符串中英文的相關(guān)資料,主要通過正則re中的sub函數(shù)、findall函數(shù)以及compile函數(shù)判斷來實(shí)現(xiàn),需要的朋友可以參考下
    2021-05-05
  • 簡單介紹django提供的加密算法

    簡單介紹django提供的加密算法

    在本篇文章里小編給大家分享的是關(guān)于django提供的加密算法的相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。
    2019-12-12
  • Python fileinput模塊使用實(shí)例

    Python fileinput模塊使用實(shí)例

    這篇文章主要介紹了Python fileinput模塊使用實(shí)例,本文講解了典型用法、基本格式、默認(rèn)格式、常用函數(shù)和常見例子等內(nèi)容,需要的朋友可以參考下
    2015-06-06
  • 在Python 3中實(shí)現(xiàn)類型檢查器的簡單方法

    在Python 3中實(shí)現(xiàn)類型檢查器的簡單方法

    這篇文章主要介紹了在Python 3中實(shí)現(xiàn)類型檢查器的簡單方法,包括對函數(shù)注解這個(gè)新特性的介紹,需要的朋友可以參考下
    2015-07-07
  • Python調(diào)用.net動(dòng)態(tài)庫實(shí)現(xiàn)過程解析

    Python調(diào)用.net動(dòng)態(tài)庫實(shí)現(xiàn)過程解析

    這篇文章主要介紹了Python調(diào)用.net動(dòng)態(tài)庫實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Python 解析xml文件的示例

    Python 解析xml文件的示例

    這篇文章主要介紹了Python 解析xml文件的示例,幫助大家更好的利用python處理文件,感興趣的朋友可以了解下
    2020-09-09
  • Pytorch技法之繼承Subset類完成自定義數(shù)據(jù)拆分

    Pytorch技法之繼承Subset類完成自定義數(shù)據(jù)拆分

    這篇文章主要介紹了Pytorch技法之繼承Subset類完成自定義數(shù)據(jù)拆分,下文我們介紹一些下面是加載內(nèi)置訓(xùn)練數(shù)據(jù)集的常見操作,需要的小伙伴可以參考一下
    2022-02-02
  • Python中values()函數(shù)用法簡單示例

    Python中values()函數(shù)用法簡單示例

    這篇文章主要給大家介紹了關(guān)于Python中values()函數(shù)用法的相關(guān)資料,python內(nèi)置的values()函數(shù)返回一個(gè)字典中所有的值,文中給出了代碼示例,需要的朋友可以參考下
    2023-09-09
  • Python算法輸出1-9數(shù)組形成的結(jié)果為100的所有運(yùn)算式

    Python算法輸出1-9數(shù)組形成的結(jié)果為100的所有運(yùn)算式

    這篇文章主要介紹了Python算法輸出1-9數(shù)組形成的結(jié)果為100的所有運(yùn)算式,然后介紹了另外一個(gè)相關(guān)實(shí)例,具體內(nèi)容請參閱正文,需要的朋友可以參考下。
    2017-11-11
  • python右對齊的實(shí)例方法

    python右對齊的實(shí)例方法

    在本篇文章里小編給大家整理的是關(guān)于python右對齊的實(shí)例方法,有需要的朋友們可以學(xué)習(xí)參考下。
    2020-07-07

最新評(píng)論