Python裝飾器怎麼運用

來源:互聯網
上載者:User

Python裝飾器怎麼運用

   這篇文章主要介紹了更為深入的Python的裝飾器的運用,Python的裝飾器是Python學習進階當中的重要知識點,需要的朋友可以參考下

  裝飾器在 python 中用的相當廣泛,如果你用過 python 的一些 web 架構,那麼一定對其中的 “ route() 裝飾器” 不陌生,今天咱們再看一個具體的案例。

  咱們來類比一個情境,需要你去抓去一個頁面,然後這個頁面有好多url也要分別去抓取,而進入這些子url後,還有資料要抓取。簡單點,我們就按照三層來看,那我們的代碼就是如下:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

def func_top(url):

data_dict= {}

 

#在頁面上擷取到子url

sub_urls = xxxx

 

data_list = []

for it in sub_urls:

data_list.append(func_sub(it))

 

data_dict['data'] = data_list

 

return data_dict

 

def func_sub(url):

data_dict= {}

 

#在頁面上擷取到子url

bottom_urls = xxxx

 

data_list = []

for it in bottom_urls:

data_list.append(func_bottom(it))

 

data_dict['data'] = data_list

 

return data_dict

 

def func_bottom(url):

#擷取資料

data = xxxx

return data

  func_top是上層頁面的處理函數,func_sub是子頁面的處理函數,func_bottom是最深層頁面的處理函數,func_top會在取到子頁面url後遍曆調用func_sub,func_sub也是同樣。

  如果正常情況下,這樣確實已經滿足需求了,但是偏偏這個你要抓取的網站可能極不穩定,經常連結不上,導致資料拿不到。

  於是這個時候你有兩個選擇:

  1.遇到錯誤就停止,之後重新從斷掉的位置開始重新跑

  2.遇到錯誤繼續,但是要在之後重新跑一遍,這個時候已經有的資料不希望再去網站拉一次,而只去拉沒有取到的資料

  對第一種方案基本無法實現,因為如果別人網站的url調整順序,那麼你記錄的位置就無效了。那麼只有第二種方案,說白了,就是要把已經拿到的資料cache下來,等需要的時候,直接從cache裡面取。

  OK,目標已經有了,怎麼實現呢?

  如果是在C++中的,這是個很麻煩的事情,而且寫出來的代碼必定醜陋無比,然而慶幸的是,我們用的是python,而python對函數有裝飾器。

  所以實現方案也就有了:

  定義一個裝飾器,如果之前取到資料,就直接取cache的資料;如果之前沒有取到,那麼就從網站拉取,並且存入cache中.

  代碼如下:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

import os

import hashlib

 

def deco_args_recent_cache(category='dumps'):

'''

裝飾器,返回最新cache的資料

'''

def deco_recent_cache(func):

def func_wrapper(*args, **kargs):

sig = _mk_cache_sig(*args, **kargs)

data = _get_recent_cache(category, func.__name__, sig)

if data is not None:

return data

 

data = func(*args, **kargs)

if data is not None:

_set_recent_cache(category, func.__name__, sig, data)

return data

 

return func_wrapper

 

return deco_recent_cache

 

def _mk_cache_sig(*args, **kargs):

'''

通過傳入參數,產生唯一標識

'''

src_data = repr(args) + repr(kargs)

m = hashlib.md5(src_data)

sig = m.hexdigest()

return sig

 

def _get_recent_cache(category, func_name, sig):

full_file_path = '%s/%s/%s' % (category, func_name, sig)

if os.path.isfile(full_file_path):

return eval(file(full_file_path,'r').read())

else:

return None

 

def _set_recent_cache(category, func_name, sig, data):

full_dir_path = '%s/%s' % (category, func_name)

if not os.path.isdir(full_dir_path):

os.makedirs(full_dir_path)

 

full_file_path = '%s/%s/%s' % (category, func_name, sig)

f = file(full_file_path, 'w+')

f.write(repr(data))

f.close()

  然後,我們只需要在每個func_top,func_sub,func_bottom都加上deco_args_recent_cache這個裝飾器即可~~

  搞定!這樣做最大的好處在於,因為top,sub,bottom,每一層都會dump資料,所以比如某個sub層資料dump之後,是根本不會走到他所對應的bottom層的,減少了大量的開銷!

  OK,就這樣~ 人生苦短,我用python!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.