Home  >  Article  >  Backend Development  >  Reduce costs and increase efficiency! 12 must-try Python toolkits!

Reduce costs and increase efficiency! 12 must-try Python toolkits!

WBOY
WBOYforward
2023-04-19 22:10:051965browse

In this article, I have selected 12 of the most useful software packages to share with you. I believe you will learn something!

1. Dash

Dash is relatively new. It is ideal for building data visualization applications using pure Python, so it is especially suitable for those who work with data. Dash is a hybrid of Flask, Plotly.js and React.js.

Reduce costs and increase efficiency! 12 must-try Python toolkits!

#Dash quickly puts the content you want into beautiful dashboards without touching a single line of Javascript.

2. PyGame

Pygame is the Python wrapper module of the SDL multimedia library. Simple DirectMedia Layer is a cross-platform development library designed to provide low-level access to OpenGL and Direct3D Pygame's audio keyboard mouse joystick graphics hardware and is highly portable and can run on almost all platforms and operating systems.

It has a complete game engine, and you can also use the library to play MP3 files directly from Python scripts.

3.Pillow

Pillow is a fork of the Python image library. You can use the library to create thumbnails, convert between file formats, rotate, apply filters, display images, and more. This is ideal if you need to perform batch operations on many images.

To understand it quickly, this is how to display an image from Python code:

from PIL import Image
im = Image.open("kittens.jpg")
im.show()
print(im.format, im.size, im.mode)
# JPEG (1920, 1357) RGB

Reduce costs and increase efficiency! 12 must-try Python toolkits!

##4, Colorama

Using Colorama, It's possible to add some color to the terminal:

from colorama import Fore, Back, Style

print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')

The documentation is short and sweet and can be found on the Colorama PyPI page. If you want to use it on Windows as well, you need to call colorama.init() first.

5. JmesPath

Using JSON in Python is very easy because JSON maps very well to Python dictionaries. To me, this is one of its best features.

import jmespath

# Get a specific element
d = {"foo": {"bar": "baz"}}
print(jmespath.search('foo.bar', d))
# baz

# Using a wildcard to get all names
d = {"foo": {"bar": [{"name": "one"}, {"name": "two"}]}}
print(jmespath.search('foo.bar[*].name', d))
# [“one”, “two”]

6. Requests

Requests Create one of the most downloaded Python libraries. It makes web requests really simple, yet still very powerful and versatile.

import requests

r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
r.status_code
# 200
r.headers['content-type']
# 'application/json; charset=utf8'
r.encoding
# 'utf-8'
r.text
# u'{"type":"User"...'
r.json()
# {u'disk_usage': 368627, u'private_gists': 484, ...}

This is a very basic example, but requests can also do all the advanced stuff, like:

Use cookies for authentication

    Perform POST, PUT, DELETE, etc.
  • Use custom certificate
  • Use session
  • Use proxy
7, Simplejson

Local json module in Python What's the problem? No! Actually, Python's json is simplejson, which has the following advantages:

    It can be used on more Python versions.
  • It is updated more frequently than the version that comes with Python.
  • It's written in C, so it's very fast.
  • try:
    import simplejson as json
    except ImportError:
    import json

8. Emoji

This emoji can either impress or repel, depending on who is looking at it. This feature comes in handy if you are analyzing social media data.

Reduce costs and increase efficiency! 12 must-try Python toolkits!

import emoji
result = emoji.emojize('Python is :thumbs_up:')
print(result)
# 'Python is '

# You can also reverse this:
result = emoji.demojize('Python is ')
print(result)
# 'Python is :thumbs_up:'

9. Chardet

You can use the chardet module to detect the character set of a file or data stream. This is useful, for example, when analyzing large amounts of random text. However, it can also be used when working with remotely downloaded data when you don't know what the character set is. After installing chardet, you have an additional command line tool called chardetect, which can be used like this:

$ chardetect somefile.txt
somefile.txt: ascii with confidence 1.0

10, Python-dateutil

The python-dateutil module provides access to the standard datetime module powerful extension. You can do a lot of cool things with this library, such as fuzzing dates in log files.

from dateutil.parser import parse

logline = 'INFO 2020-01-01T00:00:01 Happy new year, human.'
timestamp = parse(logline, fuzzy=True)
print(timestamp)
# 2020-01-01 00:00:01

11. How to use progress bar

progress

from progress.bar import Bar

bar = Bar('Processing', max=20)
for i in range(20):
# Do some work
bar.next()
bar.finish()

Reduce costs and increase efficiency! 12 must-try Python toolkits!

tqdm has roughly the same function, but it is the latest. First, some demonstrations in the form of gif animation:

Reduce costs and increase efficiency! 12 must-try Python toolkits!

12, IPython

If you often use interactive programs but don’t know IPython, you should try it out !Some of the features provided by the enhanced IPython shell include:

    Comprehensive object introspection.
  • Input history, persists across sessions.
  • Cache output results during a session with automatically generated references.
  • Tab completion, by default supports completion of python variables and keywords, file names and function keywords.
  • "Magic" command used to control the environment and perform many IPython or operating system related tasks.
  • Session logging and reloading. Integrated access to the pdb debugger and Python profiler.
  • A little-known feature of IPython: its architecture also allows for parallel and distributed computing.
  • IPython is at the heart of Jupyter Notebook, an open-source web application that lets you create and share documents containing live code, equations, visualizations, and narrative text.

Reduce costs and increase efficiency! 12 must-try Python toolkits!

The above is the detailed content of Reduce costs and increase efficiency! 12 must-try Python toolkits!. 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