python上下文管理器協(xié)議的實現(xiàn)
前言
在上下文管理器協(xié)議的過程中,涉及到兩個魔術(shù)方法__enter__方法 和 __exit__方法
- 在python中所有實現(xiàn)了上下文管理器協(xié)議的對象 都可以用使用with操作
- with啟動了對象的上下文管理器
上下文管理器協(xié)議:
- __enter__方法: 進入enter方法返回的結(jié)果被as后面的變量接收
- exit: 退出with中所有的語句執(zhí)行完畢執(zhí)行 執(zhí)行 exit
實現(xiàn)一個簡單的文件操作來看下上下文管理器協(xié)議:
class MyOpen: ?? ?# 實例化 ? ? def __init__(self, filename, mode, encoding): ? ? ? ? self.filename = filename ? ? ? ? self.mode = mode ? ? ? ? self.encoding = encoding ? ? def __enter__(self): ? ? ? ? print("---enter---方法") ? ? ? ? # 執(zhí)行文件打開操作 ? ? ? ? self.f = open(self.filename, self.mode, encoding=self.encoding) ? ? ? ? return self.f ? ? def __exit__(self, exc_type, exc_val, exc_tb): ? ? ? ? """ ? ? ? ? :param exc_type: 異常類型 ? ? ? ? :param exc_val: 異常信息 ? ? ? ? :param exc_tb: 異常溯源對象 ? ? ? ? :return: ? ? ? ? """ ? ? ? ? print('----enter---') ? ? ? ? self.f.close() with MyOpen('hr.txt', 'w', encoding='utf-8') as f: ? ? print(f.write('當前打開了文件,寫入了數(shù)據(jù):23323232'))
用pymysql實現(xiàn)一個操作數(shù)據(jù)庫的類,實現(xiàn)上下文管理器協(xié)議,實現(xiàn)退出上下文時,自動關(guān)閉游標,斷開連接
todo:版本1
# todo:版本1: class mysql_db(object): ? ? #實例化屬性 ? ? def __init__(self):
1.連接數(shù)據(jù)庫
? ? ? ? self.cou = pymysql.connect( ? ? ? ? ? ? host= "數(shù)據(jù)庫主機地址", ? ? ? ? ? ? ? port= 端口, ? ? ? ? ? ? ? user="登錄數(shù)據(jù)庫的賬號", ? ? ? ? ? ? ? password="登錄數(shù)據(jù)庫的密碼",? ? ? ? ? ? ? database="數(shù)據(jù)庫名稱", ? ? ? ? ? ? ? charset='utf8', ? ? 編碼格式 ? ? ? ? ? ? cursorclass=pymysql.cursors.DictCursor ? ? 將默認的元組格式轉(zhuǎn)換成字典格式輸出 ? ? ? ? )?
2.創(chuàng)建游標
? ? ? ? self.cur = self.cou.cursor() ? ? def __enter__(self): ? ? ? ? return self.cur ? ? ? 返回cur對象 ? ? def __exit__(self, exc_type, exc_val, exc_tb): ? ? ? ? """ ? ? ? ? :param exc_type: 異常類型 ? ? ? ? :param exc_val: 異常信息 ? ? ? ? :param exc_tb: 異常溯源對象 ? ? ? ? :return: ? ? ? ? """ ? ? ? ? #關(guān)閉游標 ? ? ? ? self.cur.close() ? ? ? ?# 關(guān)閉數(shù)據(jù)庫連接 ? ? ? ? self.cou.close() def Obtain_one_date(): ? ? with mysql_db() as db: ? ? ? ? db.execute('select * from t_customer LIMIT 4') ? ? 使用execute方法進行查詢語句 ? ? ? ? content = db.fetchone() ?返回一條數(shù)據(jù)的查詢的結(jié)果 ? ? ? ? print(content) # 函數(shù)調(diào)用 Obtain_one_date()
todo:版本2
sql = 'select * from t_customer LIMIT 4' def mysql_db1(**kwargs): ? ? return pymysql.connect(host=kwargs.get('host', 'xxxx'), ? ? ? ? ? ? ? ? ? ? ? ? ? ?user=kwargs.get("user",'xxxx'), ? ? ? ? ? ? ? ? ? ? ? ? ? ?passwd=kwargs.get("passwd",'xxxx'), ? ? ? ? ? ? ? ? ? ? ? ? ? ?database=kwargs.get("database",'xxxx'), ? ? ? ? ? ? ? ? ? ? ? ? ? ?port=kwargs.get('port', xxxx), ? ? ? ? ? ? ? ? ? ? ? ? ? ?charset=kwargs.get('charset', 'utf8'))
1.創(chuàng)建數(shù)據(jù)庫連接對象
cou = mysql_db1()
2.創(chuàng)建游標
with cou.cursor() as cu: ? ? cu.execute(sql) ? ? ?使用execute方法進行查詢語句 ? ? commt = cu.fetchone() ? ? 返回一條數(shù)據(jù)的查詢的結(jié)果 ? ? print(commt) # 函數(shù)調(diào)用 mysql_db1()
到此這篇關(guān)于python上下文管理器協(xié)議的實現(xiàn)的文章就介紹到這了,更多相關(guān)python上下文管理器 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解model.train()和model.eval()兩種模式的原理與用法
這篇文章主要介紹了詳解model.train()和model.eval()兩種模式的原理與用法,相信很多沒有經(jīng)驗的人對此束手無策,那么看完這篇文章一定會對你有所幫助2023-03-03關(guān)于tf.reverse_sequence()簡述
今天小編就為大家分享一篇關(guān)于tf.reverse_sequence()簡述,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01keras load model時出現(xiàn)Missing Layer錯誤的解決方式
這篇文章主要介紹了keras load model時出現(xiàn)Missing Layer錯誤的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06