第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Python mixin/decorator/__metaclass__ 用于基類增強(qiáng)

Python mixin/decorator/__metaclass__ 用于基類增強(qiáng)

回首憶惘然 2021-06-10 13:08:40
我正在為 Django REST API 實(shí)現(xiàn)一個(gè)內(nèi)容感知緩存系統(tǒng)。我想開發(fā)一個(gè)可以添加到現(xiàn)有視圖的組件,該組件將通過檢查緩存并在未命中時(shí)回退到基類行為來修改基類的行為。基本上,我有這樣的事情:class Base:   def get(self, request, *args, **kwargs):       ....       return Responseclass AnotherBase:   def get(self, request, *args, **kwargs):       ....        return Responseclass Derived(Base):    passclass OtherDerived(AnotherBase):    pass我最初的想法是做一些類似的事情class Cacheable:    def get(self, request, *args, **kwargs):       cache_key = self.get_cache_key(request)       base_get = #.... and this is the problem       return cache.get(cache_key, base_get(request, *args, **kwargs))    def get_cache_key(self, request):       # .... do stuffclass Derived(Cacheable, Base):    passclass AnotherDerived(Cacheable, AnotherBase):    pass很明顯這不起作用,因?yàn)槲也恢廊绾?,或者是否可能,或者是否建議從 mixin 訪問兄弟超類。我的目標(biāo)是一種實(shí)現(xiàn),它允許我在不觸及現(xiàn)有類的內(nèi)部結(jié)構(gòu)的情況下向現(xiàn)有視圖添加緩存行為。給定一個(gè)視圖類, C, st C.get(request, *args, **kwargs) -> Response,是否有一個(gè)函數(shù), F, stF(C).get(...在回退到 之前是否進(jìn)行緩存檢查C.get?在這個(gè)準(zhǔn)正式的表示法中,我們會(huì)說向類定義中最左邊的父類添加一個(gè) mixin 算作一個(gè)函數(shù)。使用方法裝飾器是否更合適?或者類裝飾器如何工作?然后我__metaclass__在研究這個(gè)時(shí)看到了參考資料,但我不清楚這種方法是什么樣的。這是 Python 3.6
查看完整描述

3 回答

?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

簡單的例子:


def Base:


    def _get_data(self):

        # get the data eg from database

        return self._get_data_native()


    def get(self, request, *args, **kwargs):

        return Response(self._get_data())


def Cacheable(Base):


    def _get_data(self):

        # get from cache ...

        result = ...

        if result is None:

            # or from base ...

            result = ...


        return result


def Derived(Cacheable):


    def _get_data_native(self):

        # get the data eg from database

        ...

通過從 Cacheable 繼承,您可以在此處包含緩存,因?yàn)樵诖颂巁get_data被覆蓋。


對于這個(gè)問題,如果您只想在一個(gè)地方添加緩存,則不需要元類或裝飾器。


當(dāng)然,裝飾器可用于以更通用的方式包含緩存。


查看完整回答
反對 回復(fù) 2021-06-15
?
慕村225694

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊

答案是一個(gè)裝飾器和一些Django特定的庫。


from django.utils.decorators import method_decorator

from django.core.cache import cache


def cached_get(cache_key_func=None):

    """

    Decorator to be applied via django.utils.decorators.method_decorator

    Implements content-aware cache fetching by decorating the "get" method

    on a django View

    :param cache_key_func: a function of fn(request, *args, **kwargs) --> String

    which determines the cache key for the request

    """

    def decorator(func):

        def cached_func(request, *args, **kwargs):

            assert cache_key_func is not None, "cache_key_function is required"

            key = cache_key_func(request, *args, **kwargs)

            result = cache.get(key)

            if result is None:

                return func(request, *args, **kwargs)

            return Response(result)

        return cached_func

    return decorator


@method_decorator(cached_get(cache_key_func=get_cache_key), name="get")

class SomeView(BaseView):

    ...


def get_cache_key(request):

    # do arbitrary processing on request, the following is the na?ve melody

    key =  urllib.urlencode(request.query_params)

    return key 

因此,解決方案是使用 Django 的內(nèi)置method_decorator函數(shù),將其第一個(gè)參數(shù)裝飾器應(yīng)用于裝飾類的方法,該方法由第二個(gè)參數(shù)name, to命名method_decorator。我定義了一個(gè)高階函數(shù),cached_get,它接受另一個(gè)函數(shù)作為它的參數(shù),并返回一個(gè)柯里化函數(shù)(閉包,所謂的)。通過調(diào)用這個(gè)函數(shù)get_cache_key(而不是,請注意,調(diào)用該函數(shù)),我有一個(gè)裝飾器,它將應(yīng)用于 .get 方法SomeView。


裝飾器本身是一個(gè)簡單的 Python 裝飾器——在這個(gè)應(yīng)用程序中,它是cached_func,而原始的、未裝飾的get方法是func。因此,cached_func內(nèi)容替換SomeView.get,所以當(dāng)SomeView.get被調(diào)用時(shí),它首先檢查緩存,但回落到未命中未修飾方法。


我希望這種方法能夠在通用適用性與內(nèi)容感知密鑰派生之間取得平衡。


查看完整回答
反對 回復(fù) 2021-06-15
?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊

我的兩分錢:

  1. 你正在走進(jìn)這里晦澀難懂的領(lǐng)域。熟悉所有相關(guān)概念,嘗試一些,然后再?zèng)Q定。

  2. 是一個(gè)關(guān)于元類的很好的教程。

  3. 這里有一個(gè)關(guān)于裝飾器。

  4. 我絕不隸屬于該網(wǎng)站。


查看完整回答
反對 回復(fù) 2021-06-15
  • 3 回答
  • 0 關(guān)注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號