Python3 built-in module usage

[ TOC]

0 x00 Quick Start####

(1) List of built-in modules
Description: A module is a file that contains all the functions and variables you define. Its suffix is .py. The module can be failed to be imported to use the functions and other functions in the module.

#>>> dir(random)  #View and use functions in the module,Prerequisites must introduce modules,High-level usage import introduces modules as module aliases;
#>>> help(random) #Module help

import os        #Operating system module
import sys       #System module
import math      #Mathematical calculation module
import random    #Random number generation module
import pickle    #Data sequence and deserialization module
import time      #Time analysis module provides various time-related functions(time, datetime and calendar)import dlifflib  #File comparison module(Detailed explanation of Python automated operation and maintenance office)import filecmp   #File directory difference comparison method(Simultaneously)import pprint    #Format beautiful output
import requests   #Requests Sending network requests is very simple
import smtplib   #Mail sending module
import email     #Mail template package
import uuid      #UUID module

0 x01 module details####

os module#####

Description: How to use common functions of the os module about files/directories:
getcwd() returns the current working directory
chdir(path) change working directory
listdir(path='.') List the file names in the specified directory ('.' means the current directory,'..' means the upper-level directory)
mkdir(path) creates a single-level directory, and throws an exception if the directory already exists
makedirs(path) Recursively create multi-layer directories. If the directory already exists, an exception will be thrown. Note:'E:\a\b' and'E:\a\c' will not conflict
remove(path) delete file
rmdir(path) delete a single-level directory, throw an exception if the directory is not empty
removedirs(path) Recursively delete a directory, try to delete it layer by layer from the subdirectory to the parent directory, and throw an exception when the directory is not empty
rename(old, new) rename the file old to new
system(command) Run the shell command of the system, display the return after executing the command, and whether the return execution is successful 0/1
walk(top) traverse all subdirectories under the top path and return a triple: (path, [include directory], [include file])

The following are some definitions commonly used in supporting path operations, supporting all platforms (equivalent to constants)

os.curdir refers to the current directory ('.')
os.pardir refers to the upper level directory ('..')
os.Sep output operating system-specific path separator (under Win'\\', Under Linux'/')
os.linesep The line terminator used by the current platform (under Win'\r\n', Under Linux'\n')
os.name refers to the operating system currently in use (including:'posix','nt','mac','os2','ce','java')
os.popen(command) 执行指定的系统可执行脚本的command

os module case 1:

#! /usr/bin/python3
# Function: use of OS module

import os
print("Current path:",os.getcwd())
os.chdir('C:\\Users\\Administrator\\Desktop\\Python')  #Modify path
print("Modified path:",os.getcwd())
os.mkdir('.\\test')         #Create a directory
os.makedirs('.\\web\\a\\b')  #When the directory already exists, the directory and multi-level directories cannot be created
# os.remove('.\\demo2-10.txt')    #Delete Files
os.rmdir('.\\test')              #Remove a single directory
os.removedirs('.\\web\\a\\b')    #Remove multiple directories
print(os.system('DATE /T'))      #Successful execution returns 0, otherwise 1print(list(os.walk(os.curdir)))  #Traverse the current directory os.pardir()print('Specify the separator:',os.sep)print('Abort symbol for current platform: %s'%os.linesep)  #\r\n
print('The currently used operation attracts:',os.name)

OS module

**os module case 2: **
Description: Use os.popen to replace os.system to execute system commands;

#! /usr/bin/env python
# - *- coding:utf-8-*-

# Get the native open port Linux/Windwos
command ="netstat -an|findstr TCP"
command ="netstat -tnlp|egrep -i tcp|awk {'print $4'}|awk -F':' '{print $NF}'|sort"
lines = os.popen(command).readlines()  #key point
for line in lines:
 port = line.split()
 port_list.append(port[0])

command ="netstat -an|findstr TCP" 
lines = os.popen(command).readlines()  #key point
for line in lines:
 port=line.split()
 port=port[1].split(':')print(port[1])

# runfile('E:/githubProject/Study-Promgram/Python3/Day9/os.popen.py', wdir='E:/githubProject/Study-Promgram/Python3/Day9')
# 135
# 443
# 445
# 902

# Example 2.Get the IP address information of the machine
for k in os.popen("ip addr").readlines():print(k)
# 15: eth0:<BROADCAST,MULTICAST,UP> mtu 1500 group default qlen 1
#  link/ether 98:90:96:e1:91:d6
#  inet 192.168.1.88/24 brd 10.20.172.255 scope global dynamic
#  valid_lft 691196sec preferred_lft 691196sec
#  inet6 fe80::d97d:fe6c:10bf:4244/64 scope link dynamic
#  valid_lft forever preferred_lft forever
os.path class module######

Description: How to use common functions of os.path module path
basename(path) remove the directory path and return the file name separately
dirname(path) remove the file name and return the directory path separately
join(path1[, path2[, …]]) Combine each part of path1, path2 into a path name
split(path)
Split the file name and path, and return the (f_path, f_name) tuple. If the directory is completely used, it will also separate the last directory as the file name, and will not determine whether the file or directory exists
splitext(path) separates the file name and extension, and returns (f_name, f_extension) tuple
getsize(file) returns the size of the specified file in bytes
getatime(file)
Returns the most recent access time of the specified file (floating point type seconds, converted by the gmtime() or localtime() function of the time module)
getctime(file) returns the creation time of the specified file (floating point seconds, same as above)
getmtime(file) returns the latest modification time of the specified file (floating point seconds, same as above)

The following functions return True or False
exists(path) Determine whether the specified path (directory or file) exists
isabs(path) Determine whether the specified path is an absolute path
isdir(path) Determine whether the specified path exists and is a directory
isfile(path) Determine whether the specified path exists and is a file
islink(path) Determines whether the specified path exists and is a symbolic link
ismount(path) Determine whether the specified path exists and is a mount point
samefile(path1, paht2) Determine whether path1 and path2 point to the same file

Case:

#! /usr/bin/python3
# Function: OS module use

import os

print(os.path.basename('E:\\web\\conf\\test.php'))print(os.path.dirname('E:\\web\\conf\\test.php'))print(os.path.join('E:\\','test\\demo.php'))  #Note that path1 needs to add escape symbols in the absolute path
print(os.path.split('E:\\web\\conf\\test.php'))print(os.path.splitext('E:\\web\\conf\\test.php'))print(os.path.getsize(os.curdir +'\\python.txt'),'Bytes')print("Establishment time:",os.path.getctime(os.curdir +'\\python.txt'),'ms')print("Change the time:",os.path.getmtime(os.curdir +'\\python.txt'),'ms')print("interview time:",os.path.getatime(os.curdir +'\\python.txt'),'ms')if os.path.exists('.\\python.txt'):print("Directory or file exists")else:print('Directory or file does not exist')print("Whether it is an absolute path:",os.path.isabs('.\\python.txt'))print("Is it a file:",os.path.isfile('.\\python.txt'),"Is it a directory:",os.path.isdir(os.pardir+'\\python'))print("Determine whether it is a mount point:",os.path.ismount('D:\\'))

os.path module

sys module#####

Description: List of attributes and parameters
(1) Obtaining sys.argv CMD list parameters

import sys  #System module
sys.setdefaultenconding('utf-8|gb2312') #Set the system default encoding format
sys.version_info  #Current Python version information
# sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
math module

import math Python mathematical functions

Mathematical constant
pi Mathematical constant pi (pi, generally represented by π)
e Mathematical constant e, e is the natural constant (natural constant)

(1) abs(x) returns the absolute value of a number, such as abs(-10) returns 10
(2) ceil(x) returns the upward integer of the number, such as math.ceil(4.1) returns 5
(3) exp(x) returns e to the power of x (ex), such as math.exp(1) returns 2.718281828459045
(4) fabs(x) returns the absolute value of the number, such as math.fabs(-10) returns 10.0
(5) floor(x) returns the rounded integer of the number, such as math.floor(4.9) returns 4
(6) log(x) such as math.log(math.e) returns 1.0, math.log(100,10) returns 2.0
(6) log10(x) returns the logarithm of x based on 10, such as math.log10(100) returns 2.0
(7) max(x1, x2,...) returns the maximum value of a given parameter, which can be a sequence.
(8) min(x1, x2,...) returns the minimum value of a given parameter, which can be a sequence.
(9) modf(x) returns the integer part and the decimal part of x. The numerical symbols of the two parts are the same as x, and the integer part is represented by floating point (stored as a tuple).
(10) pow(x, y) The value of x**y after operation (ie exponentiation).
(11) round(x [,n]) returns the rounded value of the floating-point number x. If n is given, it represents the number of digits rounded to the decimal point.
(12) sqrt(x) returns the square root of the number x.
(13) sum(iterable[,start-0]) returns the sum of the sequence iterable and the optional parameter start (requires consistent data types)

Trigonometric functions of mathematics:
(13) acos(x) returns the arc cosine of x in radians.
(14) asin(x) returns the arc sine of x in radians.
(15) atan(x) returns the arc tangent of x in radians.
(16) atan2(y, x) returns the arctangent of the given X and Y coordinates.
(17) cos(x) returns the cosine of x in radians.
(18) hypot(x, y) returns the Euclidean norm sqrt(xx + yy).
(19) sin(x) returns the sine of x radians.
(20) tan(x) returns the tangent of x radians.
(21) degrees(x) converts radians to angles, such as degrees(math.pi/2), returns 90.0
(22) radians(x) convert angle to radians

Python math function module case:

#! /usr/bin/python3
# coding:utf-8
# Function: Mathematical function verification
import math  #Need to import math packages

# constant
print(math.pi,math.e)  #3.1415926535897932.718281828459045

# max /min to determine the maximum value/Minimum
print(max(1,2))  #2print(min([1,2,3],[2,3,4])) #[1,2,3]print(math.ceil(1.2))  #2print(math.floor(1.2)) #1
tup = math.modf(1.8)print("math.modf(1.8)",tup[1],tup[0])  #1.00.8print(pow(5,2)) # 25print(round(8.5)) #Rounding (note that when the decimal part is only 5, it is still discarded, the integer part is not rounded) 8print(round(8.51))   #9print(math.sqrt(10)) #3.1622776601683795

# Tuple or sequence (list) carton
tuple1 =(1.1,2.2,3.3)print(sum(tuple1))       # 6.6print(sum(tuple1,4.4))   #6.6+4.4=11.0
random module#####

Random numbers can be used in the fields of mathematics, games, security, etc., and are often embedded in algorithms to improve algorithm efficiency and improve program security.
import random

(1) choice(seq) randomly select an element from the elements of the sequence, such as random.choice(range(10)), randomly select an integer from 0 to 9.
(2) randrange ([start,] stop [,step]) Obtain a random number from the set that is incremented by the specified base within the specified range, the base default is 1
(3) random() randomly generates the next real number, which is in the range [0,1).
(4) seed([x]) Change the seed of the random number generator. If you don't understand the principle, you don't need to set (5)seed specially, Python will help you choose the seed.
(6) shuffle(lst) randomly sort all elements of the sequence
(7) uniform(x, y) randomly generates the next real number, which is in the range of [x,y].

Case:

#! /usr/bin/python3
# coding:utf-8
# Function: random number function
import random  #Load module

print(random.choice(range(0,10)))  #0~10 random int
print(random.randrange(0,100))     #0~100 random number int
print(random.random())                 #0~1 Randomly generated 0.47044481738738064 float
print(random.uniform(0,10))          #0~1 Randomly generated 13.47044481738738064 float
random.seed()   #Random number seed

list =[1,2,3,4,5,6]print(list)  #[1,2,3,4,5,6]
random.shuffle(list)  #Randomly sort all elements of the sequence
print(list) #[2,4,5,1,3,6]
pickle module

Use some algorithms to "paste" your data object into a binary file; note that you must use the'wb' mode to open the file first;
(1) pickle.dump(data, file) # The first parameter is the data object to be stored, and the second parameter is the file object to be stored
(2) pickle.load(file) # The parameter is the file object stored by the target

time module#####

Modules related to time processing in Python include: time, datetime and calendar

List of methods:
gmtime() Convert to display Greenwich time
localtime() Convert display local time
strptime() #Return in the form of time ancestor (struct_time).

# Index Attribute value (Values)
0 	 tm_year (for example: 2015)
1 	 tm_mon (month) 1~122	 tm_mday (day) 1~313	 tm_hour (hour) 0~234	 tm_min (minutes) 0~595	 tm_sec (seconds) 0~61 #(See note 1 below)
6 	 tm_wday (day of the week) 0~6 (0 means Monday)
7 	 tm_yday (day of the year) 1~3668	 tm_isdst (whether it is daylight saving time) 0, 1,-1(-1 represents daylight saving time)
# Note 1: The range is really 0~61 (you read it right^_^); 60 represents a leap second, 61 is reserved for historical reasons
  1. time.altzone returns the offset seconds of the daylight saving time zone in the west of Greenwich; if the zone is in the east of Greenwich, it will return a negative value (such as Western Europe, including the United Kingdom); it can only be used for areas where daylight saving time is enabled.

  2. time.asctime([t]) accepts a time tuple and returns a readable form of 24 characters in the form of "Tue Dec 11 18:07:14 2015" (Tuesday, December 11, 2015, 18:07:14) String.

  3. time.clock() returns the current CPU time in seconds calculated with floating point numbers. Used to measure the time consumption of different programs, more useful than time.time().
    Python 3.3 or later is not recommended. Because this method depends on the operating system, it is recommended to use perf_counter() or process_time() instead (one returns the system running time and the other returns the process running time, please choose according to actual needs)

  4. time.ctime([secs]) is equivalent to asctime(localtime(secs)), and no parameter is equivalent to asctime()

  5. time.gmtime([secs]) receives the time stop (the number of floating-point seconds elapsed since the 1970 epoch) and returns the time tuple t in Greenwich astronomical time (Note: t.tm_isdst is always 0)

  6. time.daylight If daylight saving time is defined, the value is non-zero.

  7. time.localtime([secs]) receives the time stop (the number of floating-point seconds elapsed since the 1970 epoch) and returns the time tuple t in the local time (t.tm_isdst can be 0 or 1, depending on whether the local daylight saving time is )

  8. time.mktime(t) accepts a time tuple and returns the time stop (floating point number of seconds elapsed since the 1970 epoch)

  9. time.perf_counter() returns the precise time of the timer (the running time of the system), including the sleep time of the entire system. Since the reference point of the return value is undefined, only the difference between the results of successive calls is valid.

  10. time.process_time() returns the sum of the CPU execution time of the current process, excluding sleep time. Since the reference point of the return value is undefined, only the difference between the results of successive calls is valid.

  11. time.sleep(secs) Delays the running of the calling thread. The unit of secs is seconds.

  12. time.strftime(format[, t]) converts a tuple representing time or struct_time (such as returned by time.localtime() and time.gmtime()) into a formatted time string.
    If t is not specified, time.localtime() will be passed in. If any element in the tuple is out of bounds, a ValueError will be thrown.

# date, datetime,And time objects support strftime(format)Method to convert the specified date or time into a custom format string
>>> from datetime import datetime
>>> dt = datetime.now()>>>print('(%Y-%m-%d %H:%M:%S %f): ', dt.strftime('%Y-%m-%d %H:%M:%S %f'))(%Y-%m-%d %H:%M:%S %f):2014-08-3123:54:58379804>>>print('(%Y-%m-%d %H:%M:%S %p): ', dt.strftime('%y-%m-%d %I:%M:%S %p'))(%Y-%m-%d %H:%M:%S %p):14-08-3111:54:58 PM
>>> print('%%a: %s '% dt.strftime('%a'))%a: Sun 
>>> print('%%A: %s '% dt.strftime('%A'))%A: Sunday 
>>> print('%%b: %s '% dt.strftime('%b'))%b: Aug 
>>> print('%%B: %s '% dt.strftime('%B'))%B: August 
>>> print('Date time%%c: %s '% dt.strftime('%c'))
Date time%c:08/31/1423:54:58>>>print('date%%x:%s '% dt.strftime('%x'))
date%x:08/31/14>>>print('time%%X:%s '% dt.strftime('%X'))
time%X:23:54:58>>>print('Today is the first of this week%s days'% dt.strftime('%w'))
Today is the 0th day of the week
>>> print('Today is the first of this year%s days'% dt.strftime('%j'))
Today is the 243rd day of this year
>>> print('This week is the first of the year%s week'% dt.strftime('%U'))
This week is the 35th week of the year

WeiyiGeek.format formatting parameter table

Time module case:

# Time zone difference
print("Green:",time.gmtime(os.path.getctime(os.curdir +'\\python.txt')))print("local:",time.localtime(os.path.getctime(os.curdir +'\\python.txt')))>>> time.localtime()
# Local: time.struct_time(tm_year=2019, tm_mon=4, tm_mday=7, tm_hour=17, tm_min=26, tm_sec=46, tm_wday=6, tm_yday=97, tm_isdst=0)
# Green: time.struct_time(tm_year=2019, tm_mon=3, tm_mday=2, tm_hour=6, tm_min=45, tm_sec=39, tm_wday=5, tm_yday=61, tm_isdst=0)

#! /usr/bin/python
# - *- coding:UTF-8-*-
# Function: use of time module

import time as t  

print("####sleep -Delay 3s execution!###")
# t.sleep(3)

# Convert a formatted time string to struct_time 
print(t.strftime("%a %b %Y %H:%M:%S +0000", t.localtime()))print(t.strftime("%A %B %Y %H:%M:%S +0000", t.gmtime()))

# Accept a time tuple and return a readable form
print("asctime() ",t.asctime(t.localtime()))

# Function equivalent to asctime(localtime(secs)), No parameter is equivalent to asctime()print("ctime() ",t.ctime())

# Accept time tuple and return time stop
print("mktime()Timestamp of the specified time",t.mktime(t.localtime()))

# Returns the timestamp of the current time (the number of floating-point seconds that have passed since the 1970 epoch)
print("time()Current timestamp:",t.time()) 

# The number of seconds offset from Greenwich in the local time zone (daylight saving time is not activated) (Americas>0; most of Europe, Asia, Africa<=0)
print("timezone offset in seconds from Greenwich:",t.timezone)

# Returns the offset seconds for the daylight saving time zone west of Greenwich
print("altzone returns the offset in seconds for the summer time zone west of Greenwich:",t.altzone)

# Return the precise time of the timer (system running time)
print("perf_counter()The precise time of the timer",t.perf_counter())

# Returns the sum of the CPU execution time of the current process, excluding sleep time
print("process_time()The sum of the CPU time of the current process",t.process_time())

# A tuple containing two strings: the first is the name of the local non-daylight saving time zone, and the second is the name of the local DST time zone.
print(t.tzname)  #Garbled

####### sleep -Delay 3s execution!######
# > python demo3.14.py 
# Sun Apr 201917:04:21+0000
# Sunday April 201909:04:21+0000
# asctime()  Sun Apr  717:04:212019
# ctime()  Sun Apr  717:04:212019
# mktime()Timestamp of the specified time 1554627861.0
# time()Current timestamp:1554627861.0564845
# timezone offset in seconds from Greenwich:-28800
# altzone returns the offset in seconds for the summer time zone west of Greenwich:-32400
# perf_counter()The precise time of the timer 0.612054762
# process_time()The sum of the time the current process executes the CPU 0.59375
1 datatime Detailed######

Description: The datetime module provides various classes for manipulating date and time. The module focuses on efficient formatted output

**The datetime module defines two constants: **

The classes defined in the datetime module (the first four are explained in detail below):

# The affiliation of these classes above:
object
 timedelta
 tzinfo
  timezone
 time
 date
  datetime

(1) timedelta object
timedelta object represents the interval between two dates or times

Ranges:

## All parameters are optional (default is 0-Inside are attributes), the parameters can be integers or floating point numbers, positive or negative numbers.
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)1 millisecond ->1000 microseconds
1 minutes ->60 seconds
1 hours ->3600 seconds
1 weeks ->7 days
0<= microseconds <10000000<= seconds <3600*24 (seconds in 1 hour*24 hours)
- 999999999<= days <=999999999

timedelta case:

>>> from datetime import timedelta
>>> year =timedelta(days=365)>>> another_year =timedelta(weeks=40, days=84, hours=23, minutes=50, seconds=600)  # adds up to 365 days
>>> year.total_seconds()31536000.0>>> year == another_year
True
>>> ten_years =10* year
>>> ten_years, ten_years.days // 365(datetime.timedelta(3650),10)>>> nine_years = ten_years - year
>>> nine_years, nine_years.days // 365(datetime.timedelta(3285),9)>>> three_years = nine_years // 3;>>> three_years, three_years.days // 365(datetime.timedelta(1095),3)>>>abs(three_years - ten_years)==2* three_years + year
True

(2) date object
The object represents a date. In an idealized calendar, the date is composed of year (year), month (month), and day (day)
Ranges:

datetime.date(year, month, day)

MINYEAR <= year <=MAXYEAR (that is, 1~9999)
1<= month <=121<= day <=Determine based on year and month (for example, February 2015 has only 28 days)
# The following is an example of calculating the number of days:
>>> import time
>>> from datetime import date
>>> today = date.today()>>> today
datetime.date(2014,8,31)>>> today == date.fromtimestamp(time.time())  #True
>>> my_birthday =date(today.year,6,24)>>>if my_birthday < today:
  my_birthday = my_birthday.replace(year = today.year +1)   #datetime.date(2015,6,24)>>> time_to_birthday =abs(my_birthday - today)>>> time_to_birthday.days
297>>> d = date.fromordinal(735678)  #Since date 1.1.735678 day after 0001
>>> d
datetime.date(2015,3,21)>>> t = d.timetuple()>>>for i in t:print(i,end='')2015321000580-1>>> ic = d.isocalendar()>>>for i in ic:print(i)2015126>>> d.isoformat()'2015-03-21'>>> d.strftime("%d/%m/%y")'21/03/15'>>> d.strftime("%A %d. %B %Y")'Saturday 21. March 2015'>>>'The {1} is {0:%d}, the {2} is {0:%B}.'.format(d,"day","month")'The day is 21, the month is March.'

(3) time object
The time object represents a time of day and can be adjusted through the tzinfo object;
Ranges:

# The parameter inside is the attribute of the instance but the read-only time.hour
datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None)0<= hour <240<= minute <600<= second <600<= microsecond <1000000
Note: If the parameter is out of range, ValueError will be raised

time.isoformat()- returns a date string in ISO 8601 format, such as "HH:MM:SS.mmmmmm"

time.str()- For the time object t, str(t) is equivalent to t.isoformat()

time.strftime(format)- returns a custom formatted string representing the time, as detailed below

time.format(format)-Same as time.strftime(format), this allows the time object string to be specified when calling str.format()

time.utcoffset()-If the tzinfo attribute is None, return None; otherwise return self.tzinfo.utcoffset(self)

time.dst()-If the tzinfo attribute is None, return None; otherwise return self.tzinfo.dst(self)

time.tzname()-If the tzinfo attribute is None, return None; otherwise return self.tzinfo.tzname(self)

# Learn programming, to fish C
>>> from datetime import time, timedelta, tzinfo
>>> classGMT1(tzinfo):
  def utcoffset(self, dt):returntimedelta(hours=1)
  def dst(self, dt):returntimedelta(0)
  def tzname(self, dt):return"Europe/Prague">>> t =time(14,10,30, tzinfo=GMT1())>>> t
datetime.time(14,10,30, tzinfo=<__main__.GMT1 object at 0x02D7FE90>)>>> gmt =GMT1()>>> t.isoformat()'14:10:30+01:00'>>> t.dst()
datetime.timedelta(0)>>> t.tzname()'Europe/Prague'>>> t.strftime("%H:%M:%S %Z")'14:10:30 Europe/Prague'>>>'The {} is {:%H:%M}.'.format("time", t)'The time is 14:10.'
2 timeit detailed######

The timeit module accurately measures the execution time of a small piece of code. It can be used directly on the command line interface or called by importing a module. The module flexibly avoids errors that are easy to occur when measuring execution time.

This module defines three utility functions and a public class.

# Command line interface syntax
python -m timeit [-n N][-r N][-s S][-t][-c][-h][statement ...](1)timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000)  #Create a Timer instance, the parameters are stmt (statement or function that needs to be measured), setup (initialization code or import statement of the build environment), timer (timing function), number (the number of times the statement is executed in each measurement)(2) timeit.repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000)  #repeat (number of repeated measurements)(3)timeit.default_timer() #The default timer, usually time.perf_counter()The method can provide the highest precision timer on any platform (it only records natural time, and recording natural time will be affected by many other factors, such as computer load).(4)classtimeit.Timer(stmt='pass', setup='pass', timer=<timer function>)  #A class for calculating the execution speed of a small piece of code. Both the first two parameters can contain multiple statements. Use a semicolon (;) Or separated by a new line.
# The stmt and setup parameters can also be objects that can be called but have no parameters. This will nest them in a timing function, and then be timeit()Executed. Note that due to additional calls, the timing overhead will be relatively small

## timeit attribute
- timeit(number=1000000) #Function: Calculate the execution time of the statement number times
- repeat(repeat=3, number=1000000)  #Function: call timeit repeatedly()-print_exc(file=None)  #Function: Output the traceback of timing code (Traceback)
# The advantage of standard backtracking is that in the compiled template, the source sentence line will be displayed. The optional file parameter specifies the location where the traceback will be sent, the default is to send to sys.stderr。

WeiyiGeek. Command line parameters

# Command Line
> python -m timeit -s "for i in range(10): oct(i)"-p
50000000 loops, best of5:6.25 nsec per loop

> python -m timeit -s "for i in range(10): oct(i)"-r 1-n 11 loop, best of1:365 nsec per loop

# IDLE call execution
>>> import timeit
>>> timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)0.8187260627746582>>> timeit.timeit('"-".join(map(str, range(100)))', number=10000)0.5858950614929199>>> timeit.Timer('for i in range(10): oct(i)','gc.enable()').timeit()0.9954580070000247>>> timeit.timeit('text.find(char)', setup='text = "I love FishC.com!"; char = "o"')1.7246671520006203

# actual case
#! /usr/bin/python
def test():"""Stupid test timeit module function"""
 L =[i for i inrange(1000)]if __name__ =='__main__':import timeit   #Import module
 t = timeit.Timer()try:print("execution time:",end=" ")print(timeit.timeit(stmt="test()", setup="from __main__ import test",number=100))
 except Exception:print('Exception thrown:')
  t.print_exc()
# > python demo6.2.py
# Execution time: 0.0026121469999999952

Precautions:

pprint module

Description: Used to beautify the printed output data, very practical;

Use Cases:

# Example 1.
sys.path
[' D:\\Program Files\\JetBrains\\PyCharm2019\\helpers\\pydev','D:\\Program Files\\JetBrains\\PyCharm2019\\helpers\\pycharm_display','D:\\Program Files\\JetBrains\\PyCharm2019\\helpers\\third_party\\thriftpy','E:\\githubProject\\Study-Promgram\\Python3','E:/githubProject/Study-Promgram/Python3']
pprint.pprint(sys.path) #Beautiful output
[' D:\\Program Files\\JetBrains\\PyCharm2019\\helpers\\pydev','D:\\Program Files\\JetBrains\\PyCharm2019\\helpers\\pycharm_display','D:\\Program Files\\JetBrains\\PyCharm2019\\helpers\\third_party\\thriftpy','E:\\githubProject\\Study-Promgram\\Python3','E:/githubProject/Study-Promgram/Python3']
requests module#####

Description: Using Requests to send network requests is very simple, just import the module;
Reference: https://2.python-requests.org//zh_CN/latest/user/advanced.html#advanced

Module method:

#1. get custom request header/Request parameter/Set cookies/Disable redirection
# SSL authentication can be set to True by default.
# The verify option only applies to host certificates. verify incoming CA_The path of the BUNDLE file, or the path of the folder containing the trusted CA certificate file
# Client certificate: Specify a local certificate as the client certificate, which can be a single file (including the key and certificate) or a tuple containing two file paths
# After making a network request, the response body will be downloaded immediately. This behavior can be overridden by the stream parameter to delay downloading the response body until the Response is accessed.content attribute
r = requests.get(url, headers={'user-agent':'my-app/0.0.1'},param={'key1':"value"}, cookies=dict(cookies_are='working'),allow_redirects=False,timeout=0.001,verify=True,verify='/path/to/certfile',cert=('/path/client.cert','/path/client.key'),stream=Truem,proxies ={"http":"http://10.10.1.10:3128","https":"http://10.10.1.10:1080",})
# Note: In addition to the basic HTTP proxy, Request also supports SOCKS protocol proxy pip install requests[socks],The method of use is as follows
# proxies ={
#  ' http':'socks5://user:[email protected]:port',
#  ' https':'socks5://user:[email protected]:port'
# }

#2. post request
r = requests.post("http://httpbin.org/post", data={'key1':'value1','key2':'value2'})
r = requests.post("https://api.github.com/some/endpoint", data=json.dumps(payload)) #Github API v3 accepts POST encoded as JSON/PATCH data

#3. Session object
s = requests.Session()
r = s.get('http://httpbin.org/cookies', cookies={'from-my':'browser'})
# or
with requests.Session()as s:
 s.get('http://httpbin.org/cookies/set/sessioncookie/123456789') #Ensure that the session can be closed after the with block exits, even if an exception occurs.

Module attributes:

requestsOBJ.status_code :Response status code requests.codes.ok a built-in status code query object
requestsOBJ.headers :Server response header
requestsOBJ.url :The query string of the URL requested by the object
requestsOBJ.text :Read the content of the server response,Automatically decode the content from the server, most unicode character sets can be decoded seamlessly
requestsOBJ.content :Access the request response body in bytes for non-text requests(Binary), Automatically decode gzip and deflate transmission coded response data for you
requestsOBJ.encoding: View what encoding is used by Requests to set the parsing encoding r.encoding ='ISO-8859-1'
r.history :Redirection and request history,Except HEAD by default,Requests will automatically handle all redirects.
r.raw #Fetch the raw socket response from the server, then you can access r.raw,The premise is that stream is set in the initial request=True can also use r.raw.read(10)
r.json() #There is also a built-in JSON decoder in Requests to help you process JSON data
r.cookies['example_cookie_name']  #Get the cookies returned by the website

actual case:

# Example 1.Various requests get/post/put/delete/head/options etc.;>>> r = requests.get('https://api.github.com/events',params ={'key1':'value1','key2':'value2'}) #Pass URL parameters
>>> r = requests.post('http://httpbin.org/post', data ={'key':'value'})>>> r = requests.put('http://httpbin.org/put', data ={'key':'value'})>>> r = requests.delete('http://httpbin.org/delete')>>> r = requests.head('http://httpbin.org/get')>>> r = requests.options('http://httpbin.org/get')

# Example 2.POST a multi-part code(Multipart-Encoded)document
>>> url ='http://httpbin.org/post'>>> files ={'file':open('report.xls','rb'),'application/vnd.ms-excel',{'Expires':'0'}}>>> files ={'file':('report.csv','some,data,to,send\nanother,row,to,send\n')} #You can also send a character string received as a file
>>> r = requests.post(url, files=files)

# Example 3.POST multiple files with block encoding
# To achieve this, just set the file to a list of tuples, where the tuple structure is(form_field_name, file_info):<input type="file" name="images" multiple="true" required="true"/>>>> url ='http://httpbin.org/post'>>> multiple_files =[('images',('foo.png',open('foo.png','rb'),'image/png')),('images',('bar.png',open('bar.png','rb'),'image/png'))]>>> r = requests.post(url, files=multiple_files)>>> r.text

# Example 3.The returned object of Cookie is RequestsCookieJar, which behaves like a dictionary, but the interface is more complete, suitable for cross-domain and cross-path use.
>>> jar = requests.cookies.RequestsCookieJar()>>> jar.set('tasty_cookie','yum', domain='httpbin.org', path='/cookies')>>> jar.set('gross_cookie','blech', domain='httpbin.org', path='/elsewhere')>>> url ='http://httpbin.org/cookies'>>> r = requests.get(url, cookies=jar)>>> r.text
'{" cookies": {"tasty_cookie": "yum"}}'

# Example 4.Request website and return information
def get(url):try:
 r = requests.get(url)
 except ConnectionError as e:print("[*] Error = "+str(e))exit(0)
 except TimeoutError as e:print("[*] Time = "+str(e))exit(1)
 except Exception as e:print("[*] Other Error = "+str(e))exit(2)print("URL:",r.url,"\nHeader:",end="")pprint(r.headers)print("Status:",r.status_code)print("Encoding:",r.encoding)
 r.encoding ="utf-8"  #Output content utf8 encoding to prevent garbled characters at noon
 print(r.history)

# Execution result information#
# Connected to pydev debugger(build 191.7479.30)
# URL: http://127.0.0.1:4000/archives/ 
# Header:{'X-Powered-By':'Hexo','Content-Type':'text/html','Date':'Fri, 12 Jul 2019 07:21:36 GMT','Connection':'keep-alive','Transfer-Encoding':'chunked'}
# Status: 200
# Encoding: ISO-8859-1
# []
# ('<! DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" '

Recommended Posts

Python3 built-in module usage
Python3 module
Python learning os module and usage
Python Lesson 37-Module
Python built-in function -compile()
Python3 external module use
Detailed explanation of the usage of Python decimal module
Python3 built-in function table.md
Python advanced usage summary
Prettytable module of python
MongoDB usage in Python
Python high-order function usage summary!
matplotlib of python drawing module
Python Faker data forgery module
Python high-order function usage summary!
The usage of wheel in python
Analysis of usage examples of Python yield
Python novice learns to raise usage
Detailed usage of dictionary in Python
Usage of os package in python
Simple usage of python definition class
Python decorator simple usage example summary
​Full analysis of Python module knowledge
How to view the python module
The usage of tuples in python
Python requests module session code example
Python requests module cookie instance analysis
Error when installing Python module on Ubuntu
python Douban replaces pip to install python module
The usage of Ajax in Python3 crawler
Python novices learn standard library module naming
Comprehensive summary of Python built-in exception types
Summary of common operations of Python time module
Method of installing django module in python
How does Python handle the json module
How to learn the Python time module
Detailed usage of Python virtual environment venv