欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

分享四個(gè)python接口常用封裝函數(shù)

 更新時(shí)間:2022年04月24日 08:28:54   作者:螞蟻愛Python  
這篇文章主要給大家分享的是分享四個(gè)python接口常用封裝函數(shù),文章基于python的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下

前言:

又到每日分享Python小技巧的時(shí)光了,今天給大家分享的是Python接口常用封裝函數(shù)。相信對(duì)于封裝,大家都不陌生吧,今天就

用四個(gè)小案例來給大家展示,廢話不多說,直接上代碼:

1.封裝上傳圖片的函數(shù)

.def upload_image(pathName, pathRoute, pathType, keyName=None):
? ? '''
? ? :param pathName: ? 圖片名稱
? ? :param pathRoute: ?圖片路徑
? ? :param pathType: ? 圖片類型
? ? :param keyName: ? ?文件名稱
? ? :return:
? ? '''
? ? file = open(pathRoute, 'rb')
? ? files = {
? ? ? ? ? ? keyName: (pathName, file, pathType)
? ? }
? ? return files

2. 封裝車牌號(hào)的函數(shù)

def chepaihao(len='6'):
? ? char0 = '京津滬渝冀豫云遼黑湘皖魯新蘇浙贛鄂桂甘晉蒙陜吉閩贛粵青藏川寧瓊'
? ? char1 = 'ABCDEFGHJKLMNPQRSTUVWXYZ' ?# 車牌號(hào)中沒有I和O,可自行百度
? ? char2 = '1234567890ABCDEFGHJKLMNPQRSTUVWXYZ'
? ? char3 = '1234567890'
? ? len0 = len(char0) - 1
? ? len1 = len(char1) - 1
? ? len2 = len(char2) - 1
? ? len3 = len(char3) - 1
? ? # while True:
? ? code = ''
? ? index0 = random.randint(1,len0)
? ? index1 = random.randint(1, len1)
? ? code += char0[index0]
? ? code += char1[index1]
? ? code += ' '
? ? for i in ran## 標(biāo)題ge(1, 5):
? ? ? ? index2 = random.randint(1, len2)
? ? ? ? code += char2[index2]
? ? index3 = random.randint(1,len3)
? ? code += char3[index3]
? ? # test = re.match('^.\w.[A-Z]\d{4}$|^.\w.\d[A-Z]\d{3}$|^.\w.\d{2}[A-Z]\d{2}$|^.\w.\d{3}[A-Z]\d$|^.\w.\d{5}$',code)
? ? print(code)
? ? return code

3. 封裝生成UUid 函數(shù)

# 生成UUid
def uuid_():
? ? uid = uuid.uuid1()
? ? return uid.hex

4. 封裝連接數(shù)據(jù)庫的函數(shù)

import pymysql


# 獲取連接方法
def get_db_conn():
? ? conn = pymysql.connect(host='地址',
? ? ? ? ? ? ? ? ? ? ? ? ? ?port=000, # 端口號(hào)
? ? ? ? ? ? ? ? ? ? ? ? ? ?user='name',
? ? ? ? ? ? ? ? ? ? ? ? ? ?passwd='23456',
? ? ? ? ? ? ? ? ? ? ? ? ? ?db='3454', ?# 庫名
? ? ? ? ? ? ? ? ? ? ? ? ? ?cursorclass=pymysql.cursors.DictCursor)
? ? return conn

# 封裝數(shù)據(jù)庫查詢單條操作
def query_db(sql):
? ? conn = get_db_conn() ? ? ?
? ? cur = conn.cursor() ? ? ? ?
? ? cur.execute(sql) ? ? ? ? ??
? ? conn.commit()
? ? result = cur.fetchone() ? ?
? ? cur.close() ? ? ? ? ? ? ? ?
? ? conn.close() ? ? ? ? ? ? ??
? ? return result

# 封裝數(shù)據(jù)庫查詢所有操作
def query_all(sql):
? ? conn = get_db_conn() ? ? ??
? ? cur = conn.cursor() ? ? ? ?
? ? cur.execute(sql) ? ? ? ? ??
? ? conn.commit()
? ? result = cur.fetchall() ? ?
? ? cur.close() ? ? ? ? ? ? ? ?
? ? conn.close() ? ? ? ? ? ? ??
? ? return result

# 封裝更改數(shù)據(jù)庫操作
def change_db(sql):
? ? conn = get_db_conn() ?
? ? cur = conn.cursor() ?
? ? try:
? ? ? ? cur.execute(sql) ?
? ? ? ? conn.commit() ?
? ? except Exception as e:
? ? ? ? conn.rollback() ?
? ? finally:
? ? ? ? cur.close() ?
? ? ? ? conn.close() ?
# 封裝數(shù)據(jù)庫新增所有操作
def insert_into(sql):
? ? conn = get_db_conn() ? ? ?
? ? cur = conn.cursor() ? ? ? ?
? ? cur.execute(sql) ? ? ? ??
? ? conn.commit()
? ? result = cur.fetchall() ? ?
? ? conn.close() ? ? ? ? ? ? ?
? ? return result

最后:

這幾個(gè)都是比較常用的封裝函數(shù),大家可以收藏起來以備不時(shí)之需。今天的分享到這里就結(jié)束了,更多的內(nèi)容需要關(guān)注才能及時(shí)

到此這篇關(guān)于分享四個(gè)python接口常用封裝函數(shù)的文章就介紹到這了,更多相關(guān)python接口封裝函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論