日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python上下文管理器協(xié)議的實現(xiàn)_python

作者:henry_rhy ? 更新時間: 2022-08-16 編程語言

前言

在上下文管理器協(xié)議的過程中,涉及到兩個魔術方法__enter__方法 和 __exit__方法

  • 在python中所有實現(xiàn)了上下文管理器協(xié)議的對象 都可以用使用with操作
  • with啟動了對象的上下文管理器

上下文管理器協(xié)議:

  • __enter__方法: 進入enter方法返回的結果被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)退出上下文時,自動關閉游標,斷開連接

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 ? ? 將默認的元組格式轉換成字典格式輸出
? ? ? ? )? 

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:
? ? ? ? """
? ? ? ? #關閉游標
? ? ? ? self.cur.close()
? ? ? ?# 關閉數(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ù)的查詢的結果
? ? ? ? print(content)

# 函數(shù)調用
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ù)的查詢的結果
? ? print(commt)

# 函數(shù)調用
mysql_db1()

原文鏈接:https://blog.csdn.net/qq_40236497/article/details/125415201

欄目分類
最近更新