Python Standard library--a Quick Walkthrough

Source: Internet
Author: User
Tags mathematical constants mathematical functions network function pickle package what date

Vamei source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

Python has a very useful standard library. The standard library will be installed in your computer along with the Python interpreter. It is a part of Python. These standard libraries are a great tool that Python has prepared for you to make programming more Effective.

Based on my personal experience, I will pick out the package description of the three aspects of the standard library:

    • Python Enhancements
    • System interaction
    • Internet

First Class: python Enhancements

Some of the existing features of Python itself can be enhanced with the use of standard Libraries.

1) word processing

The Python string class provides a way to manipulate strings. further, python can use regular expressions (regular Expression) to process strings through the RE package in the standard library.

A regular expression is a string Template. Python can search for parts of the template from a character, or replace this part with something Else. For example, you can search for all the numbers in a Text. The key to regular expressions is to make up the template according to your NEEDS.

In addition, the Python standard library provides a richer format for the output of strings, such as String packets, textwrap Packets.

2) Data Objects

Different data objects, suitable for different occasions of the organization and management of Data. Python's standard library defines data objects other than tables and dictionaries, such as arrays (array), Queues. A python user familiar with data structure can find the data structure they need in these Packages.

In addition, we often use copy packages to copy Objects.

3) Date and time

The management of dates and times is not complicated, but it is easy to make Mistakes. Python's Standard library has a pretty good management of dates and times (using time packages to manage dates and times with datetime packages), you can not only make date-time queries and transformations (such as July 18, 2012, which corresponds to the day of the week), but also perform calculations on datetime ( For example, 2000.1.1 13:00 of 378 hours after what date, what time). With these standard libraries, you can also control the text format of the datetime output as needed (e.g., output ' 2012-7-18 ' or ' 2012 ')

4) Mathematical Operations

In the standard library, Python defines a number of new numeric types (decimal packages, fractions packages) to compensate for possible deficiencies in the previous numeric type (integer, float). The standard library also contains the random packet, which is used to deal with the functions associated with stochastic numbers (generating random numbers, random sampling, etc.). The math package complements some important mathematical constants and mathematical functions such as pi, trigonometric, and so On.

(although NumPy is not a package in the standard library, its well-supported array operations allow it to be used fairly extensively in python-based research and computing, with due attention.) )

5) Storage

Earlier in our quick tutorial, only the input and output of the text were Mentioned. In fact, python can enter or output arbitrary objects. These objects can be converted into binary format (binary) through the pickle package in the standard library, then stored in a file, or they can be read backwards from a binary file.

In addition, Basic database functionality (sqlite3 Package) is also supported in the standard library. XML and csv-formatted files also have corresponding processing Packages.

  

Type Ii: System interaction

System interaction, mainly refers to the interaction between Python and the operating system (operate system), the file System. Python can implement many of the features of an operating System. It can manage the operating system like a bash script, which is why Python is sometimes a scripting Language.

1) Python Run control

SYS package is used to manage the operating environment of Python itself. Python is an interpreter (interpreter) and a program that runs on the operating System. We can use the SYS package to control many of the parameters that the program runs, such as the memory and CPU that Python can occupy, the path that Python will scan, and so On. Another important feature is interacting with Python's own command line, reading commands and parameters from the command line.

2) Operating System

If Python is a small world, then the operating system is the world that surrounds it. Python's interaction with the operating system allows Python to manage the entire world in its own small universe.

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 the system process, changing the current path (equivalent to ' CD '), changing the file permissions, etc., established. note, however, that the OS package is built on the platform of the operating system, and many features are not possible on Windows Systems. In addition, in the use of the OS package, Note that some of these features have been replaced by other Packages.

We use the file system to manage the files stored on the Disk. finding, deleting, Copying files, and listing file lists are common file Operations. These features can often be seen in the operating system (e.g., Linux commands such as ls, mv, cp, etc.), but can now be implemented inside Python through Python's glob package, shutil package, os.path package, and some functions of the OS Package.

The subprocess package is used to execute external commands that function as if we entered commands on the command line of the operating system, such as the common system command ' ls ' or ' CD ', or any program that can be executed at the command Line.

4) Threads and processes

Python supports multi-threaded (threading Package) runs and multi-process (multiprocessing Package) runs. Through multi-threading and multi-process, the utilization of system resources can be improved and the processing speed of computer is Improved. Python is included in these packages with relevant communication and memory management Tools. In addition, Python supports unix-like signal systems to achieve coarse signal communication between processes.

Category Iii: Network

now, the strength of the network function largely determines the success or not of a language. This can be felt from ruby, JavaScript, and Php. Python's Standard library is not fully supportive of Internet development, which is the starting point for python-based projects such as django: enhancing Python's application capabilities in the Web. These projects have been a great success and a big reason many people are willing to come 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

Sockets are the bottom line of the network programmability Section. With the socket package, we can manage the socket directly, such as assigning a socket to a port, connecting a remote port, and transferring data over a Connection. We can also use the Socketserver package to build servers more easily.

With multi-threaded and multi-process coordination, the establishment of multi-threaded or multi-process server, can effectively improve the server's ability to Work. In addition, asynchronous processing through the Asyncore package is also a solution to improve server Performance.

2) Internet Applications

In practical applications, many of the underlying details of the network (such as Sockets) are hidden by high-level protocols. The HTTP protocol built on top of the socket is actually easier and more often used. HTTP establishes a connection and communicates through Request/responce mode, and its information content is easier to understand. The Python standard library has HTTP server-side and client application support (basehttpserver packages, urllib packages, urllib2 packages) and can be understood and manipulated through the Urlparse package to the URL (where the URL actually explains where the network resource resides).

The above introduction is very rough, just hope to provide you with a knowledge of the standard library entrance. You are welcome to share the experience of using the standard library.

Python Standard library--a Quick Walkthrough

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.