筱团Blog筱团のBlog

python 常用第三方模块

筱团·2022-08-10 17:33·220 次阅读

python 常用第三方模块

Prerequisite

参考文章:廖雪峰
参考博客:python libnum库安装使用方法

包含:Pillow、chardet、psutil、libnum、arrow、tinydb

Pillow#

Pillow 功能太多,我只记录了制作验证码图片的功能(注意:要下载对应的字体)

Copy
from PIL import Image, ImageDraw, ImageFont, ImageFilter import random # 随机字母: def rndChar(): return chr(random.randint(65, 90)) # 随机颜色1: def rndColor(): return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) # 随机颜色2: def rndColor2(): return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)) # 240 x 60: width = 60 * 4 height = 60 image = Image.new('RGB', (width, height), (255, 255, 255)) # 创建Font对象: font = ImageFont.truetype('Averia Libre Bold.ttf', 36) # 创建Draw对象: draw = ImageDraw.Draw(image) # 填充每个像素: for x in range(width): for y in range(height): draw.point((x, y), fill=rndColor()) # 输出文字: for t in range(4): draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2()) # 模糊: image = image.filter(ImageFilter.BLUR) image.save('code.jpg', 'jpeg')

chardet#

chardet 的作用是自动检测对象的编码

Copy
import chardet a = chardet.detect(b'Hello, world!') # {'encoding': 'ascii', 'confidence': 1.0, 'language': ''} a = chardet.detect('离离原上草,一岁一枯荣'.encode('gbk')) # {'encoding': 'GB2312', 'confidence': 0.7407407407407407, 'language': 'Chinese'} a = chardet.detect('离离原上草,一岁一枯荣'.encode('utf-8')) # {'encoding': 'utf-8', 'confidence': 0.99, 'language': ''} a = chardet.detect('最新の主要ニュース'.encode('euc-jp')) # {'encoding': 'EUC-JP', 'confidence': 0.99, 'language': 'Japanese'}

psutil#

psutil 的作用是监控系统运行的状态,比如能打印 CPU 信息、内存信息、磁盘信息、网络信息、进程信息等

详细去看廖雪峰的文章

libnum#

Copy
import libnum # 字符串转十进制 s = "flag{pcat}" print(libnum.s2n(s)) # 十进制转字符串 n = 0x666c61677b706361747d print(libnum.n2s(n)) # 二进制转字符串 b = '01100110011011000110000101100111011110110111000001100011011000010111010001111101' print(libnum.b2s(b)) # 字符串转二进制 s = 'flag{pcat}' print(libnum.s2b(s))

arrow#

arrow 可以很方便的处理时间和日期

Copy
import arrow now = arrow.now() print(now) # 2022-12-11T00:25:59.424262+08:00 year = now.format('YYYY') print("Year: {0}".format(year)) # Year: 2022 date = now.format('YYYY-MM-DD') print("Date: {0}".format(date)) # Date: 2022-12-11 date_time = now.format('YYYY-MM-DD HH:mm:ss') print("Date and time: {0}".format(date_time)) # Date and time: 2022-12-11 00:25:59 date_time_zone = now.format('YYYY-MM-DD HH:mm:ss ZZ') print("Date and time and zone: {0}".format(date_time_zone)) # Date and time and zone: 2022-12-11 00:25:59 +08:00

tinydb#

tinydb 是一个轻量级数据库,和 MongoDB 类似是处理 json 文件,但只需要 pip 一下即可使用,非常方便

Copy
from tinydb import * db = TinyDB('db.json') # 如果没有会自动创建 db.insert({'type': 'apple', 'count': 10}) db.insert({'type': 'banana', 'count': 20}) # [{'type': 'apple', 'count': 10}, {'type': 'banana', 'count': 20}] # 单条创建 db.insert_multiple([ {'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}]) # [{'type': 'apple', 'count': 10}, {'type': 'banana', 'count': 20}, {'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}] # 多条创建 print(db.all()) # [{'type': 'apple', 'count': 10}, {'type': 'banana', 'count': 20}, {'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}] # 查看全部数据 Fruit = Query() print(db.search(Fruit.type == 'apple')) # [{'type': 'apple', 'count': 10}] # 查看特定数据 db.update({'type': 'apple', 'count': 50}) print(db.search(Fruit.type == 'apple')) # [{'type': 'apple', 'count': 50}, {'type': 'apple', 'count': 50}, {'name': 'John', 'age': 22, 'type': 'apple', 'count': 50}, {'name': 'John', 'age': 37, 'type': 'apple', 'count': 50}] # 更新数据 db.remove(Fruit.count < 40) print(db.all()) # [{'type': 'apple', 'count': 50}, {'type': 'apple', 'count': 50}, {'name': 'John', 'age': 22, 'type': 'apple', 'count': 50}, {'name': 'John', 'age': 37, 'type': 'apple', 'count': 50}] # 删除某条数据 db.truncate() print(db.all()) # [] # 清空数据库
posted @   筱团  阅读(220)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示
目录