pip install --upgrade prestool
from prestool.Tool import Tool
tool = Tool()
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
tool.url_encode('编码前的url地址') # 编码
tool.url_decode('解码前的url地址') # 解码
tool.base_64_encode('编码前的字符串') # base64编码
tool.to_md5('原始字符串')
tool.to_hmac_256('原始字符串', '加密key')
tool.to_sha_256('原始字符串')
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='邮件内容')
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) # 时间戳转为时间字符串
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
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上
tool.dubbo_args('参数1', '参数2', '参数3') # dubbo接口参数
tool.invoke_dubbo('地址', '端口', '服务API名', '接口方法名', 'dubbo接口参数') # 请求dubbo接口
tool.logger('日志信息')
tool.get_ip_by_url('url地址') # 获取ip
from prestool.PresMySql import SqlStr
sql = SqlStr()
sql.select_sql_str(table='table1', where={'id': 1, 'name': '张三'})
select * from table1 where id = 1 and name = '张三';
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='张三';
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;
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;
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);
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 '中国%';
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;
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 = '张三';
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, '张三');
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'
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': '张三'})
本文分享自 Python数据分析实例 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!