Home > Article > Backend Development > I got seven super powerful Python libraries again
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.
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
Human Python job scheduling. Run a Python function (or any other callable function) periodically using friendly syntax.
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
"""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
>>> from ftfy import fix_encoding >>> print(fix_encoding("(ง'⌣')ง")) (ง'⌣')ง
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
这是一个透明的python库,用于对称的远程过程调用、集群和分布式计算。它利用对象代理这一技术,利用python的动态特性,克服进程和计算机之间的物理界限,使远程对象可以像本地一样被操作。这个库在GitHub上有超过1k颗星。
项目地址:https://github.com/tomerfiliba-org/rpyc
pyglet 的一些特性是:
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
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!