In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what functions the Python standard library has. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
One of the great benefits of Python is that it has a useful set of standard libraries (standard library). The standard library is installed on your computer with Python and is part of Python (there are special cases, of course. In some cases, all standard libraries are not used because of system security requirements, such as Google App Engine).
Using existing classes (class) and functions (function) for development can save you the trouble of writing all your programs from scratch. These standard libraries are bricks that have been burned to build a house, which is much more convenient than burning bricks yourself.
Based on my personal experience, I will first select the package introduction of the following three aspects of the standard library to illustrate the powerful functions of the standard library:
Python Enhancement; system interaction; Network
* Class: Python enhancement
Some of the existing functions of Python itself can be enhanced with the use of standard libraries.
1) word processing
The string class of Python provides methods for dealing with strings. But Python doesn't stop there. Python implements support for regular expressions (regular expression) through the re package in the standard library. The regular expression of Python is comparable to the regular expression of Perl and Linux bash.
(regular expressions search for or replace strings that match the template in the text through a custom template. For example, you can search for all the numbers in a text. The key to regular expressions is to form templates according to your own needs. )
In addition, the Python standard library also provides a richer format for string output, such as string package, textwrap package.
2) data object
Our previous quick tutorials introduced data objects such as tables (list) and dictionaries (dictionary). They have different characteristics and are suitable for the organization and management of data in different situations. Python's standard library defines more data objects, such as array and Queue. These data objects also have their own characteristics and functions. A Python user who is familiar with data structures (data structure) can find the data structures they need in these packages.
In addition, we often use copy packages to copy objects.
3) date and time
The management of date and time is not complicated, but it is easy to make mistakes. Python's standard library for date and time management is quite complete (using time package to manage time, using datetime package to manage date and time), you can not only query and change date and time (for example, July 18, 2012 corresponds to the day of the week), but also calculate date and time (such as what date and time is after 13:00 in 2000.1.1.1378 hours). Through these standard libraries, you can also control the text format of the date and time output as needed (for example, output '2012-7-18' or'18 Jul 2012')
4) Mathematical operation
In the standard library, Python defines some new numeric types (decimal package, fractions package) to make up for the possible shortcomings of the previous numeric types (integer, float). The standard library also includes random packages for dealing with random number-related functions (generating random numbers, random sampling, etc.). The math package adds some important mathematical constants and mathematical functions, such as pi, trigonometric functions, and so on.
Although numpy is not a package in the standard library, its good support for array operations makes it widely used in scientific research and computing based on Python.)
5) Stora
In our previous quick tutorial, we only mentioned the input and output of text. In fact, Python can import or export any object. These objects can be converted to binary format (binary) through the pickle package in the standard library and then stored in a file, or they can be read from the binary file in reverse.
In addition, basic database functions (sqlite3 packages) are supported in the standard library. Files in XML and csv formats also have corresponding processing packages.
The second category: system interaction
System interaction, mainly refers to the interaction between Python and operating system (operate system), file system (file system). Python can implement many functions of an operating system. It can manage the operating system like bash scripts, which is why Python is sometimes called a scripting language.
1) Python operation control
The sys package is used to manage the environment in which Python itself runs. Python is an interpreter (interpreter) and a program that runs on the operating system. We can use the sys package to control many parameters of the program, such as the memory occupied by Python and CPU, the path to be scanned by Python, and so on. Another important function is to interact with Python's own command line, reading commands and arguments from the command line.
2) operating system
If Python makes up a small world, then the operating system is the big world that surrounds it. Python's interaction with the operating system allows Python to manage the whole world in its own small world.
The os package is the interface between Python and the operating system. We can use the os package to implement many functions of the operating system, such as managing system processes, changing the current path (equivalent to 'cd'), changing file permissions, and so on. However, it should be noted that the os package is built on the platform of the operating system, and many functions cannot be implemented on Windows systems. In addition, when using os packages, it is important to note that some of these functions have been replaced by other packages.
We manage the files stored on disk through the file system. Finding, deleting, copying files, and listing files are all common file operations. These functions can often be seen in the operating system (such as ls, mv, cp and other Linux commands), but now they can be implemented within Python through the glob package, shutil package, os.path package, and some functions of the os package in the Python standard library.
The subprocess package is used to execute external commands, which is equivalent to entering commands on the command line of the operating system to execute. For example, the common system command 'ls' or' cd', can also be any program that can be executed on the command line.
4) Thread and process
Python supports multithreading (threading package) and multi-process (multiprocessing package) operation. Through multi-thread and multi-process, the utilization rate of system resources and the processing speed of computer can be improved. Python comes with related communication and memory management tools in these packages. In addition, Python also supports signal systems similar to UNIX to achieve rough signal communication between programs.
The third category: network
Now, the strength of the network function largely determines the success of a language. You can feel this in Ruby, JavaScript, php. The standard library of Python does not fully support Internet development, which is the starting point of Python-based projects such as Django: to enhance the application function of Python in the network. These projects have been very successful, which is one of the reasons why many people are willing to learn Python. It should be noted, however, that these Python-based projects are also based on the Python standard library.
1) Network application based on socket layer
Socket is the bottom layer of the programmable part of the network. With the socket package, we can directly manage socket, such as assigning socket to a port (port), connecting to a remote port, and transferring data over a connection. We can also use the SocketServer package to set up the server more easily.
By cooperating with multi-thread and multi-process to establish a multi-thread or multi-process server, the working ability of the server can be effectively improved. In addition, asynchronous processing through asyncore packages is also a way to improve server performance.
2) Internet applications
In practical applications, many low-level details of the network (such as socket) are hidden by high-level protocols. The http protocol based on socket is actually easier and more frequently used. Http connects and communicates through the mode of request/responce, and the content of its information is easier to understand. In the Python standard library, there are http server-side and client-side application support (BaseHTTPServer package; urllib package, urllib2 package), and URL (URL actually indicates the location of network resources) can be understood and operated through the urlparse package.
This is the end of this article on "what are the functions of the Python standard library". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.