網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言:
又到每日分享Python小技巧的時(shí)光了,今天給大家分享的是Python接口常用封裝函數(shù)。相信對(duì)于封裝,大家都不陌生吧,今天就
用四個(gè)小案例來(lái)給大家展示,廢話不多說(shuō),直接上代碼:
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)中沒(méi)有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ù)庫(kù)的函數(shù)
import pymysql
# 獲取連接方法
def get_db_conn():
? ? conn = pymysql.connect(host='地址',
? ? ? ? ? ? ? ? ? ? ? ? ? ?port=000, # 端口號(hào)
? ? ? ? ? ? ? ? ? ? ? ? ? ?user='name',
? ? ? ? ? ? ? ? ? ? ? ? ? ?passwd='23456',
? ? ? ? ? ? ? ? ? ? ? ? ? ?db='3454', ?# 庫(kù)名
? ? ? ? ? ? ? ? ? ? ? ? ? ?cursorclass=pymysql.cursors.DictCursor)
? ? return conn
# 封裝數(shù)據(jù)庫(kù)查詢單條操作
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ù)庫(kù)查詢所有操作
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ù)庫(kù)操作
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ù)庫(kù)新增所有操作
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ù),大家可以收藏起來(lái)以備不時(shí)之需。今天的分享到這里就結(jié)束了,更多的內(nèi)容需要關(guān)注才能及時(shí)
原文鏈接:https://blog.csdn.net/xff123456_/article/details/124343883
相關(guān)推薦
- 2023-11-22 Linux的vim命令如何使用
- 2022-04-09 python中異常的傳播詳解_python
- 2022-10-20 詳解Python中的?type()函數(shù)_python
- 2022-10-20 Swift泛型Generics淺析講解_Swift
- 2022-07-21 基于Spring Boot項(xiàng)目構(gòu)建流水線
- 2024-03-02 前端directus對(duì)接單點(diǎn)登錄
- 2023-11-18 數(shù)據(jù)處理使用Python提取String、字符串中的數(shù)字
- 2022-11-26 React?DnD如何處理拖拽詳解_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支