[Python] Standard Library

Python has a powerful standard library. The core of the Python language only contains common types and functions such as numbers, strings, lists, dictionaries, and files, while the Python standard library provides additional functions such as system management, network communication, text processing, database interfaces, graphics systems, and XML processing. .

The main functions of the Python standard library are:

  • Text processing, including text formatting, regular expression matching, text difference calculation and merging, Unicode support, binary data processing and other functions
  • File processing, including file operations, creating temporary files, file compression and archiving, operating configuration files, etc.
  • Operating system functions, including thread and process support, IO reuse, date and time processing, calling system functions, logging, etc.
  • Network communication, including network sockets, SSL encrypted communication, asynchronous network communication and other functions
  • Network protocol, support HTTP, FTP, SMTP, POP, IMAP, NNTP, XMLRPC and other network protocols, and provide a framework for writing network servers
  • W3C format support, including HTML, SGML, XML processing.
  • Other functions, including internationalization support, mathematical operations, HASH, Tkinter, etc.

sys module

The sys module includes a set of very useful services, containing many functions and variables, used to process the configuration and resources of the Python runtime, so that it can interact with the system environment outside the previous program, such as the Python interpreter.

List of common functions of sys module:
Insert picture description here

os module

In automated testing, it is often necessary to find operating files, such as finding configuration files (thereby reading the information of the configuration files), finding test reports (thus sending test report emails), and often operating on a large number of files and a large number of paths. Depends on os module

In the Python interface, you can view the detailed introduction of the os module through the help() command, and press q to exit help()

import os

help(os)

os and its submodules contain many functions, here we introduce common submodules

(1) About the path submodule:

Insert picture description here

(2) About files/directories/attributes

Insert picture description here

(3) About environment variables

Insert picture description here
(4) About the process
Insert picture description here

math module

Digital processing module
Insert picture description here

random module

Generate random numbers
Insert picture description here

platform module

Get operating system details
Insert picture description here

time module

The time library provides various operation time values
Insert picture description here
strftime():
Insert picture description here
datetime module:
Insert picture description here

datetime.date() class:
Insert picture description here

datetime.datetime()类:
Insert picture description here

datetime.time()类:
Insert picture description here

JSON module

JSON is a lightweight data exchange format. Generally, the data returned by API is mostly JSON and XML. If JSON is returned, the acquired data will be converted into a dictionary for easy processing in the program.

There are two methods commonly used in the json library: dumps and loads():

Example:

1. Convert the dictionary to a JSON string

import json  
  
dict = {
    
    'user':[{
    
    'user1': 123}, {
    
    'user2': 456}]}  
type(dict)  
  
json_str = json.dumps(dict)  
type(json_str)  

2. Convert JSON string to dictionary

d = json.loads(json_str)  
  
type(d)  

Data type after JSON and Python decoding:

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45468845/article/details/108455137