Python commonly used module introduction

  • 2020-04-02 14:26:01
  • OfStack

In addition to keywords and built-in types and functions (builtins), python provides more functionality through libraries (modules).

The commonly used libraries (modules) are as follows:

1) python runtime services

* copy: copy module provides the ability to make shallow and deep copies of compound objects (list, tuple, dict, custom class).

* pickle: the pickle module is used to serialize python objects into the bytes stream, making it suitable for storage to files, network transfers, or database storage. (pickling process has also been called serializing, marshalling or flattening, pickling and can be used to deserialize bytes flow for python objects).

* sys: the sys module contains variables and functions related to the python parser and environment.

* others: atexit, gc, inspect, marshal, traceback, types, warnings, weakref.

2) the mathematical

* a decimal: in python float using double binary floating-point coding to represent, this led to a decimal number can not be accurate, said actually memory of 0.100000000000000001 0.1, for example, there are 3 * 0.1 = = 0.3 is False. A decimal is in order to solve the similar problem, has a higher accuracy, can be said a wider range of Numbers, rounded and more accurately.

* math: the math module defines standard mathematical methods, such as cosine (x), sine (x), etc.

* random: the random module provides various methods for generating random Numbers.

* other: fractions, Numbers.

3) data structure, algorithm and code simplification

* array: an array represents an array, similar to a list, except that it can only store objects of the same type.

* bisect: bisect is an ordered list that USES bitsections internally to do most of the work.

* collections: the collections module contains high-performance implementations of some useful containers, abstract base classes for various containers, and functions to create name-tuple objects. Examples include containers deque, defaultdict, namedtuple, etc.

* heapq: heapq is a priority queue implemented using heap.

* itertools: itertools contains functions to create effective iterators. All functions return iterators or contain iterators (for example, a derivative and a derivative expression).

* operator: operator provides access to python's built-in operations and special methods provided by the parser, such as x+y for add (x, y), x+=y for iadd (x, y), a % b for mod (a, b), and so on.

* others: ABC, contextlib, functools.

4) string and text processing

*codecs: the codecs module is used to handle the conversion of different character encodings and unicode text IO.

* re: the re module is used to match and replace strings with regular expressions.

* string: the string module contains a number of useful constants and functions for handling strings. Also contains classes in the new string format.

* struct: the struct module is used to translate between python and binary structures.

* unicodedata: the unicodedata module provides access to the unicode character database

5) python database access

* relational databases have a common Specification Python Database API Specification V2.0, MySQL, Oracle, etc., all implement this Specification, and then add their own extensions.

Sqlite3: the sqlite3 module provides an interface for SQLite database access. SQLite database is a self-contained relational database in the form of a file or memory.

* dbm-style database modules: python provides typed modules to support UNIX dbm-style database files. The DBM module reads standard unix-dbm database files, GDBM reads GNU DBM database files, and dbhash reads Berkeley DB database files. All of these modules provide an object that implements a dictionary for string-based persistence, much like the dictionary dict, but whose keys and values must be strings.

* shelve: the shelve module USES special "shelf" objects to support persistent objects. This object behaves like dict, but all the objects it stores are stored on the hard disk using a hashtable-based database (dbhash, DBM, GDBM). The difference with the DBM module is that the objects stored are not only strings, but can be arbitrary pickle-compatible objects.

6) file and directory processing

* bz2: the bz2 module is used to process files compressed by the bzip2 compression algorithm.

* filecmp: the filecmp module provides functions to compare files and directories.

* fnmatch: the fnmatch module provides a UNIX shell-style wildcard to match file names. This module is just for matching, using glob to get a list of matching files.

* glob: the glob module returns all files in a directory that match the specified UNIX shell wildcard.

* gzip: the gzip module provides the GzipFile class for reading and writing files compatible with the GNUgzip program.

* shutil: the shutil module is used to perform higher level file operations such as copy, delete, and rename. Shutil operates on general files and does not support pipes, block devices, and other file types.

* tarfile: the tarfile module is used to maintain tarfile archive files. Tar has no ability to compress.

* tempfile: the tempfile module is used to generate temporary files and file names.

* zipfile: the zipfile module handles files in zip format.

* zlib, zlib module provides access to zlib library compression function.

7) operating system services

* cmmands: the commands module is used to execute simple system commands that are passed in as strings and return the output of the commands as strings. But this module is only available on UNIX systems.

The configParser module is used to read and write configuration files in the Windows configParser format.

* datetime, the datetime module provides various types to represent and process dates and times.

* errno, which defines the symbol names for all errorcodes.

* IO, the IO module implements various IO forms and the built-in open() function.

* logging, the logging module is flexible and easy to record events, errors, warnings, and debuging information to an application. This log information can be collected, filtered, written to a file or system log, or even sent over a network to a remote machine.

*mmap, the mmap module provides support for memory-mapped file objects, using memory-mapped files is similar to using normal files or byte strings.

* MSVCRT, MSCRT is only available on Windows systems and is used to access many useful features of the Visual C runtime library.

*optparse, the optparse module handles unix-style command-line options sys.argv at a higher level.

* OS, the OS module provides a portable interface to common operating system services. OS can be thought of as an abstraction of nt and posix. Nt provides the service interface for Windows, and posix provides the service interface for UNIX (Linux, MAC).

* os.path, os.path module handles path-related operations in a portable manner.

* signal, a module used for signal processing, is often associated with synchronization.

* subprocess, subprocess module contains functions and objects to uniformly create a new process, control the input and output flow of the new process, and handle the return of the process.

* time, time module provides a variety of time related functions. Commonly used time. Sleep ().

* winreg, the winreg module is used to operate the Windows registry.

* others: FCNTL.

8) threads and parallelism

* multiprocessing, the multiprocessing module provides subprocess to load multiple tasks, communicate, share data, and perform various synchronous operations.

* threading, the threading module provides a lot of synchronized methods to the thread class for multithreaded programming.

* queue, the queue module realizes various multi-producer, multi-consumer queues, which are used to realize the information security exchange of multi-threaded programs.

* others: Coroutines and Microthreading.

9) network programming and sockets

* asynchat, the asynchat module simplifies network asynchronous processing of applications by encapsulating asyncore.

* SSL, the SSL module is used to wrap socket objects using secure sockets layer (SSL), enabling data encryption and terminal authentication. Python USES openssl to implement this module.

* socketserver, the socketserver module provides types that simplify the implementation of socketserver in the TCP, UDP, and UNIX domains.

* other: asyncore, select.

10) Internet application programming

* ftplib, ftplib module to achieve the client side of the FTP protocol. This module is rarely used because urllib provides a more advanced interface.

* HTTP package, including the implementation of HTTP client and server and the module of cookies management.

* smtplib, smtplib contains the underlying interface of SMTP client, which is used to send mail using SMTP protocol.

* urllib, the urllib package provides an advanced interface to implement clients that interact with HTTP server, FTP server, and local files.

* XMLRPC, the XMLRPC module is implemented as an xml-rpc client class.

11) web programming

* cgi, the cgi module is used to implement cgi scripts, cgi programs are generally executed by the webserver, to process user input in the form, or to generate some dynamic content. When a request with a cgi script is submitted, the webserver executes the cgi as a child process, and the cgi program gets input through sys.stdin or environment variables, and output through sys.stdout.

* webbrowser, the webbrowser module provides platform-independent utility functions to open documents using webbrowser.

* others: wsgiref/WSGI (Python Web Server Gateway Interface).

12) Internet data processing and coding

Base64, base64 module provides base64, base32, base16 encoding, used to achieve binary and text encoding and decoding. Base64 is often used to encode binary data to be embedded in mail or HTTP protocols.

* binascii, the binascii module provides a low-level interface for the conversion of binary and various ASCII codes.

* CSV, the CSV module is used to read and write comma-separated values (CSV) files.

* email, the email package provides a number of functions and objects that are represented using the MIME standard to parse and maintain email messages.

* hashlib, the hashlib module implements various secure hash and message digest algorithms, such as MD5 and SHA1.

* htmlparser (html.parser), this module defines htmlparser to parse HTML and XHTML documents. To use this class, you need to define your own class and inherit from HTMLParser.

* json, the json module is used to serialize Javascript object notation (json) objects using class serialization or rice serialization.

* XML, the XML package provides a variety of ways to process XML.


Related articles: