99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python with語句上下文管理器兩種實現(xiàn)方法分析
Python with語句上下文管理器兩種實現(xiàn)方法分析
2018-03-31
收藏

Python with語句上下文管理器兩種實現(xiàn)方法分析

本文實例講述了Python with語句上下文管理器。分享給大家供大家參考,具體如下:

在編程中會經(jīng)常碰到這種情況:有一個特殊的語句塊,在執(zhí)行這個語句塊之前需要先執(zhí)行一些準備動作;當語句塊執(zhí)行完成后,需要繼續(xù)執(zhí)行一些收尾動作。例如,文件讀寫后需要關閉,數(shù)據(jù)庫讀寫完畢需要關閉連接,資源的加鎖和解鎖等情況。

對于這種情況python提供了上下文管理器(Context Manager)的概念,可以通過上下文管理器來定義/控制代碼塊執(zhí)行前的準備動作,以及執(zhí)行后的收尾動作。

一、為何使用上下文管理器

1、不使用上下文管理器的情況

通過try...finally語句執(zhí)行異常處理和關閉句柄的動作。

logger = open("log.txt", "w")
try:
  logger.write('Hello ')
  logger.write('World')
finally:
  logger.close()
print logger.closed


2、使用上下文管理器

默認文件Python的內(nèi)置file類型是支持上下文管理協(xié)議的。
使用上下文管理器with使得依據(jù)精簡了很多。

with open("log.txt", "w") as logger:
  logger.write('Hello ')
  logger.write('World')
print logger.closed



二、實現(xiàn)上下文管理器實現(xiàn)上下文管理器有兩種方式實現(xiàn)。方法一:類實現(xiàn)__enter__和__exit__方法。方法二:contextlib模塊裝飾器和生成器實現(xiàn)。

下面我們通過兩種方法分別實現(xiàn)一個自定義的上下文管理器。

1、方法一:通過類實現(xiàn)__enter__和__exit__方法

class File(object):
 def __init__(self, file_name, method):
  self.file_obj = open(file_name, method)
 def __enter__(self):
  return self.file_obj
 def __exit__(self, type, value, traceback):
  self.file_obj.close()
with File('demo.txt', 'w') as opened_file:
 opened_file.write('Hola!')



實現(xiàn)__enter__和__exit__方法后,就能通過with語句進行上下文管理。

a、底層都發(fā)生了什么?

1、with語句先暫存了File類的__exit__方法,然后它調(diào)用File類的__enter__方法。
2、__enter__方法打開文件并返回給with語句,打開的文件句柄被傳遞給opened_file參數(shù)。
3、with語句調(diào)用之前暫存的__exit__方法,__exit__方法關閉了文件。

b、異常處理

關于異常處理,with語句會采取哪些步驟。

1. 它把異常的type,value和traceback傳遞給__exit__方法
2. 它讓__exit__方法來處理異常
3. 如果__exit__返回的是True,那么這個異常就被忽略。
4. 如果__exit__返回的是True以外的任何東西,那么這個異常將被with語句拋出。

異常拋出

#異常拋出,_exit__返回的是True以外的任何東西,那么這個異常將被with語句拋出
class File(object):
 def __init__(self, file_name, method):
  self.file_obj = open(file_name, method)
 def __enter__(self):
  return self.file_obj
 def __exit__(self, type, value, traceback):
  self.file_obj.close()
  print "type:",type
  print "value:",value
  print "traceback:",traceback
with File('demo.txt', 'w') as opened_file:
 opened_file.undefined_function('Hola!')
#output================================================
# type: <type 'exceptions.AttributeError'>
# value: 'file' object has no attribute 'undefined_function'
# traceback: <traceback object at 0x000000000262D9C8>
#  opened_file.undefined_function('Hola!')
# AttributeError: 'file' object has no attribute 'undefined_function'

異常忽略:
    
#異常忽略,__exit__返回的是True,那么這個異常就被忽略。
class File(object):
 def __init__(self, file_name, method):
  self.file_obj = open(file_name, method)
 def __enter__(self):
  return self.file_obj
 def __exit__(self, exception_type, exception_value, traceback):
  print("Exception has been handled")
  self.file_obj.close()
  return True
with File('demo.txt', 'w') as opened_file:
 opened_file.undefined_function('Hola!')
# output==================================
# Exception has been handled



2、方法二:contextlib模塊裝飾器和生成器實現(xiàn)

這種方式實現(xiàn)更優(yōu)雅,我個人更喜歡這種方式。

yield之前的代碼由__enter__方法執(zhí)行,yield之后的代碼由__exit__方法執(zhí)行。本質(zhì)上還是__enter__和__exit__方法。

# coding:utf-8
import contextlib
@contextlib.contextmanager
def myopen(filename, mode):
 f = open(filename, mode)
 try:
  yield f.readlines()
 except Exception as e:
  print e
 finally:
  f.close()
if __name__ == '__main__':
 with myopen(r'c:\ip2.txt', 'r') as f:
  for line in f:
   print line

3、with語句上多個下文關聯(lián)

直接通過一個with語句打開多個上下文,即可同時使用多個上下文變量,而不必需嵌套使用with語句。    
class File(object):
 def __init__(self, file_name, method):
  self.file_obj = open(file_name, method)
 def __enter__(self):
  return self.file_obj
 def __exit__(self, exception_type, exception_value, traceback):
  self.file_obj.close()
  return True
with File('demo.txt', 'w') as f1,File('demo.txt','w') as f2:
 print f1,f2
# Output============================# <open file 'demo.txt', mode 'w' at 0x000000000263D150> <open file 'demo.txt', mode 'w' at 0x000000000263D1E0>



數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }