您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

又双叒叕 get 了 七个超强的 Python 库

2024/3/16 10:23:39发布31次查看
有一句谚语:不必重新造轮子。python库是这方面的最好例子。它可以帮助你以一种简单的方式编写复杂而耗时的功能。据我所知,一个好的项目会使用一些最好的库。
1. pysnooper我们正在编写一个通过返回位列表将数字转换为二进制的函数。@pysnooper.snoop()可以通过添加装饰器来研究它:
import pysnooper @pysnooper.snoop() def number_to_bits(number): if number: bits = [] while number: number, remainder = divmod(number, 2) bits.insert(0, remainder) return bits else: return [0] number_to_bits(6)
或者,如果不想跟踪整个函数,则可以将相关部分包装在一个with块中:
import pysnooper import random def foo(): lst = [] for i in range(10): lst.append(random.randrange(1, 1000)) with pysnooper.snoop(): lower = min(lst) upper = max(lst) mid = (lower + upper) / 2 print(lower, mid, upper) foo()
输出如下:
new var:....... i = 9 new var:....... lst = [681, 267, 74, 832, 284, 678, ...] 09:37:35.881721 line10 lower = min(lst) new var:....... lower = 74 09:37:35.882137 line11 upper = max(lst) new var:....... upper = 832 09:37:35.882304 line12 mid = (lower + upper) / 2 74 453.0 832 new var:....... mid = 453.0 09:37:35.882486 line13 print(lower, mid, upper) elapsed time: 00:00:00.000344
作为开发者,大部分时间都用在了调试上。这个库是一个调试器。大多数人都会在战略位置上使用print​行,其中一些行会显示变量的值。这个库可以做同样的事情,只不过你不需要精心设计正确的print行,而只需要在你感兴趣的函数上添加一行装饰符。你会得到一个函数的逐字记录日志,包括哪些行在什么时候运行,以及局部变量什么时候被改变。它在github上有超过15k颗星。
项目地址:https://github.com/cool-rr/pysnooper
2. schedule人类的 python 作业调度。使用友好的语法定期运行 python 函数(或任何其他可调用函数)。
一个简单易用的 api,用于安排作业,专为人类设计。周期性作业的进程内调度程序。不需要额外的过程!非常轻巧,没有外部依赖。出色的测试覆盖率。可在 python 和 3.6、3.7、3.8、3.9 上测试import schedule import time def job(): print(i'm working...) schedule.every(10).seconds.do(job) schedule.every(10).minutes.do(job) schedule.every().hour.do(job) schedule.every().day.at(10:30).do(job) schedule.every(5).to(10).minutes.do(job) schedule.every().monday.do(job) schedule.every().wednesday.at(13:15).do(job) schedule.every().day.at(12:42, europe/amsterdam).do(job) schedule.every().minute.at(:17).do(job) def job_with_argument(name): print(fi am {name}) schedule.every(10).seconds.do(job_with_argument, name=peter) while true: schedule.run_pending() time.sleep(1)
这是一个面向人类的python作业调度库。它可以让你使用友好的语法周期性地运行python函数(或任何其他可调用)。它包括很多功能,比如周期性工作的进程内调度器(不需要额外的进程),非常轻量级,没有外部依赖性,有很好的测试覆盖率等等。这个库在github上有超过10k颗星。
项目地址:https://github.com/dbader/schedule
3. mechanicalsoupexample usage of mechanicalsoup to get the results from the qwant search engine. import re import mechanicalsoup import html import urllib.parse # connect to qwant browser = mechanicalsoup.statefulbrowser(user_agent='mechanicalsoup') browser.open(https://lite.qwant.com/) # fill-in the search form browser.select_form('#search-form') browser[q] = mechanicalsoup browser.submit_selected() # display the results for link in browser.page.select('.result a'): # qwant shows redirection links, not the actual url, so extract # the actual url from the redirect link: href = link.attrs['href'] m = re.match(r^/redirect/[^/]*/(.*)$, href) if m: href = urllib.parse.unquote(m.group(1)) print(link.text, '->', href)
这个库将帮助你实现与网站的自动互动。它自动存储和发送cookies,跟踪重定向,并能跟踪链接和提交表单。它不做javascript。这个库在github上有超过4k颗星。
项目地址:https://github.com/mechanicalsoup/mechanicalsoup
4. ftfy>>> from ftfy import fix_encoding >>> print(fix_encoding((ง'✣')ง)) (ง'⌣')ง
它能做什么以下是 ftfy 可以做什么的一些示例(在现实世界中找到):
ftfy 可以通过检测明显是 utf-8 但被解码为其他字符的字符模式来修复 mojibake(编码混淆):
>>> import ftfy >>> ftfy.fix_text('✔ no problems') '✔ no problems'
这听起来不可能吗?真的不是。utf-8 是一种设计良好的编码,当它被误用时会很明显,而且一串 mojibake 通常包含我们恢复原始字符串所需的所有信息。
ftfy 可以同时修复多层 mojibake:
>>> ftfy.fix_text('the mona lisa doesnãƒâ¢ã¢â€šâ¬ã¢â€žâ¢t have eyebrows.') the mona lisa doesn't have eyebrows.
它可以修复上面应用了“卷曲引号”的 mojibake,在引号展开之前无法持续解码:
>>> ftfy.fix_text(l’humanitã©) l'humanité
ftfy 可以修复包含字符 u+a0(不间断空格)的 mojibake,但 u+a0 变成了 ascii 空格,然后与另一个以下空格组合:
>>> ftfy.fix_text('ãxa0 perturber la rã©flexion') 'à perturber la réflexion' >>> ftfy.fix_text('ã perturber la rã©flexion') 'à perturber la réflexion'
ftfy 还可以解码出现在 html 之外的 html 实体,即使在实体大写不正确的情况下也是如此:
>>> # by the html 5 standard, only 'pérez' is acceptable >>> ftfy.fix_text('pérez') 'pérez'
这些修复并不适用于所有情况,因为 ftfy 有一个坚定的目标,即避免误报——它永远不应该将正确解码的文本更改为其他内容。
以下文本可以在 windows-1252 中编码并在 utf-8 中解码,并将解码为“marquʌ”。然原文已明理,故不改。
>>> ftfy.fix_text('il y marqué…') 'il y marqué…'
这个库将帮助你修复以各种方式损坏的unicode。这个库的目标是接收坏的unicode并输出好的unicode,以便在你的unicode感知代码中使用。它在github上有超过3千颗星。
项目地址:https://github.com/rspeer/python-ftfy
5. rpyc
这是一个透明的python库,用于对称的远程过程调用、集群和分布式计算。它利用对象代理这一技术,利用python的动态特性,克服进程和计算机之间的物理界限,使远程对象可以像本地一样被操作。这个库在github上有超过1k颗星。
项目地址:https://github.com/tomerfiliba-org/rpyc
6. pygletpyglet 的一些特性是:
没有外部依赖项或安装要求。对于大多数应用程序和游戏需求,pyglet 除了 python 之外不需要其他任何东西,简化了分发和安装。使用 pyinstaller 等冷冻机可以轻松打包您的项目。利用多窗口和多显示器桌面。pyglet允许你使用多个平台原生窗口,并且完全了解用于全屏游戏的多显示器设置。加载几乎任何格式的图像、声音、音乐和视频。pyglet可以选择使用 ffmpeg 播放音频格式,如 mp3、ogg/vorbis 和 wma,以及视频格式,如 mpeg2、h.264、h.265、wmv 和 xvid。如果没有 ffmpeg,pyglet包含对 wav、png、bmp 等标准格式的内置支持。pyglet 完全用纯 python 编写,并利用ctypes模块与系统库进行交互。你可以修改代码库或做出贡献,而无需任何第二语言编译步骤或编译器设置。尽管是纯 python,但pyglet具有出色的性能,这要归功于用于绘制数千个对象的高级批处理。pyglet 是在 bsd 开源许可证下提供的,允许你将它用于商业和其他开源项目,几乎没有限制。import pyglet window = pyglet.window.window() label = pyglet.text.label('hello, world!', font_size=36, x=window.width // 2, y=window.height // 2, anchor_x='center', anchor_y='center') @window.event def on_draw(): window.clear() label.draw() pyglet.app.run()
这是一个跨平台的python窗口和多媒体库,用于开发游戏和其他视觉效果丰富的应用程序。它支持窗口化、用户界面事件处理、操纵杆、opengl图形、加载图像和视频,以及播放声音和音乐。它可以在windows、os x和linux上运行。它在github上有超过1千颗星。
项目地址:https://github.com/pyglet/pyglet
7. ropeimport rope.base.project myproject = rope.base.project.project('/path/to/myproject')
这个库提供了强大而安全的重构。它包括轻度依赖性等特点,与pyright或pylance不同,它不依赖node.js,完全由python编写,等等。它在github上有超过1千颗星。
项目地址:https://github.com/python-rope/rope
文档地址:https://rope.readthedocs.io/en/latest/overview.html
以上就是又双叒叕 get 了 七个超强的 python 库的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product