自己封装的 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>&#x5B57;&#x7B26;&#x4E32;</data></xml>')  # xml&#x8F6C;&#x6210;python&#x5B57;&#x5178;
tool.dict_to_xml({"test": "python&#x5B57;&#x5178;"})  # python&#x5B57;&#x5178; &#x8F6C;&#x6210;xml

7、http 请求

tool.http_client(url='', data={}, method='GET')  # get&#x8BF7;&#x6C42;
tool.http_client(url='', data={}, method='POST')  # post&#x8BF7;&#x6C42;

tool.get_cookies(url='&#x63A5;&#x53E3;&#x5730;&#x5740;', data={}, method='GET')
tool.get_cookies(url='&#x63A5;&#x53E3;&#x5730;&#x5740;', data={}, method='POST')

tool.trans_data_to_url(url='&#x63A5;&#x53E3;&#x5730;&#x5740;', data={})  # &#x628A;&#x53C2;&#x6570;&#x62FC;&#x63A5;&#x5230;url&#x4E0A;

8、dubbo 接口

tool.dubbo_args('&#x53C2;&#x6570;1', '&#x53C2;&#x6570;2', '&#x53C2;&#x6570;3')  # dubbo&#x63A5;&#x53E3;&#x53C2;&#x6570;
tool.invoke_dubbo('&#x5730;&#x5740;', '&#x7AEF;&#x53E3;', '&#x670D;&#x52A1;API&#x540D;', '&#x63A5;&#x53E3;&#x65B9;&#x6CD5;&#x540D;', 'dubbo&#x63A5;&#x53E3;&#x53C2;&#x6570;')  # &#x8BF7;&#x6C42;dubbo&#x63A5;&#x53E3;

9、其他

tool.logger('&#x65E5;&#x5FD7;&#x4FE1;&#x606F;')
tool.get_ip_by_url('url&#x5730;&#x5740;')  # &#x83B7;&#x53D6;ip

1、生成数据库 sql 语句

from prestool.PresMySql import SqlStr
sql = SqlStr()

2、查询语句

# target &#x4E0D;&#x4F20;&#x65F6;&#xFF0C;&#x4E3A;&#x5168;&#x90E8;&#x5B57;&#x6BB5;&#xFF0C;&#x5373; *&#xFF0C;where={'key':'value'}
sql.select_sql_str(table='table1', where={'id': 1, 'name': '&#x5F20;&#x4E09;'})
select * from table1 where id = 1 and name = '&#x5F20;&#x4E09;';

# target=[i1,i2,i3] &#x65F6;&#xFF0C;&#x4E3A;&#x76F8;&#x5E94;&#x5B57;&#x6BB5;
sql.select_sql_str(table='table1', target=['a', 'b', 'c'], where={'id': 1, 'name': '&#x5F20;&#x4E09;'})
select a, b, c from table1 where 1=1 and id=1 and name='&#x5F20;&#x4E09;';

# limit=10 limit='10,1000' &#x4E3A;&#x7B5B;&#x9009;&#x9650;&#x5236;&#x5B57;&#x6BB5;
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 &#x6761;&#x4EF6;&#x4E2D;&#x6709;&#x7684;&#x5B57;&#x6BB5;&#x4E3A; null &#x6216;&#x8005; not null &#x65F6;
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;

# &#x652F;&#x6301;&#x6392;&#x5E8F;&#x8BED;&#x53E5;
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;

# &#x652F;&#x6301;&#x67E5;&#x8BE2; in &#x8BED;&#x53E5;
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);

# &#x652F;&#x6301; like &#x8BED;&#x53E5;
sql.select_sql_str(table='table1', target=['a', 'b', 'c'], like={'name': '%&#x5149;', 'address': "&#x4E2D;&#x56FD;%"})
select a, b, c from table1 where 1=1 and name like '%&#x5149;' and address like '&#x4E2D;&#x56FD;%';

# &#x652F;&#x6301; between &#x8BED;&#x53E5;
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;

# &#x652F;&#x6301;&#x5927;&#x4E8E;&#x3001;&#x5C0F;&#x4E8E;&#x8BED;&#x53E5;
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;

# &#x66F4;&#x65B0;&#x8BED;&#x53E5;
target &#x4E3A;&#x8981;&#x66F4;&#x65B0;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4E3A;&#x5B57;&#x5178;&#x7ED3;&#x6784; (&#x652F;&#x6301;&#x5927;&#x4E8E;&#x3001;&#x5C0F;&#x4E8E;&#x8BED;&#x53E5;&#x3001;between &#x8BED;&#x53E5;&#x3001;like &#x8BED;&#x53E5;&#x3001;in &#x8BED;&#x53E5;)
sql.update_sql_str(table='table1', target={'name': '&#x674E;&#x56DB;', 'age': 15}, where={'id': 1, 'name': '&#x5F20;&#x4E09;'})
update table1
set name='&#x674E;&#x56DB;',
    age=15
where id = 1
  and name = '&#x5F20;&#x4E09;';

# &#x5220;&#x9664;&#x6570;&#x636E;
&#x652F;&#x6301;&#x5927;&#x4E8E;&#x3001;&#x5C0F;&#x4E8E;&#x8BED;&#x53E5;&#x3001;between &#x8BED;&#x53E5;&#x3001;like &#x8BED;&#x53E5;&#x3001;in &#x8BED;&#x53E5;
sql.delete_sql_str(table='table1', where={'id': 1, 'name': '&#x5F20;&#x4E09;'})
delete
from table1
where id = 1
  and name = '&#x5F20;&#x4E09;';

# &#x63D2;&#x5165;&#x6570;&#x636E;
sql.insert_sql_str(table='table1', target={'id': 1, 'name': '&#x5F20;&#x4E09;'})
insert into table1 (id, name)
values (1, '&#x5F20;&#x4E09;');
</':>

2、执行数据库语句

from prestool.PresMySql import PresMySql

pres = PresMySql()
# &#x521D;&#x59CB;&#x5316;&#x6570;&#x636E;&#x5E93;&#x4FE1;&#x606F;
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': '&#x5F20;&#x4E09;'})
pres.to_delete(table='table1', where={'id': 1, 'name': '&#x5F20;&#x4E09;'})
pres.to_update(table='table1', target={'name': '&#x674E;&#x56DB;', 'age': 15}, where={'id': 1, 'name': '&#x5F20;&#x4E09;'})

Original: https://www.cnblogs.com/jinjiangongzuoshi/p/16007144.html
Author: 狂师
Title: 自己封装的 Python 常用工具库(prestool)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/9929/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部