Hello, everyone. Today I will introduce three Python modules that are particularly easy to use. Few people know about them, but they are particularly easy to use.
-
Psutil
-
Pendulum
-
Pyfiglet
Psutil
The Psutil module in Python is a cross platform library. It can easily obtain the process and system utilization of the system, including CPU, memory, disk, network and other information. Its installation is also very simple, and the command line
pip install psutil
Because the overall space is limited here, I will only list a few common methods for the time being. For example, we want to check the CPU utilization
psutil.cpu_percent()
The returned result represents the percentage of CPU utilization in the current system. If we want to check the number of CPUs in the system, the code is as follows
## Number of logical CPU s psutil.cpu_count() ## Number of physical CPU s psutil.cpu_count(logical=False)
Or we want to check the physical memory in the system. The code is as follows
## Remaining physical memory free = str(round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2)) ## Total physical memory total = str(round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2))
If we want to view the information of a single disk, we can call disk directly_ Usage() method
print(psutil.disk_usage('C:\\'))
To get the information of all disks, disk is called_ Partitions() method
print(psutil.disk_partitions())
In addition, we can also get the startup time of the system
from datetime import datetime print(u"System startup time: %s" % datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))
Pendulum
Generally, we use datatime module to process date, time and other data, but we have to say that datatime module also has its own limitations. For example, it will be insufficient when dealing with time zone. This time, let's introduce Pendulum module
First, we use the pip command line to install
pip install pendulum
The most impressive function of the pendulum module is the time zone. For example, if we want to know the time of "Paris", we can do so
now_in_paris = pendulum.now('Europe/Paris') print(now_in_paris)
output
2022-01-22T14:59:06.484816+01:00
You can also know the date of the day
d1 = pendulum.yesterday() # yesterday d2 = pendulum.today() # today d3 = pendulum.tomorrow() # tomorrow
output
2022-01-21T00:00:00+08:00 # Yesterday's date 2022-01-22T00:00:00+08:00 # today 2022-01-23T00:00:00+08:00 # tomorrow
We can also add and subtract time data by calling the add and subtract methods
dt = pendulum.datetime(2022, 1, 22) dt_years_add = dt.add(years=5) print(dt_years_add) dt_years_subtract = dt.subtract(years=1) print(dt_years_subtract) dt_month_add = dt.add(months=60) print(dt_month_add) dt_month_subtract = dt.subtract(months=60) print(dt_month_subtract)
output
2027-01-22T00:00:00+00:00 2021-01-22T00:00:00+00:00 2027-01-22T00:00:00+00:00 2017-01-22T00:00:00+00:00
If we want to convert the time data into a string, we can do so. The code is as follows
dt = pendulum.datetime(2022, 1, 23, 15, 16, 10)
If we need a prefix date string, we can do so
dt.to_date_string()
output
2022-01-23
If we need a suffix time string, we can do so
dt.to_time_string()
output
15:16:10
Of course, sometimes we need both date and time. The code is as follows
dt.to_datetime_string()
output
2022-01-23 15:16:10
Or
dt.to_day_datetime_string()
output
Sun, Jan 23, 2022 3:16 PM
Of course, the module has many other powerful functions. You can see its documents. Finally, we want to talk about the humanized time output function.
If we usually use the search engine, we will see that the time of many contents is marked as "1 day ago", "1 week later" and so on. This can also be easily implemented in the pendulum module
print(pendulum.now().subtract(days=1).diff_for_humans()) ## '1 day ago' print(pendulum.now().diff_for_humans(pendulum.now().subtract(years=1))) ## '1 year after' print(pendulum.now().subtract(days=24).diff_for_humans()) ## '3 weeks ago'
Maybe if some people don't understand English, we can also switch to Chinese, as follows
print(pendulum.now().subtract(days=14).diff_for_humans()) ## '2 weeks ago' print(pendulum.now().add(seconds=5).diff_for_humans()) ## 'after 5 seconds'
Pyfiglet
pyfiglet is a module specially used to generate artistic characters, and supports fonts with a variety of artistic characters. Let's take a look at the following example
result = pyfiglet.figlet_format("Python", font="larry3d") print(result)
output
____ __ __ /\ _`\ /\ \__/\ \ \ \ \L\ \__ __\ \ ,_\ \ \___ ___ ___ \ \ ,__/\ \/\ \\ \ \/\ \ _ `\ / __`\ /' _ `\ \ \ \/\ \ \_\ \\ \ \_\ \ \ \ \/\ \L\ \/\ \/\ \ \ \_\ \/`____ \\ \__\\ \_\ \_\ \____/\ \_\ \_\ \/_/ `/___/> \\/__/ \/_/\/_/\/___/ \/_/\/_/ /\___/ \/__/
If you don't like the font above, you can use the following code
pyfiglet.FigletFont.getFonts()
Choose any one of the output fonts to shape artistic characters
Recommended articles
- Addicted, I recently gave the company a large visual screen (with source code)
- So elegant, four Python automatic data analysis artifacts are really fragrant
- After combing for more than half a month, we have carefully prepared 17 knowledge and thinking maps. This time, we should clarify statistics
- Year end summary: 20 visual large screen templates, directly apply Zhenxiang (the source code is attached at the end of the article)
Technical exchange
Welcome to reprint, collect, gain, praise and support!
At present, a technical exchange group has been opened, with more than 2000 group friends. The best way to add notes is: source + Interest direction, which is convenient to find like-minded friends
- Method ① send the following pictures to wechat, long press to identify, and the background replies: add group;
- Mode ②. Add micro signal: dkl88191, remarks: from CSDN
- WeChat search official account: Python learning and data mining, background reply: add group