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

python裝飾器-限制函數(shù)調(diào)用次數(shù)的方法(10s調(diào)用一次)

 更新時(shí)間:2018年04月21日 09:30:34   作者:taobaohua000  
下面小編就為大家分享一篇python裝飾器-限制函數(shù)調(diào)用次數(shù)的方法(10s調(diào)用一次),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

這是博主最近一家大公司的面試題,寫一個(gè)裝飾器,限制函數(shù)每10s調(diào)用一次。當(dāng)時(shí)是筆試的,只寫了大概的代碼,回來(lái)后溫習(xí)了python裝飾器的基礎(chǔ)知識(shí),把代碼寫完了。決定寫篇博客記錄下。

裝飾器分為帶參數(shù)得裝飾器以及不帶參數(shù)得裝飾器。

#不帶參數(shù)的裝飾器
@dec1
@dec2
def func():
  ...
#這個(gè)函數(shù)聲明等價(jià)于
func = dec1(dec2(func))
#帶參數(shù)的裝飾器
@dec(some_args)
def func():
  ...
#這個(gè)函數(shù)聲明等價(jià)于
func = dec(some_args)(func)

不帶參數(shù)的裝飾器需要注意的一些細(xì)節(jié)

1. 關(guān)于裝飾器函數(shù)(decorator)本身

因此一個(gè)裝飾器一般對(duì)應(yīng)兩個(gè)函數(shù),一個(gè)是decorator函數(shù),用來(lái)進(jìn)行一些初始化操作處理,一個(gè)是decorated_func用來(lái)實(shí)現(xiàn)對(duì)被裝飾的函數(shù)func的額外處理。并且為了保持對(duì)func的引用,decorated_func一般作為decorator的內(nèi)部函數(shù)

def decorator(func):
  def decorator_func()
    func()
  return decorated_func

decorator函數(shù)只在函數(shù)聲明的時(shí)候被調(diào)用一次

裝飾器實(shí)際上是語(yǔ)法糖,在聲明函數(shù)之后就會(huì)被調(diào)用,產(chǎn)生decorated_func,并把func符號(hào)的引用替換為decorated_func。之后每次調(diào)用func函數(shù),實(shí)際調(diào)用的是decorated_func(這個(gè)很重要,裝飾之后,其實(shí)每次調(diào)用的是decorated_func)。

>>> def decorator(func):
...   def decorated_func():
...     func(1)
...   return decorated_func
... 
#聲明時(shí)就被調(diào)用
>>> @decorator
... def func(x):
...   print x
... 
decorator being called 
#使用func()函數(shù)實(shí)際上使用的是decorated_func函數(shù)
>>> func()
1
>>> func.__name__
'decorated_func'

如果要保證返回的decorated_func的函數(shù)名與func的函數(shù)名相同,應(yīng)當(dāng)在decorator函數(shù)返回decorated_func之前,加入decorated_func.name = func.name, 另外functools模塊提供了wraps裝飾器,可以完成這一動(dòng)作。

#@wraps(func)的操作相當(dāng)于
#在return decorated_func之前,執(zhí)行
#decorated_func.__name__ = func.__name__
#func作為裝飾器參數(shù)傳入, 
#decorated_func則作為wraps返回的函數(shù)的參數(shù)傳入
>>> def decorator(func):
...   @wraps(func)
...   def decorated_func():
...     func(1)
...   return decorated_func
... 
#聲明時(shí)就被調(diào)用
>>> @decorator
... def func(x):
...   print x
... 
decorator being called 
#使用func()函數(shù)實(shí)際上使用的是decorated_func函數(shù)
>>> func()
1
>>> func.__name__
'func'

decorator函數(shù)局部變量的妙用

因?yàn)閏losure的特性(詳見(jiàn)(1)部分閉包部分的詳解),decorator聲明的變量會(huì)被decorated_func.func_closure引用,所以調(diào)用了decorator方法結(jié)束之后,decorator方法的局部變量也不會(huì)被回收,因此可以用decorator方法的局部變量作為計(jì)數(shù)器,緩存等等。

值得注意的是,如果要改變變量的值,該變量一定要是可變對(duì)象,因此就算是計(jì)數(shù)器,也應(yīng)當(dāng)用列表來(lái)實(shí)現(xiàn)。并且聲明一次函數(shù)調(diào)用一次decorator函數(shù),所以不同函數(shù)的計(jì)數(shù)器之間互不沖突,例如:

#!/usr/bin/env python
#filename decorator.py
def decorator(func):
  #注意這里使用可變對(duì)象
  a = [0]
  def decorated_func(*args,**keyargs):
    func(*args, **keyargs)
    #因?yàn)殚]包是淺拷貝,如果是不可變對(duì)象,每次調(diào)用完成后符號(hào)都會(huì)被清空,導(dǎo)致錯(cuò)誤
    a[0] += 1
    print "%s have bing called %d times" % (func.__name__, a[0])
  return decorated_func
@decorator
def func(x):
  print x
@decorator
def theOtherFunc(x):
  print x

下面我們開始寫代碼:

#coding=UTF-8
#!/usr/bin/env python
#filename decorator.py
import time
from functools import wraps
def decorator(func):
  "cache for function result, which is immutable with fixed arguments"
  print "initial cache for %s" % func.__name__
  cache = {}
  @wraps(func)
  def decorated_func(*args,**kwargs):
    # 函數(shù)的名稱作為key
    key = func.__name__
    result = None
    #判斷是否存在緩存
    if key in cache.keys():
      (result, updateTime) = cache[key]
      #過(guò)期時(shí)間固定為10秒
      if time.time() -updateTime < 10:
        print "limit call 10s", key
        result = updateTime
      else :
        print "cache expired !!! can call "
        result = None
    else:
      print "no cache for ", key
    #如果過(guò)期,或則沒(méi)有緩存調(diào)用方法
    if result is None:
      result = func(*args, **kwargs)
      cache[key] = (result, time.time())
    return result
  return decorated_func
@decorator
def func(x):
  print 'call func'

隨便測(cè)試了下,基本沒(méi)有問(wèn)題。

>>> from decorator import func
initial cache for func
>>> func(1)
no cache for func
call func
>>> func(1)
limit call 10s func
1488082913.239092
>>> func(1)
cache expired !!! can call
call func
>>> func(1)
limit call 10s func
1488082923.298204
>>> func(1)
cache expired !!! can call
call func
>>> func(1)
limit call 10s func
1488082935.165979
>>> func(1)
limit call 10s func
1488082935.165979

以上這篇python裝飾器-限制函數(shù)調(diào)用次數(shù)的方法(10s調(diào)用一次)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python操作Excel把數(shù)據(jù)分給sheet

    Python操作Excel把數(shù)據(jù)分給sheet

    這篇文章主要介紹了Python操作Excel把數(shù)據(jù)分給sheet,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Python traceback模塊獲取異常信息的使用

    Python traceback模塊獲取異常信息的使用

    Python的traceback模塊提供了多種方法來(lái)獲取和展示異常的堆棧信息,本文主要介紹了Python traceback模塊獲取異常信息的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • Flask框架URL管理操作示例【基于@app.route】

    Flask框架URL管理操作示例【基于@app.route】

    這篇文章主要介紹了Flask框架URL管理操作,結(jié)合實(shí)例形式分析了@app.route進(jìn)行URL控制的相關(guān)操作技巧,需要的朋友可以參考下
    2018-07-07
  • 淺談pandas中shift和diff函數(shù)關(guān)系

    淺談pandas中shift和diff函數(shù)關(guān)系

    下面小編就為大家分享一篇淺談pandas中shift和diff函數(shù)關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • python實(shí)現(xiàn)文件名批量替換和內(nèi)容替換

    python實(shí)現(xiàn)文件名批量替換和內(nèi)容替換

    這篇文章主要介紹了python實(shí)現(xiàn)文件名批量替換和內(nèi)容替換,第一個(gè)例子可以指定文件類型,需要的朋友可以參考下
    2014-03-03
  • python 判斷三個(gè)數(shù)字中的最大值實(shí)例代碼

    python 判斷三個(gè)數(shù)字中的最大值實(shí)例代碼

    這篇文章主要介紹了python 判斷三個(gè)數(shù)字中的最大值,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-07-07
  • python字符串中的單雙引

    python字符串中的單雙引

    下面小編就為大家?guī)?lái)一篇python字符串中的單雙引。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • python實(shí)現(xiàn)哈希表

    python實(shí)現(xiàn)哈希表

    介紹一個(gè)用python實(shí)現(xiàn)的哈希表,處理沖突的方法是開放地址法,沖突表達(dá)式為Hi=(H(key)+1)mod m,m為表長(zhǎng)。遲點(diǎn)再實(shí)現(xiàn)更難的拉鏈法
    2014-02-02
  • Python Django框架防御CSRF攻擊的方法分析

    Python Django框架防御CSRF攻擊的方法分析

    這篇文章主要介紹了Python Django框架防御CSRF攻擊的方法,結(jié)合實(shí)例形式分析了Python Django框架防御CSRF攻擊的原理、配置方法與使用技巧,需要的朋友可以參考下
    2019-10-10
  • django使用channels實(shí)現(xiàn)通信的示例

    django使用channels實(shí)現(xiàn)通信的示例

    這篇文章主要介紹了django使用channels實(shí)現(xiàn)通信的示例,幫助大家更好的理解和學(xué)習(xí)django框架,感興趣的朋友可以了解下
    2020-10-10

最新評(píng)論