preface
10 wonderful Python libraries, I was stunned after reading!
Let's start happily~
development tool
Python version: 3.6.4
Related modules:
socket module;
textblob module;
pygame module;
pyqrcode module;
pyshorteners module;
Google trans module;
pendulum module;
fabulous module;
pywebview module;
Environment construction
Install Python and add it to the environment variable. pip can install the relevant modules required.
Speedtest (network speed test)
The Speedtest module can test the network bandwidth of the computer.
Use Baidu source installation library.
#Install speedtest pip install speedtest -i https://mirror.baidu.com/pypi/simple/ Copy code
When using, you need to cancel certificate verification
import speedtest #Global cancel certificate validation import ssl ssl._create_default_https_context = ssl._create_unverified_context\ test = speedtest.Speedtest() down = test.download() upload = test.upload() print(f"Upload speed:{round(upload/(1024 * 1024),2)} Mbps")\ print(f"Download speed:{round(down/(1024 * 1024),2)} Mbps") Copy code
The results are as follows
Upload speed: 31.3 Mbps Download speed: 86.34 Mbps Copy code
Socket (get local ip address)
Using socket, first obtain the host name of the computer, and then obtain the IP address of the computer.
socket is Python's built-in standard library, which does not need to be installed.
import socket as f hostn = f.gethostname() Laptop = f.gethostbyname(hostn) print("Your computer is local IP the address is:" + Laptop) Copy code
The results are as follows. This IP is the IP in the LAN.
Your computer is local IP The address is: 192.168.2.101 Copy code
If you want to get the public IP address of the computer, you can use some third-party websites, such as the following.
#Browser access, return public IP address https://jsonip.com Copy code
The code is as follows, and the certificate verification is also cancelled.
import json from urllib.request import urlopen #Global cancel certificate validation import ssl ssl._create_default_https_context = ssl._create_unverified_context with urlopen(r'https://jsonip.com') as fp: content = fp.read().decode() ip = json.loads(content)['ip'] print("Your computer public network IP the address is:" + ip) Copy code
Initiate a request for the website and parse the returned results.
Finally, the public IP address is successfully obtained.
#Here's a random one~ Your computer public network IP The address is: 120.236.128.201 Copy code
Textblob (text processing)
TextBlob is a Python library for processing text data, which is only used for English analysis.
Chinese can use SnowNLP, which can easily process Chinese text content. It is inspired by TextBlob.
Let's do a spell check for English.
from textblob import TextBlob a = TextBlob("I dream about workin with goof company") a = a.correct() print(a) Copy code
give the result as follows
I dream about working with good company Copy code
You can see that the words in the sentence have been corrected.
PyGame (making games)
pygame, a Python library for making games.
It not only provides developers with a graphics and sound library for making games, but also uses built-in modules to realize complex game logic.
Let's use pygame to make a small music player
from pygame import mixer import pygame import sys pygame.display.set_mode([300, 300]) music = "my_dream.mp3" mixer.init() mixer.music.load(music) mixer.music.play() #Click × Code that can close the interface while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() Copy code
Pyqrcode (generate QR code)
QR code is abbreviated as QR Code(Quick Response Code), scientific name is quick response matrix code, which is a kind of two-dimensional bar code. It was invented by Denso Wave company of Japan in 1994.
Now, with the popularity of smart phones, they have been widely used in daily life, such as commodity information query, social friend interaction, network address access and so on.
pyqrcode module is a QR code generator, which is simple to use and written in pure python.
Installation.
#Install pyqrcode pip install pyqrcode -i https://mirror.baidu.com/pypi/simple/ Copy code
Next, baidu click will generate a QR code
import pyqrcode import png from pyqrcode import QRCode inpStr = "www.baidu.com" qrc = pyqrcode.create(inpStr) qrc.png("baidu.png", scale=6) Copy code
Short URLs
Python shorteners is a simple Python library for URL shortening.
18 short chain root domain names are provided for use.
#Installing pyshorteners\ pip install pyshorteners -i https://mirror.baidu.com/pypi/simple/ Copy code
In Clck Ru format as an example
import pyshorteners as psn url = "http://www.shuhai.com/" u = psn.Shortener().clckru.short(url) print(u) Copy code
The results are as follows
#Results https://clck.ru/WPJgg Copy code
Google trans (translation)
Google Trans is a free and unlimited Python Translation Library, which can be used to automatically detect language types, translations and so on.
Install version 3.1.0a0. The latest version cannot be used.
#Install Google trans pip install googletrans==3.1.0a0 -i https://mirror.baidu.com/pypi/simple/ Copy code
View all supported languages.
import googletrans from googletrans import Translator print(googletrans.LANGUAGES) Copy code
The results are as follows, including simplified and traditional Chinese.
LANGUAGES = { 'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'he': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'or': 'odia', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'ug': 'uyghur', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu', } Copy code
translater = Translator() out = translater.translate("Hello", dest='en', src='auto') print(out) Copy code
Hello
give the result as follows
#Translation results Translated(src=zh-CN, dest=en, text=Hello, pronunciation=None, extra_data="{'translat...") Copy code
src: the language of the source text
dest: the language in which the source text is converted.
text: translation results
Pendulum (time)
Pendulum is a Python library that handles dates and times, which is useful when it comes to time zones.
Installation.
#Installing pendulum pip install pendulum -i https://mirror.baidu.com/pypi/simple/ Copy code
Look at the time two minutes ago.
import pendulum past = pendulum.now().subtract(minutes=2) print(past.diff_for_humans()) print(past) Copy code
give the result as follows
2 minutes ago 2021-07-25T19:10:09.222953+08:00 Copy code
Fabulous (add text color)
If you run a Python program on the command line, the output is the same color, which is inconvenient to observe
With Fabulous, you can add images and colored text to highlight the output
Installation.
#Installing fabulous pip install fabulous -i https://mirror.baidu.com/pypi/simple/ Copy code
Let's take a look at an example!
from fabulous.color import bold, magenta, highlight_red print(bold(magenta( """ hello world this is some new line and here is the last line. :) """ ))) Copy code
As a result, the output font is bold and colored
pywebview(GUI browser)
pywebview is a Python library for displaying HTML, CSS, and JavaScript content in GUI form.
This means that with this library, you can display web pages in desktop applications.
install
#Install pywebview pip install pywebview -i https://mirror.baidu.com/pypi/simple/ Copy code
Start a window of a given website and run the following code
import webview window = webview.create_window( title='use Baidu Search,It's all advertising', url='http://www.baidu.com', width=850, height=600, resizable=False, #Fixed window size text_select=False, #Prohibit selection of text content confirm_close=True #Prompt when closing ) webview.start() Copy code
give the result as follows
I hope it can help you!
For those who need to get free materials, add a little assistant vx: soxwv # to get materials for free!