自己封装的 Python 常用工具库(prestool)
一、安装
需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': '张三'})
自己封装的 Python 常用工具库(prestool)的更多相关文章
- python常用工具库介绍
Numpy:科学计算 HOME: http://www.numpy.org/ NumPy is the fundamental package for scientific computing wi ...
- Python常用的库简单介绍一下
Python常用的库简单介绍一下fuzzywuzzy ,字符串模糊匹配. esmre ,正则表达式的加速器. colorama 主要用来给文本添加各种颜色,并且非常简单易用. Prettytable ...
- PYTHON常用五大库
python常用五大库 Numpy Numpy 是python科学计算的基础包,本书大部分内容都基于numpy以及构建于其上的库.其功能有: 快速高效的多维数组对象ndarray 用于对数组执行元素级 ...
- python常用三方库 - openpyxl
目录 python常用三方库 - openpyxl 读取Excel文件 写入Excel文件 python常用三方库 - openpyxl openpyxl是一个第三方库, 可以处理xlsx格式的Exc ...
- python常用第三方库(转载)
Python标准库与第三方库详解(转载) 转载地址: http://www.codeweblog.com/python%e6%a0%87%e5%87%86%e5%ba%93%e4%b8%8e%e7%a ...
- python常用函数库收集。
学习过Python都知道python中有很多库.python本身就是万能胶水,众多强大的库/模块正是它的优势. 收集一些Python常用的函数库,方便大家选择要学习的库,也方便自己学习收集,熟悉运用好 ...
- 吐血整理!Python常用第三方库,码住!!!
Python作为一种编程语言近年来越来越受欢迎,它为什么这么火? 其中一个重要原因就是因为Python的库丰富--Python语言提供超过15万个第三方库,Python库之间广泛联系.逐层封装.几 ...
- javaScript常用工具库
对应于百度前端技术学院2015年春季的课程2相关内容 https://github.com/baidu-ife/ife/tree/master/2015_spring/task/task0002 ht ...
- python常用20库
python核心库和统计 简述 1. Requests.最着名的http库由kenneth reitz编写.这是每个python开发人员必备的. 2. Scrapy.如果您参与webscraping, ...
- python常用删除库的方法
本文记于初学py的时候,两年后补发. python常用库的安装方法一般有几种,比如: 1.编译过的exe包,直接无脑下一步就可以了. 2.pip install 库名,快速安装.自动匹配最新版本. 3 ...
随机推荐
- #1016:Prime Ring Problem(经典DFS)
原题链接 题意:很容易理解,就是让你输出满足相邻的相加是素数的序列(注意不要重复) 思路就是深搜思想把每种情况遍历一次 代码实现: #include<iostream> #include& ...
- AtCoder Beginner Contest 170 (D~F题,D筛法,E multiset使用,F Dijkstra算法改进)
题目链接:Here ABC水题, D. Not Divisible 看了题解才想到,可以用 Sieve of Eratosthenes,因为 \(A_i\) 最大才 \(10^6\) 但有注意的点 1 ...
- vue中class样式与内联样式
(1):style使用 <div class="score" :style="{ color: colorComputed(item.status) }" ...
- Vue项目实现导入导出Excel表格功能
前提:在我的项目中我使用的是ElementUi前端UI框架,用到的是里面的Upload上传组件. 第一步:需要安装三个依赖 npm install -S file-saver xlsx (这里其实安 ...
- C# 加解密
1. Md5 /// <summary> /// 不可逆加密 /// 1 防止被篡改 /// 2 防止明文存储 /// 3 防止抵赖,数字签名 /// </summary> ...
- Java应用架构演变史
垂直应用架构 也叫单体架构.以 MVC 的垂直架构举例,MVC 架构通常分为 3 层,展示层.控制层.模型层.通常基于 MVC 架构开发的应用代码会打成一个 war 包,部署在 Tomcat 等 We ...
- spring boot 中默认最大线程连接数,线程池数配置查看
本文为博主原创,转载请注明出处: 可以查看 AbstractEndpoint 源码中的常量的定义: public abstract class AbstractEndpoint<S, U> ...
- Nacos源码 (3) 注册中心
本文将从一个服务注册示例入手,通过阅读客户端.服务端源码,分析服务注册.服务发现原理. 使用的2.0.2的版本. 客户端 创建NacosNamingService对象 NacosNamingServi ...
- 上下文中找不到org.springframework.boot.web.servlet.server.ServletWebServerFactory bean
1.问题 报错如下: Description: Web application could not be started as there was no org.springframework.boo ...
- PolarD&N2023秋季个人挑战赛—Misc全解
签个到叭 题目信息 压缩包带密码,放到010查看PK头错误,改回去.. 解压后得到 562+5Yiw5Lmf5LiN6IO96L+Z5LmI566A5Y2V5ZGA77yM5b+r5p2l55yL55 ...