一、安裝
需Python 版本建議 3.7 以上
pip install --upgrade prestool
二末秃、常用工具
from prestool.Tool import Tool
tool = Tool()
1概页、隨機(jī)數(shù)據(jù)
tool.random_name() # 隨機(jī)姓名
tool.random_phone() # 隨機(jī)手機(jī)號(hào)
tool.random_ssn() # 隨機(jī)身份證
tool.random_string(16) # 隨機(jī)位數(shù)的字符串
tool.random_number(8) # 隨機(jī)位數(shù)的數(shù)字
tool.random_ua() # 隨機(jī)UA
tool.random_ua('chrome') # 隨機(jī)UA-Chrome
tool.random_ua('firefox') # 隨機(jī)UA-Firefox
tool.random_ua('ie') # 隨機(jī)UA-IE
tool.random_ua('opera') # 隨機(jī)UA-opera
tool.random_ua('safari') # 隨機(jī)UA-safari
2、編碼解碼
tool.url_encode('編碼前的url地址') # 編碼
tool.url_decode('解碼前的url地址') # 解碼
tool.base_64_encode('編碼前的字符串') # base64編碼
3练慕、加密相關(guān)
tool.to_md5('原始字符串')
tool.to_hmac_256('原始字符串', '加密key')
tool.to_sha_256('原始字符串')
4绰沥、發(fā)送消息
# 釘釘
tool.ding_talk_token = '釘釘機(jī)器人token'
tool.ding_talk_sign_key = '釘釘機(jī)器人簽名key'
tool.send_ding_talk_msg('消息內(nèi)容')
# 企業(yè)微信
tool.qy_wechat_token = '企業(yè)微信機(jī)器人token'
tool.send_qy_wechat_msg('消息內(nèi)容')
# 郵件
tool.mail_from_user_host = '發(fā)件地址host'
tool.mail_from_user = '發(fā)件人郵箱號(hào)'
tool.mail_from_user_pwd = '發(fā)件人pwd'
tool.send_mail_msg(to_user='收件人郵箱地址(列表)', title='郵件標(biāo)題', content='郵件內(nèi)容')
5、時(shí)間相關(guān)
tool.time_stamp() # 秒級(jí)時(shí)間戳10位
tool.time_stamp('ms') # 毫秒級(jí)時(shí)間戳13位
tool.get_now_time() # 獲取當(dāng)前時(shí)間 20201206000000
tool.get_now_time('-') # 獲取當(dāng)前時(shí)間 2020-12-06 00:00:00
tool.date_to_time_stamp('2012-01-01 00:00:00') # 時(shí)間字符串轉(zhuǎn)為時(shí)間戳
tool.time_stamp_to_date(1732312234) # 時(shí)間戳轉(zhuǎn)為時(shí)間字符串
6贺待、格式轉(zhuǎn)換
tool.json_dumps({"test": "python字典"}) # 字典轉(zhuǎn)json
tool.json_loads('{"test": "python字典"}') # json轉(zhuǎn)字典
tool.xml_to_dict('<xml><data>字符串</data></xml>') # xml轉(zhuǎn)成python字典
tool.dict_to_xml({"test": "python字典"}) # python字典 轉(zhuǎn)成xml
7徽曲、http 請(qǐng)求
tool.http_client(url='', data={}, method='GET') # get請(qǐng)求
tool.http_client(url='', data={}, method='POST') # post請(qǐng)求
tool.get_cookies(url='接口地址', data={}, method='GET')
tool.get_cookies(url='接口地址', data={}, method='POST')
tool.trans_data_to_url(url='接口地址', data={}) # 把參數(shù)拼接到url上
8、dubbo 接口
tool.dubbo_args('參數(shù)1', '參數(shù)2', '參數(shù)3') # dubbo接口參數(shù)
tool.invoke_dubbo('地址', '端口', '服務(wù)API名', '接口方法名', 'dubbo接口參數(shù)') # 請(qǐng)求dubbo接口
9麸塞、其他
tool.logger('日志信息')
tool.get_ip_by_url('url地址') # 獲取ip
三秃臣、數(shù)據(jù)庫(kù)語(yǔ)句(MySQL)
1、生成數(shù)據(jù)庫(kù) sql 語(yǔ)句
from prestool.PresMySql import SqlStr
sql = SqlStr()
2哪工、查詢語(yǔ)句
# target 不傳時(shí)奥此,為全部字段,即 *雁比,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] 時(shí)稚虎,為相應(yīng)字段
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 時(shí)
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;
# 支持排序語(yǔ)句
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 語(yǔ)句
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 語(yǔ)句
sql.select_sql_str(table='table1', target=['a', 'b', 'c'], like={'name': '%光', 'address': "中國(guó)%"})
select a, b, c from table1 where 1=1 and name like '%光' and address like '中國(guó)%';
# 支持 between 語(yǔ)句
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;
# 支持大于、小于語(yǔ)句
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;
# 更新語(yǔ)句
target 為要更新的數(shù)據(jù)偎捎,為字典結(jié)構(gòu) (支持大于蠢终、小于語(yǔ)句、between 語(yǔ)句茴她、like 語(yǔ)句寻拂、in 語(yǔ)句)
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 = '張三';
# 刪除數(shù)據(jù)
支持大于、小于語(yǔ)句丈牢、between 語(yǔ)句祭钉、like 語(yǔ)句、in 語(yǔ)句
sql.delete_sql_str(table='table1', where={'id': 1, 'name': '張三'})
delete
from table1
where id = 1
and name = '張三';
# 插入數(shù)據(jù)
sql.insert_sql_str(table='table1', target={'id': 1, 'name': '張三'})
insert into table1 (id, name)
values (1, '張三');
2己沛、執(zhí)行數(shù)據(jù)庫(kù)語(yǔ)句
from prestool.PresMySql import PresMySql
pres = PresMySql()
# 初始化數(shù)據(jù)庫(kù)信息
pres.mysql_host = ''
pres.mysql_port = 3306
pres.mysql_user = ''
pres.mysql_pwd = ''
pres.mysql_db_name = ''
pres.mysql_charset = 'utf8mb4'
執(zhí)行相應(yīng)語(yǔ)句即可慌核,執(zhí)行的方法參數(shù)等同于第三節(jié)所述的 sql 語(yǔ)句距境,如
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': '張三'})