python中的functools模組

來源:互聯網
上載者:User

標籤:rds   訪問   urllib   最好   graph   數字   inner   調用   sorted   

functools模組可以作用於所有的可以被調用的對象,包括函數 定義了__call__方法的類等

  1 functools.cmp_to_key(func)

    將比較函數(接受兩個參數,通過比較兩個參數的大小返回負值,0,或者正數)轉換為key function(返回一個值用來比較或者排序的可調用對象),

    例如: sorted(iterable, functools.cmp_to_key(locale.strcoll))

def cmp1(n1, n2):    return n1 - n2a = [1, 6, 2, 9]print(sorted(a, key=functools.cmp_to_key(cmp1)))

  2 @functools.lru_cache(maxsize=128, typed=False)

    首先這是一個裝飾器

    其次,介紹一下LRU演算法:

      LRU是最常用的緩衝演算法,全稱叫“Least Recently Used”,顧名思義,就是在緩衝miss 並且緩衝空間已滿的時候,將最久沒訪問過的資料刪除從而騰出空間。

    然後,說一下這個裝飾器的兩個參數的含義:

      maxsize: 表示緩衝大小,如果設定為None,表示不限制,設定為0表示不啟用緩衝機制

      typed:如果設定為True,則該裝飾器所裝飾的函數的參數即使值相等(比如說 3 == 3.0 ),但類型不同(一個是整型一個是浮點),也會被區分對待為不同的緩衝

    然後,說明一下這個裝飾器對所裝飾的函數的要求,

      1 函數的參數接收的值必須是不可變對象,像字串,數字,元組等都屬於此列 

      2 其次函數返回的對象最好也是不可變對象,當然這一點沒有硬性要求,但是道理大家懂。

    來一個栗子:

@functools.lru_cache(2526)def get_resource(page):    url = "https://urls_does_not_contain_pornographic_informations/%s" % page    try:        with urllib.request.urlopen(url) as s:            return s.read()    except urllib.error.HTTPError:        return ‘Not Found‘for i in range(1, 2526):    pep = get_resource(i)    print(pep)

  3 @functools.total_ordering

    首先這是一個類裝飾器,這個類裝飾器要求它所定義的類中必須定義:

      1  小於__lt__(), 小於等於__le__(),大於__gt__(),大於等於__ge__()中的一個

      2  還要定義等於__eq__()方法。

    只要我們按照要求定義了這些方法,該裝飾器就會為我們完成其餘的比較排序方法 。

  4 functools.partial(func, *args, **keywords)

     類似於這樣:

def abc(a, b):    print a + bdef partial(func, *args, **kwargs):    args_li = list(args)    def inner(*nargs, **nkwargs):        args_li.extend(nargs)        kwargs.update(nkwargs)        return func(*args_li, **kwargs)    return innernew_abc = partial(abc, 2)new_abc(4)

實際上就是給某個函數加上幾個固定參數然後返回一個新的函數,對於多個對象更新相同的值來說可以用到。比如:

from functools import partialclass Test(object):    def __init__(self):        self.name = "lala"        self.age = 20def _update_attr(obj, update_dic):    map(lambda item: setattr(obj, item[0], item[1]), update_dic.iteritems())update_attr = partial(_update_attr, update_dic={"name": "mncu", "age": 18})test_obj_list = [Test() for i in xrange(20)]map(update_attr, test_obj_list)for test_obj in test_obj_list:    print test_obj.name, test_obj.age

   5 class functools.partialmethod(func, *args, **keywords)

    作用類似於上面的partial函數,但這個方法作用於類的方法,返回的是方法而不是函數。

>>> class Cell(object):...     def __init__(self):...         self._alive = False...     @property...     def alive(self):...         return self._alive...     def set_state(self, state):...         self._alive = bool(state)...     set_alive = partialmethod(set_state, True)...     set_dead = partialmethod(set_state, False)...>>> c = Cell()>>> c.aliveFalse>>> c.set_alive()>>> c.aliveTrue

   6 functool.update_wrapper(wrapper, wrapped[, assigned][, updated])

      functools.wraps(wrapped[, assigned][, updated])

    在python中,當一個函數被裝飾器裝飾後,這個函數名字對應的函數對象實際上是那個裝飾器函數,也就是該函數名對應的的__name__以及__doc__實際上已經改變了,這就導致很難調試。而update_wrapper以及wraps就是用來解決這個問題。

#!/usr/bin/env python# encoding: utf-8def wrap(func):    def call_it(*args, **kwargs):        """wrap func: call_it"""        print ‘before call‘        return func(*args, **kwargs)    return call_it@wrapdef hello():    """say hello"""    print ‘hello world‘from functools import update_wrapperdef wrap2(func):    def call_it(*args, **kwargs):        """wrap func: call_it2"""        print ‘before call‘        return func(*args, **kwargs)    return update_wrapper(call_it, func)@wrap2def hello2():    """test hello"""    print ‘hello world2‘if __name__ == ‘__main__‘:    hello()    print hello.__name__    print hello.__doc__    print    hello2()    print hello2.__name__    print hello2.__doc__

結果:

  before call
  hello world
  call_it
  wrap func: call_it

  before call
  hello world2
  hello2
  test hello

from functools import wrapsdef wrap3(func):    @wraps(func)    def call_it(*args, **kwargs):        """wrap func: call_it2"""        print ‘before call‘        return func(*args, **kwargs)    return call_it@wrap3def hello3():    """test hello 3"""    print ‘hello world3‘

結果:

  before call
  hello world3
  hello3
  test hello 3

 

參考:

  https://blog.theerrorlog.com/simple-lru-cache-in-python-3.html,  Kay Zheng  

  http://www.wklken.me/posts/2013/08/18/python-extra-functools.html  WKLKEN 

 

python中的functools模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.