自己封裝的 Python 常用工具庫(prestool)
阿新 • • 發佈:2022-03-15
一、安裝
需Python 版本建議 3.7 以上
pip install --upgrade prestool
二、常用工具
from prestool.Tool import Tool
tool = Tool()
1、隨機資料
tool.random_name() # 隨機姓名 tool.random_phone() # 隨機手機號 tool.random_ssn() # 隨機身份證 tool.random_string(16) # 隨機位數的字串 tool.random_number(8) # 隨機位數的數字 tool.random_ua() # 隨機UA tool.random_ua('chrome') # 隨機UA-Chrome tool.random_ua('firefox') # 隨機UA-Firefox tool.random_ua('ie') # 隨機UA-IE tool.random_ua('opera') # 隨機UA-opera tool.random_ua('safari') # 隨機UA-safari
2、編碼解碼
tool.url_encode('編碼前的url地址') # 編碼
tool.url_decode('解碼前的url地址') # 解碼
tool.base_64_encode('編碼前的字串') # base64編碼
3、加密相關
tool.to_md5('原始字串')
tool.to_hmac_256('原始字串', '加密key')
tool.to_sha_256('原始字串')
4、傳送訊息
# 釘釘 tool.ding_talk_token = '釘釘機器人token' tool.ding_talk_sign_key = '釘釘機器人簽名key' tool.send_ding_talk_msg('訊息內容') # 企業微信 tool.qy_wechat_token = '企業微信機器人token' tool.send_qy_wechat_msg('訊息內容') # 郵件 tool.mail_from_user_host = '發件地址host' tool.mail_from_user = '發件人郵箱號' tool.mail_from_user_pwd = '發件人pwd' tool.send_mail_msg(to_user='收件人郵箱地址(列表)', title='郵件標題', content='郵件內容')
5、時間相關
tool.time_stamp() # 秒級時間戳10位
tool.time_stamp('ms') # 毫秒級時間戳13位
tool.get_now_time() # 獲取當前時間 20201206000000
tool.get_now_time('-') # 獲取當前時間 2020-12-06 00:00:00
tool.date_to_time_stamp('2012-01-01 00:00:00') # 時間字串轉為時間戳
tool.time_stamp_to_date(1732312234) # 時間戳轉為時間字串
6、格式轉換
tool.json_dumps({"test": "python字典"}) # 字典轉json tool.json_loads('{"test": "python字典"}') # json轉字典 tool.xml_to_dict('<xml><data>字串</data></xml>') # xml轉成python字典 tool.dict_to_xml({"test": "python字典"}) # python字典 轉成xml
7、http 請求
tool.http_client(url='', data={}, method='GET') # get請求
tool.http_client(url='', data={}, method='POST') # post請求
tool.get_cookies(url='介面地址', data={}, method='GET')
tool.get_cookies(url='介面地址', data={}, method='POST')
tool.trans_data_to_url(url='介面地址', data={}) # 把引數拼接到url上
8、dubbo 介面
tool.dubbo_args('引數1', '引數2', '引數3') # dubbo介面引數
tool.invoke_dubbo('地址', '埠', '服務API名', '介面方法名', 'dubbo介面引數') # 請求dubbo介面
9、其他
tool.logger('日誌資訊')
tool.get_ip_by_url('url地址') # 獲取ip
三、資料庫語句(MySQL)
1、生成資料庫 sql 語句
from prestool.PresMySql import SqlStr
sql = SqlStr()
2、查詢語句
# target 不傳時,為全部欄位,即 *,where={'key':'value'}
sql.select_sql_str(table='table1', where={'id': 1, 'name': '張三'})
select * from table1 where id = 1 and name = '張三';
# target=[i1,i2,i3] 時,為相應欄位
sql.select_sql_str(table='table1', target=['a', 'b', 'c'], where={'id': 1, 'name': '張三'})
select a, b, c from table1 where 1=1 and id=1 and name='張三';
# limit=10 limit='10,1000' 為篩選限制欄位
sql.select_sql_str(table='table1', target=['a', 'b', 'c'], order={'age': 'desc', 'score': 'desc'}, limit=20)
select a, b, c from table1 where 1=1 order by age desc, score desc limit 20;
# where 條件中有的欄位為 null 或者 not null 時
sql.select_sql_str(table='table1', target=['a', 'b', 'c'], where={'id': 1, 'name': 'null', 'age': not None})
select a, b, c from table1 where 1=1 and id=1 and name is null and age is not null;
# 支援排序語句
sql.select_sql_str(table='table1', target=['a', 'b', 'c'], order={'age': 'desc', 'score': 'desc'})
select a, b, c from table1 order by age desc, score desc;
# 支援查詢 in 語句
sql.select_sql_str(table='table1', target=['a', 'b', 'c'], select_in={'orders': [123121312, 123123445, 213123]})
select a, b, c from table1 where 1=1 and orders in (123121312, 123123445, 213123);
# 支援 like 語句
sql.select_sql_str(table='table1', target=['a', 'b', 'c'], like={'name': '%光', 'address': "中國%"})
select a, b, c from table1 where 1=1 and name like '%光' and address like '中國%';
# 支援 between 語句
sql.select_sql_str(table='table1', target=['a', 'b', 'c'], between={'age': (10, 20), 'year': (2021, 2022)})
select a, b, c from table1 where 1=1 and age between 10 and 20 and year between 2021 and 2022;
# 支援大於、小於語句
sql.select_sql_str(table='table1', target=['a', 'b', 'c'],
compare={'age': {'>': 10, '<': 20}, 'year': {'>=': '2021'}})
select a, b, c from table1 where 1=1 and age > 10 and age < 20 and year >= 2021;
# 更新語句
target 為要更新的資料,為字典結構 (支援大於、小於語句、between 語句、like 語句、in 語句)
sql.update_sql_str(table='table1', target={'name': '李四', 'age': 15}, where={'id': 1, 'name': '張三'})
update table1
set name='李四',
age=15
where id = 1
and name = '張三';
# 刪除資料
支援大於、小於語句、between 語句、like 語句、in 語句
sql.delete_sql_str(table='table1', where={'id': 1, 'name': '張三'})
delete
from table1
where id = 1
and name = '張三';
# 插入資料
sql.insert_sql_str(table='table1', target={'id': 1, 'name': '張三'})
insert into table1 (id, name)
values (1, '張三');
2、執行資料庫語句
from prestool.PresMySql import PresMySql
pres = PresMySql()
# 初始化資料庫資訊
pres.mysql_host = ''
pres.mysql_port = 3306
pres.mysql_user = ''
pres.mysql_pwd = ''
pres.mysql_db_name = ''
pres.mysql_charset = 'utf8mb4'
執行相應語句即可,執行的方法引數等同於第三節所述的 sql 語句,如
pres.to_query(table='table1', target=['a', 'b', 'c'], between={'age': (10, 20), 'year': (2021, 2022)})
pres.to_insert(table='table1', target={'id': 1, 'name': '張三'})
pres.to_delete(table='table1', where={'id': 1, 'name': '張三'})
pres.to_update(table='table1', target={'name': '李四', 'age': 15}, where={'id': 1, 'name': '張三'})