These 10 Amazing Python Libraries You Must Try!

Hello everyone, I'm Xiao F~

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

Today I will share with you 10 interesting Python libraries, each of which is very useful!

They are speedtest, socket, textblob, pygame, pyqrcode, pyshorteners, googletrans, pendulum, fabulous, pywebview.

Let me introduce to you below~

① speedtest (Internet speed test)

The Speedtest module can test the network bandwidth of the computer.

Use Baidu source to install the library.

# 安装speedtest
pip install speedtest -i https://mirror.baidu.com/pypi/simple/

When used, certificate verification needs to be canceled.

import speedtest

# 全局取消证书验证
import ssl
ssl._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"下载速度:{round(down/(1024 * 1024),2)} Mbps")

The results obtained are as follows.

上传速度:31.3 Mbps
下载速度:86.34 Mbps

It seems that Xiao F's Internet speed is quite fast.

socket (get the local ip address)

Using socket, first obtain the host name of the computer, and then obtain the IP address of the machine.

Among them, socket is a built-in standard library of Python, which does not need to be installed.

import socket as f

hostn = f.gethostname()
Laptop = f.gethostbyname(hostn)
print("你的电脑本地IP地址是:" + Laptop)

The result is as follows, this IP is the IP in the LAN .

你的电脑本地IP地址是:192.168.2.101

If you want to get the public IP address of your computer , you can use some third-party websites, such as the one below.

# 浏览器访问, 返回公网IP地址
https://jsonip.com

The code is as follows, which also cancels the certificate verification.

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.loads(content)['ip']
print("你的电脑公网IP地址是:" + ip)

Make a request to the website and parse the returned result.

Finally, the public IP address was successfully obtained.

# 这里随便写了一个~
你的电脑公网IP地址是:120.236.128.201

 textblob (text processing)

TextBlob is a Python library for working with textual data, for English analysis only.

Chinese can use SnowNLP , which can easily process Chinese text content, which is inspired by TextBlob.

Let's do a spell check in English.

from textblob import TextBlob

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

The results are as follows.

I dream about working with good company

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

 pygame (making games)

pygame, a Python library for making games.

It not only provides developers with graphics and sound libraries for making games, but also can use built-in modules to implement complex game logic.

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

# 点击×可以关闭界面的代码
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

Run the above code and the computer will play music.

You must add a graphical interface to pygame, otherwise there will be no sound.

 pyqrcode (generate QR code)

QR Code (Quick Response Code) for short, scientifically called Quick Response Matrix Code, is a type of 2D barcode. Invented in 1994 by Denso Wave in Japan.

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.

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

Install.

# 安装pyqrcode
pip install pyqrcode -i https://mirror.baidu.com/pypi/simple/

The following will generate a QR code from "Baidu".

import pyqrcode
import png
from pyqrcode import QRCode


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

The QR code picture is as follows.

WeChat scans out text content, which is Baidu’s website, so it should be something to do with it.

Scan with the browser of the mobile phone, you can jump to the web page normally.

Use documentation:

https://pythonhosted.org/PyQRCode/

 pyshorteners (short URL)

pyshorteners is a simple URL shortening Python library.

18 short-chain root domain names are provided for use.

Install.

# 安装pyshorteners
pip install pyshorteners -i https://mirror.baidu.com/pypi/simple/

Take the clck.ru format as an example.

import pyshorteners as psn

url = "http://www.shuhai.com/"
u = psn.Shortener().clckru.short(url)
print(u)

The results obtained are as follows.

# 结果
https://clck.ru/WPJgg

can be accessed normally.

Use documentation:

https://pyshorteners.readthedocs.io/en/latest/

 googletrans (translation)

Googletrans is a free and unlimited Python translation library that can be used to automatically detect language types, translations, and more.

Install version 3.1.0a0, the latest version cannot be used.

# 安装googletrans
pip install googletrans==3.1.0a0 -i https://mirror.baidu.com/pypi/simple/

View all supported languages.

import googletrans
from googletrans import Translator

print(googletrans.LANGUAGES)

The results are as follows, in which there are two types of Chinese, Simplified and Traditional.

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',
}

Try to translate "hello".

translater = Translator()
out = translater.translate("你好", dest='en', src='auto')
print(out)

The results are as follows.

# 翻译结果
Translated(src=zh-CN, dest=en, text=Hello, pronunciation=None, extra_data="{'translat...")

src: the language of the source text

dest: The language to convert the source text to.

text: the result of the translation

Use documentation:

https://py-googletrans.readthedocs.io/en/latest/

 pendulum (time)

Pendulum is a Python library for working with dates and times, which is very useful when time zones are involved.

Install.

# 安装pendulum
pip install pendulum -i https://mirror.baidu.com/pypi/simple/

Look at the time 2 minutes ago.

import pendulum

past = pendulum.now().subtract(minutes=2)
print(past.diff_for_humans())
print(past)

The results are as follows.

2 minutes ago
2021-07-25T19:10:09.222953+08:00

There are many convenient places to use, you can check the documentation.

Use the document address:

https://pendulum.eustace.io/

 fabulous (add text color)

If you are running a Python program on the command line, the output is all the same color, which is inconvenient to observe.

With Fabulous, you can add images, colored text to highlight the output.

Install.

# 安装fabulous
pip install fabulous -i https://mirror.baidu.com/pypi/simple/

Let's see an example below!

from fabulous.color import bold, magenta, highlight_red

print(bold(magenta(
    """
    hello world
    this is some new line
    and here is the last line. :)
    """
)))


The result is as follows, the output font is bold and colored.

For more configuration, you can visit the usage documentation:

https://jart.github.io/fabulous/

pywebview (GUI browser) 

pywebview is a Python library for displaying HTML, CSS, and JavaScript content in GUI form.

This means that using this library, you can display web pages in desktop applications.

Install.

# 安装pywebview
pip install pywebview -i https://mirror.baidu.com/pypi/simple/

To launch a window for a given website, run the code below.

import webview

window = webview.create_window(
    title='百度一下,全是广告',
    url='http://www.baidu.com',
    width=850,
    height=600,
    resizable=False,    # 固定窗口大小
    text_select=False,   # 禁止选择文字内容
    confirm_close=True   # 关闭时提示
)
webview.start()

The results are as follows.

Well, the sharing of this issue is over.

In general, these niche Python libraries are quite useful.

Interested friends can access relevant documents and learn by themselves.

  end book  

This time, Xiao F and [Peking University Press] bring you 5 books related to neural networks.

" Python Neural Network Introduction and Practical Combat " starts from the basic knowledge of neural network, and gradually goes deep into Python advanced practical combat, and provides detailed explanation and practical application of various practical third-party libraries. Click the image below to see details/purchase????

Book donation rules : After you like this article ("I'm watching" is not required), scan the QR code below and add Xiao F's WeChat. Send me the screenshots of the likes, and I will send you the lucky draw code until 21:00 on July 26th.

Thank you all for your support of F!

Recommended reading

···  END  ···

Guess you like

Origin blog.csdn.net/river_star1/article/details/119091122