Python has a lot of cool third-party libraries that make the task easier.

Today I will share five interesting Python libraries, each very useful!

Speedtest, Socket, TextBlob, PyGame, PyQrCode, PyShorteners, GoogleTrans, Pendulum, Fabulous, PyWebView.

Here is to introduce to you ~

Speedtest

The Speedtest module tests your computer’s network bandwidth.

Use the Baidu Source installation library.

# installation speedtest PIP install speedtest - I https://mirror.baidu.com/pypi/simple/Copy the code

To use the certificate, you need to cancel certificate verification.

_create_default_https_context = ssl._create_unverified_context test = Speedtest.speedtest () down = test.download() upload = test.upload() print(f" {round(upload/(1024 * 1024),2)} Mbps") print(f" download speed: {round(down/(1024 * 1024),2)} Mbps")Copy the code

The result is as follows.

Upload speed: 31.3 Mbps Download speed: 86.34 MbpsCopy the code

Looks like little F has a pretty fast Internet connection

② Socket (obtain the local IP address) To use the socket, obtain the host name of the computer, and then obtain the LOCAL IP address.

Socket is the built-in Python standard library and does not need to be installed.

Import socket as f hostn = f.gethostname() Laptop = f.gethostbyname(hostn)Copy the code

The result is as follows. This IP is an intra-lan IP.

Your computer's local IP address is 192.168.2.101Copy the code

To obtain your computer’s public IP address, use third-party websites like this one.

Return the public IP address https://jsonip.comCopy the code

The code is as follows, also cancel certificate verification.

Request import urlopen # Import SSL ssl._create_default_https_context = import json from urllib.request import urlopen # 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.load (content)[' IP '] print(" http://hjson.hjson.htm ")Copy the code

Make a request to the web site and parse the returned results.

The public IP address is obtained successfully.

Your computer's public IP address is 120.236.128.201Copy the code

Textblob is a Python library for processing text data, only for English analysis.

For Chinese, SnowNLP can be used to easily process Chinese text content, which is inspired by TextBlob.

Here’s a spell check for English.

from textblob import TextBlob

a = TextBlob("I dream about workin with goof company")
a = a.correct()
print(a)
Copy the code

The results are as follows.

I dream about working with good company
Copy the code

As you can see, the words in the sentence have been corrected.

④ Pygame

Pygame, a Python library for making games.

It not only provides developers with graphics and sound libraries to make 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, 70]) mixer.init() Mixer.music.load (music) Mixer.music.play () # click * to close the interface while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit()Copy the code

You have to add a graphical interface to PyGame, otherwise there will be no sound.

⑤ PyQrCode (generate QR code)

QR Code(Quick Response Code) is a kind of two-dimensional bar Code. Invented by Denso Wave in Japan in 1994.

Nowadays, with the popularity of smart phones, they have been widely used in daily life, such as commodity information query, social friends interaction, network address access and so on.

The PyQrCode module is a QR code generator that is simple to use and written in pure Python.

The installation.

# installation pyqrcode PIP install pyqrcode -i https://mirror.baidu.com/pypi/simple/Copy the code

Below will “Baidu a” to generate a TWO-DIMENSIONAL code.

import pyqrcode
import png
from pyqrcode import QRCode


inpStr = "www.baidu.com"
qrc = pyqrcode.create(inpStr)
qrc.png("baidu.png", scale=6)
Copy the code

The qr code picture is as follows.

Wechat scan out is text content, baidu website, should be operating.

Scan with the phone’s browser, and you can jump to the web page normally.