一挤聘、安裝(python版本建議3.6以上)
pip install --upgrade prestool
二渊涝、常用工具
from prestool.Tool import Tool
tool = Tool()
隨機(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
編碼解碼
tool.url_encode('編碼前的url地址') # 編碼
tool.url_decode('解碼前的url地址') # 解碼
tool.base_64_encode('編碼前的字符串') # base64編碼
加密相關(guān)
tool.to_md5('原始字符串')
tool.to_hmac_256('原始字符串', '加密key')
tool.to_sha_256('原始字符串')
發(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ā)件人密碼'
tool.send_mail_msg(to_user='收件人郵箱地址(列表)', title='郵件標(biāo)題', content='郵件內(nèi)容')
時(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í)間字符串
格式轉(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字典
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上
dubbo接口
tool.dubbo_args('參數(shù)1', '參數(shù)2', '參數(shù)3') # dubbo接口參數(shù)
tool.invoke_dubbo('地址', '端口', '服務(wù)API名', '接口方法名', 'dubbo接口參數(shù)') # 請(qǐng)求dubbo接口
其他
tool.logger('日志信息')
tool.get_ip_by_url('url地址') # 獲取ip
# 上傳到pypi相關(guān)
python setup.py bdist_wheel --universal
twine upload dist/*
三辆飘、數(shù)據(jù)庫(kù)語(yǔ)句(MySQL)
from prestool.PresMySql import SqlStr
sql = SqlStr()
查詢語(yǔ)句
- target不傳時(shí)咕晋,為全部字段趁矾,即*
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': '張三'})
sql.select_sql_str(table='table1', target=['a', 'b', 'c'], order={'age': 'desc', 'score': 'desc'})
select a, b, c
from table1
where id = 1
and name = '張三';
select a, b, c
from table1
order by age desc, score desc;
更新語(yǔ)句
- target為要更新的數(shù)據(jù)槽袄,為字典結(jié)構(gòu)
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ù)
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, '張三');