Home  >  Article  >  Backend Development  >  I got seven super powerful Python libraries again

I got seven super powerful Python libraries again

王林
王林forward
2023-04-11 22:10:101709browse

There is a proverb: "You don't have to reinvent the wheel." Python libraries are the best example of this. It helps you write complex and time-consuming functionality in a simple way. As far as I know, a good project uses some of the best libraries.

1. PySnooper

We are writing a function that converts a number to binary by returning a list of bits. @pysnooper.snoop() can be studied by adding a decorator:

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)

Alternatively, if you don't want to trace the entire function, you can wrap the relevant parts in a with block:

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()

Output As follows:

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

As a developer, most of the time is spent on debugging. This library is a debugger. Most people use print lines at strategic locations, some of which display the value of a variable. This library does the same thing, except instead of crafting the right print lines, you just add a line of decorators to the functions you're interested in. You'll get a verbatim log of the function, including which lines were run when, and when local variables were changed. It has over 15k stars on GitHub.

Project address: https://github.com/cool-RR/PySnooper

2. schedule

Human Python job scheduling. Run a Python function (or any other callable function) periodically using friendly syntax.

  • A simple and easy-to-use API for scheduling jobs, designed for humans.
  • In-process scheduler for periodic jobs. No additional process required!
  • Very lightweight and no external dependencies.
  • Excellent test coverage.
  • Can be tested on Python and 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(f"I am {name}")

schedule.every(10).seconds.do(job_with_argument, name="Peter")

while True:
schedule.run_pending()
time.sleep(1)

This is a Python job scheduling library for humans. It lets you run a Python function (or any other callable) periodically using friendly syntax. It includes many features such as an in-process scheduler that works periodically (no additional processes required), is very lightweight, has no external dependencies, has good test coverage, etc. This library has over 10k stars on GitHub.

Project address: https://github.com/dbader/schedule

3. MechanicalSoup

"""Example 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)

This library will help you implement the website automatic interaction. It automatically stores and sends cookies, tracks redirects, and can follow links and submit forms. It doesn't do Javascript. This library has over 4k stars on GitHub.

Project address: https://github.com/MechanicalSoup/MechanicalSoup

4. ftfy

>>> from ftfy import fix_encoding
>>> print(fix_encoding("(ง'⌣')ง"))
(ง'⌣')ง

What can it do

Here are some examples of what ftfy can do (found in the real world):

ftfy can fix mojibake (encoding obfuscation) by detecting character patterns that are obviously UTF-8 but are decoded to other characters :

>>> import ftfy
>>> ftfy.fix_text('✔ No problems')
'✔ No problems'

Does this sound impossible? really not. UTF-8 is a well-designed encoding that is obvious when it is misused, and a string of mojibake usually contains all the information we need to recover the original string.

ftfy can fix multiple layers of mojibake at the same time:

>>> ftfy.fix_text('The Mona Lisa doesn’t have eyebrows.')
"The Mona Lisa doesn't have eyebrows."

It can fix mojibake with "curly quotes" applied above, which cannot be decoded continuously until the quotes are expanded:

>>> ftfy.fix_text("l’humanité")
"l'humanité"

ftfy Can fix mojibake containing character U A0 (non-breaking space), but U A0 becomes ASCII space, then combined with another following space:

>>> ftfy.fix_text('Ãxa0 perturber la réflexion')
'à perturber la réflexion'
>>> ftfy.fix_text('à perturber la réflexion')
'à perturber la réflexion'

ftfy Can also decode HTML entities that appear outside of HTML , even in cases where entities are incorrectly capitalized:

>>> # by the HTML 5 standard, only 'PÉREZ' is acceptable
>>> ftfy.fix_text('PÉREZ')
'PÉREZ'

These fixes do not apply in all cases, as ftfy has a firm goal of avoiding false positives - it should never decode correctly The text is changed to something else.

The following text can be encoded in Windows-1252 and decoded in UTF-8 and will be decoded as "MARQUɅ". However, the original text is clear, so it will not be changed.

>>> ftfy.fix_text('IL Y MARQUÉ…')
'IL Y MARQUÉ…'

This library will help you fix Unicode that is broken in various ways. The goal of this library is to receive bad Unicode and output good Unicode for use in your Unicode-aware code. It has over 3k stars on GitHub.

项目地址:https://github.com/rspeer/python-ftfy

5. rpyc

I got seven super powerful Python libraries again

这是一个透明的python库,用于对称的远程过程调用、集群和分布式计算。它利用对象代理这一技术,利用python的动态特性,克服进程和计算机之间的物理界限,使远程对象可以像本地一样被操作。这个库在GitHub上有超过1k颗星。

项目地址:https://github.com/tomerfiliba-org/rpyc

6. pyglet

pyglet 的一些特性是:

  • 没有外部依赖项或安装要求。对于大多数应用程序和游戏需求,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. rope

import 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

The above is the detailed content of I got seven super powerful Python libraries again. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete