Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the practical Python modules?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what are the practical Python modules". In the daily operation, I believe many people have doubts about the practical Python modules. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the practical Python modules?" Next, please follow the editor to study!

1. Psutil

The Psutil module in Python is a cross-platform library, which can easily obtain the process and system utilization of the system, including CPU, memory, disk, network and other information, and its installation is also very simple.

Command line:

Pip install psutil

Here, because the overall space is limited, the editor only lists a few commonly used methods for the time being. For example, we want to check the utilization rate of CPU.

Psutil.cpu_percent ()

The returned result represents the current system-wide CPU utilization percentage, if we want to look at the number of CPU in the system

The code is as follows:

# # number of logical CPU psutil.cpu_count () # # number of physical CPU psutil.cpu_count (logical=False)

Or maybe we want to take a look at the physical memory in the system, as follows:

# # remaining physical memory free = str (round (psutil.virtual_memory (). Free / (1024.0 * 1024.0 * 1024.0), 2)) # # Total physical memory has total = str (round (psutil.virtual_memory (). Total / (1024.0 * 1024.0 * 1024.0), 2)

If we want to see the information about a single disk, we call the disk_usage () method directly.

Print (psutil.disk_usage ('C:\\))

To get information about all the disks, the disk_partitions () method is called.

Print (psutil.disk_partitions ())

In addition, we can also get the startup time of the system.

From datetime import datetimeprint (u "system startup time:% s"% datetime.fromtimestamp (psutil.boot_time ()) .strftime ("% Y-%m-%d% H:%M:%S")) II. Pendulum

Generally speaking, we use the datatime module to deal with date, time and other data, but we have to say that the datatime module also has some limitations, such as some deficiencies in dealing with the time zone. This time we will introduce the Pendulum module.

First, we use the pip command line to install:

Pip install pendulum

The most impressive feature of the pendulum module is the time zone. For example, if we want to know the time in Paris, we can do this.

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 # date of yesterday

2022-01-22T00:00:00+08:00 # Today

2022-01-23T00:00:00+08:00 # tomorrow

We can also add and subtract the time data, 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 this, as follows:

Dt = pendulum.datetime (2022, 1, 23, 15, 16, 10)

If what we need is a prefixed date string, we can do this

Dt.to_date_string ()

Output:

2022-01-23

If what we need is a time string with a suffix, we can do this.

Dt.to_time_string ()

Output:

15:16:10

Of course, we sometimes need both date and time, and 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, this module also has many other powerful functions, specifically you can look at its documentation, and finally we want to talk about its humanized time output function.

If we usually use the search engine, we will see that the time with a lot of content is marked as "1 day ago", "1 week later" and so on, which can 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 can't read 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 ()) # #' 5 seconds later 'III. Pyfiglet

Pyfiglet is a module designed to generate artwork and supports fonts with multiple artwork.

Let's 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 all the output fonts to shape the art characters.

At this point, the study of "what are the practical Python modules" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report