分享四個python接口常用封裝函數(shù)
前言:
又到每日分享Python小技巧的時光了,今天給大家分享的是Python接口常用封裝函數(shù)。相信對于封裝,大家都不陌生吧,今天就
用四個小案例來給大家展示,廢話不多說,直接上代碼:
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. 封裝車牌號的函數(shù)
def chepaihao(len='6'): ? ? char0 = '京津滬渝冀豫云遼黑湘皖魯新蘇浙贛鄂桂甘晉蒙陜吉閩贛粵青藏川寧瓊' ? ? char1 = 'ABCDEFGHJKLMNPQRSTUVWXYZ' ?# 車牌號中沒有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, # 端口號 ? ? ? ? ? ? ? ? ? ? ? ? ? ?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
最后:
這幾個都是比較常用的封裝函數(shù),大家可以收藏起來以備不時之需。今天的分享到這里就結(jié)束了,更多的內(nèi)容需要關(guān)注才能及時
到此這篇關(guān)于分享四個python接口常用封裝函數(shù)的文章就介紹到這了,更多相關(guān)python接口封裝函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 對輸入的數(shù)字進(jìn)行排序的方法
今天小編就為大家分享一篇Python 對輸入的數(shù)字進(jìn)行排序的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06解決django xadmin主題不顯示和只顯示bootstrap2的問題
這篇文章主要介紹了解決django xadmin主題不顯示和只顯示bootstrap2的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03python數(shù)據(jù)結(jié)構(gòu)之棧、隊列及雙端隊列
在上一章的學(xué)習(xí)中,我們主要學(xué)習(xí)了怎么去衡量一個算法的好壞,比較常見的方式是使用大O記法,就是所謂的時間復(fù)雜度,這一章節(jié)我來學(xué)習(xí)基本的數(shù)據(jù)結(jié)構(gòu),如棧、隊列和雙端隊列等等。感興趣的小伙伴可以參考一下2021-12-12使用Python開發(fā)個京東上搶口罩的小實例(僅作技術(shù)研究學(xué)習(xí)使用)
這篇文章主要介紹了使用Python開發(fā)個京東上搶口罩的小實例(僅作技術(shù)研究學(xué)習(xí)使用),需要的朋友可以參考下2020-03-03Pythont特殊語法filter,map,reduce,apply使用方法
這篇文章主要介紹了Pythont特殊語法filter,map,reduce,apply使用方法,需要的朋友可以參考下2016-02-02pytorch中函數(shù)tensor.numpy()的數(shù)據(jù)類型解析
這篇文章主要介紹了pytorch中函數(shù)tensor.numpy()的數(shù)據(jù)類型,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07