current position:Home>Summary of the most complete Python Library in Station C - standard library + advanced library (10000 word long text, recommended Collection)

Summary of the most complete Python Library in Station C - standard library + advanced library (10000 word long text, recommended Collection)

2022-02-02 09:33:02 Don't eat tomatoes

Dreams are necessary , What if someone asks ?

author : Don't eat tomatoes

brief introduction :CSDN Blogger 、 Signed author of blue bridge 、 big data &Python Quality creators in the field .

CSDN Private confidence in me , reply 【 Information 】 receive :

1、100 Set of resume templates purchased by Xiaobian ;
2、1000 Set of high quality products PPT Templates ;
3、Python Basic and practical application ;
4、Java Basics 、 Senior and interview materials ;
5、 big data - Industry information ( Industry Summit 、 Technical direction );
6、 big data - Learning materials (300+ PDF);
7、 Theoretical knowledge of data warehouse and project practice ;
8、 machine learning & Recommendation system .


If the article can hit you , Please thumb up 、 For collection 、 Ask for comment

Last one ,《C The most complete station Python Standard library summary 》, It's on top 【 Comprehensive hot list of the whole station 】 and 【python Field hot list 】, To obtain the 2362 Like many times 、998 Comments 、2072 Secondary collection , Thank you guys .

Some fans left a message saying , I also want to see the common advanced Libraries , For pet powder and circle powder , Just add tomatoes Python Advanced library Use skills . I hope you like it ~


python Standard library mind map :


python Common high-level library mind map :


One 、python Common advanced Libraries


1、 Operating the database :MySQLdb

install MySQLdb, Please visit http://sourceforge.net/projects/mysql-python 

import MySQLdb
 
# Connect to database  
conn = MySQLdb.connect(host='localhost',user='root',passwd='xxxx',db='test1')

 
# obtain cursor Object to operate on
cursor = conn.cursor()

# Miss from your world ~
sql = "select * from world where someone like 'you' "


# perform sql
cursor.execute(sql)

2、Web Development : flask

 Forward all client requests to this program instance 
from flask import Flask
import datetime

server=Flask(__name__)

@server.route('/time',methods=['post','get'])
def get_time():
    now=str(datetime.datetime.now())# Convert the current time into a string 
    return " The current time is :%s"%now


server.run(port=8888)

3、 Web crawler :requests

Requests Allow you to send Pure natural Of HTTP/1.1 request , No manual labor is required . You don't need to manually set the URL Add query string , You don't need to be right POST Form code the data .Keep-alive and HTTP The function of connection pool is 100% Automated .

A simple example of a crawler :

import requests
heads = {}
heads['User-Agent'] = 'Mozilla/5.0 ' \
                          '(Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 ' \
                          '(KHTML, like Gecko) Version/5.1 Safari/534.50'

response = requests.get('http://www.baidu.com',headers=headers)

4、 operation execl:pandas

# -*- coding: utf-8 -*-
import xlrdimport xlwt
from datetime import date,datetime

def read_excel():
    #  Open file 
    workbook = xlrd.open_workbook(r'F:\demo.xlsx')
    #  Get all sheet
    print workbook.sheet_names() # [u'sheet1', u'sheet2']
    sheet2_name = workbook.sheet_names()[1]

    #  according to sheet Index or name get sheet Content 
    sheet2 = workbook.sheet_by_index(1) # sheet Index from 0 Start 
    sheet2 = workbook.sheet_by_name('sheet2')

    # sheet The name of , Row number , Number of columns 
    print sheet2.name,sheet2.nrows,sheet2.ncols

    #  Get values for entire rows and columns ( Array )
    rows = sheet2.row_values(3) #  Get the fourth line 
    cols = sheet2.col_values(2) #  Get the third column 
    print rows
    print cols

    #  Get cell contents 
    print sheet2.cell(1,0).value.encode('utf-8')
    print sheet2.cell_value(1,0).encode('utf-8')
    print sheet2.row(1)[0].value.encode('utf-8')
    
    #  Get the data type of cell content 
    print sheet2.cell(1,0).ctype

if __name__ == '__main__':
    read_excel()

5、 Data analysis :numpy

NumPy A function containing a large number of mathematical operations , Including trigonometric functions , Functions of arithmetic operations , Complex processing functions, etc .

NumPy Provides a variety of sorting methods . These sorting functions implement different sorting algorithms , Each sorting algorithm is characterized by its execution speed , Worst case performance , The required workspace and the stability of the algorithm . The following table shows a comparison of the three sorting algorithms .

Trigonometric functions

NumPy Standard trigonometric functions are provided :sin()、cos()、tan().

import numpy as np
 
a = np.array([0,30,45,60,90])
print (' Sine of different angles :')
#  By riding  pi/180  Into radians   
print (np.sin(a*np.pi/180))
print ('\n')
print (' The cosine of the angle in the array :')
print (np.cos(a*np.pi/180))
print ('\n')
print (' The tangent of the angle in the array :')
print (np.tan(a*np.pi/180))

6、 Data drawing analysis :Matplotlib

import numpy as np 
from matplotlib import pyplot as plt 
 
x = np.arange(1,11) 
y =  2  * x +  5 
plt.title("Matplotlib demo") 
plt.xlabel("x axis caption") 
plt.ylabel("y axis caption") 
plt.plot(x,y) 
plt.show()

In the example above ,np.arange() Function creation x The value on the axis .y The corresponding value on the axis is stored in another array object y in . These values use matplotlib Package's pyplot Sub module of plot() Function to draw .

The figure is created by show() Function shows .

7、 Scientific Computing :scipy

  scipy.special The special functions in the library are transcendental functions , The so-called transcendental function means that the relationship between variables cannot be added by finite times 、 reduce 、 ride 、 except 、 chengfang 、 prescribing The function represented by the operation . Such as trigonometric function in elementary function 、 Inverse trigonometric function and logarithmic function 、 Exponential functions are elementary transcendental functions , Generally speaking, non elementary functions are transcendental functions .

Elementary function : It refers to the function obtained from the basic elementary function through finite four operations and compound operations

8、 Vision and image processing :PIL(pillow)

Create a thumbnail

import os
from PIL import Image

image_path = 'python-logo.png'  #  Picture location 
size = (128, 128)  #  file size 
f, e = os.path.splitext(image_path)  #  Get the file name and suffix 
outfile = f + ".thumbnail"
if image_path != outfile:
    try:
        im = Image.open(image_path)
        im.thumbnail(size)  #  Set thumbnail size 
        im.save(outfile, "JPEG")
    except IOError:
        print("cannot convert", image_path)

Create a scrolling graphic

python Good study ,PS Also capable of !

from PIL import Image


def roll(image, delta):
    """  Scroll the image sideways  """
    xsize, ysize = image.size

    delta = delta % xsize
    if delta == 0: return image

    part1 = image.crop((0, 0, delta, ysize))
    part2 = image.crop((delta, 0, xsize, ysize))
    image.paste(part1, (xsize - delta, 0, xsize, ysize))
    image.paste(part2, (0, 0, xsize - delta, ysize))

    return image


if __name__ == '__main__':
    image_path = 'test.jpg'
    im = Image.open(image_path)
    roll(im, 300).show()  #  Roll sideways  300  Pixels 


9、 GUI development : PyQT

Tetris is the most popular computer game of all time .

The original game was designed and programmed by Russian programmer alexepakitnov in 1985 year . thereafter , Tetris is available on almost all computer platforms in many changes .
Tetris is called a building block puzzle . In this game , We have seven different shapes called tetrominoes:“s” shape ,Z-shape,t shape , One l Type space , A line ,MirroredL-shape And square . These shapes are formed with four squares . The shape is falling . Tetris game objects are moving and rotating shapes to make them fit as much as possible .

If we try to form a line , The bank destroyed our score . We until we play Tetris .

tetrominoes

PyQt5 Is a tool for creating applications . There are other libraries for creating computer games .

However ,PyQt5 And other application toolkits can be used to create simple games .
Creating a computer game is a good way to improve your programming skills .


10、 Game development :pygame

  • Pygame Is a set of cross platform Python modular , Used to create video games .
  • It is designed to be related to Python The computer graphics and sound library used together with the programming language .
  • Pygame from Pete Shinners Official preparation , To replace PySDL.
  • Pygame Suitable for creating client applications , These applications can be packaged in separate executables .
import pygame

pygame.init()
screen = pygame.display.set_mode((400, 500))
done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    pygame.display.flip()

............ Separator .......

Two 、python Common standard library

1、 Operating system interface

os Module provides many functions associated with the operating system .

>>> import os
>>> os.getcwd()      #  Return to the current working directory 
'C:\\Python34'
>>> os.chdir('/server/accesslogs')   #  Modify the current working directory 
>>> os.system('mkdir today')   #  Execute system commands  mkdir 
0

It is recommended to use "import os" Style rather than "from os import *". This ensures that the operating system will change os.open() Does not override built-in functions open().

os Common commands

Serial number Method function
1 os.access(path, mode)   Verify authority mode
2 os.chdir(path)    Change the current working directory
3 os.chflags(path, flags)   Set the path's flag as a numeric flag .
4 os.chmod(path, mode)     Change permissions
5 os.chown(path, uid, gid)   Change file owner
6 os.chroot(path)    Change the root of the current process
7 os.close(fd)     Close the file descriptor fd
8 os.closerange(fd_low, fd_high)     Close all file descriptors , from fd_low ( contain ) To fd_high ( It doesn't contain ), Mistakes will be ignored
9 os.dup(fd)     Copy file descriptors fd
10 os.dup2(fd, fd2)     Put a file descriptor fd Copy to another fd2


2、 File wildcard

glob Module provides a function to generate a list of files from a directory wildcard search :

>>> import glob
>>> glob.glob('*.py')
['primes.py', 'random.py', 'quote.py']

3、 Command line arguments

Common tool scripts often call command line arguments . These command line parameters are stored in the form of a linked list sys Modular argv Variable .

sys.argv
It can be used sys.argv Get the parameter list of the currently executing command line parameters (list).
Variable interpretation

  • sys.argv[0] Current program name
  • sys.argv[1] The first parameter
  • sys.argv[2] The second parameter

len(sys.argv)-1 Number of parameters ( Subtract file name )

For example, execute... On the command line "python demo.py one two three" After that, we can get the following output results :

>>> import sys
>>> print(sys.argv)
['demo.py', 'one', 'two', 'three']

4、 String regular match

re Module provides regular expression tools for advanced string processing . It can be said that it is necessary for reptiles , For complex matching and processing , Regular expressions provide simplicity 、 Optimized solution : If only simple functions are needed , You should first consider string methods , Because they are very simple , Easy to read and debug :

>>> 'tea for too'.replace('too', 'two')
'tea for two'

re.match function

re.match Try to match a pattern... From the beginning of the string , If it's not a successful start match ,match() Just go back to none.

Function syntax

re.match(pattern, string, flags=0)

Function parameter description :

Parameters describe
pattern Matching regular expressions
string String to match .
flags Sign a , Used to control how regular expressions are matched , Such as : Is it case sensitive , Multi line matching and so on .

The match is successful re.match Method returns a matching object , Otherwise return to None.

We can use group(num) or groups() Match object function to get match expression .

Match object method describe
group(num=0) Match the string of the entire expression ,group() You can enter more than one group number at a time , In this case it will return a tuple containing the values corresponding to those groups .
groups() Returns a tuple containing all the group strings , from 1 To Group number included .

5、 Mathematical calculation

math Module for floating-point operations to provide the underlying C Access to function library :

>>> import math
>>> math.cos(math.pi / 4)
0.70710678118654757
>>> math.log(1024, 2)
10.0

In practice ,math Standard libraries often fail to meet requirements , I also need an extension library :NumPy

NumPy(Numerical Python) Support a large number of dimension arrays and matrix operations , In addition, it also provides a large number of mathematical function libraries for array operation .


6、 Send E-mail

There are several modules for accessing the Internet and dealing with network communication protocols . The simplest two of them are used to deal with from urls Of received data urllib.request And the smtplib:

import smtplib
smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )

Parameter description

  • host: SMTP Server host . You can specify the host's ip Address or domain name, such as : runoob.com, This is an optional parameter .
  • port: If you provide host Parameters , You need to specify SMTP The port number used by the service , In general SMTP The port number is 25.
  • local_hostname: If SMTP On your own machine , You just need to specify the server address as localhost that will do .

Python SMTP Object use sendmail Method to send an email , The grammar is as follows :

SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])

Parameter description

  • from_addr: Mail sender address .
  • to_addrs: String list , Email address .
  • msg: Send a message

Case study :

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import smtplib
from email.mime.text import MIMEText
from email.header import Header
 
sender = '[email protected]'
receivers = ['[email protected]']  #  Receiving mail , Can be set to your QQ Email or other email 
 
#  Three parameters : The first is the text content , the second  plain  Format text , Third  utf-8  Set encoding 
message = MIMEText('Python  Email send test ...', 'plain', 'utf-8')
message['From'] = Header(" Novice tutorial ", 'utf-8')   #  sender 
message['To'] =  Header(" test ", 'utf-8')        #  The receiver 
 
subject = 'Python SMTP  Email test '
message['Subject'] = Header(subject, 'utf-8')
 
 
try:
    smtpObj = smtplib.SMTP('localhost')
    smtpObj.sendmail(sender, receivers, message.as_string())
    print " Mail sent successfully "
except smtplib.SMTPException:
    print "Error:  Unable to send mail "

7、 Date and time

datetime Module provides both simple and complex methods for date and time processing .

Support date and time algorithm at the same time , Implementation focuses on more efficient processing and formatting of output .

The module also supports time zone processing :

>>> # dates are easily constructed and formatted
>>> from datetime import date
>>> now = date.today()
>>> now
datetime.date(2003, 12, 2)
>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'

The three ways of existence of time : Time object , Time string , Time stamp .

(1) String rotation datetime:

>>> string = '2014-01-08 11:59:58'
>>> time1 = datetime.datetime.strptime(string,'%Y-%m-%d %H:%M:%S')
>>> print time1
2014-01-08 11:59:58

(2)datetime Turn the string :

>>> time1_str = datetime.datetime.strftime(time1,'%Y-%m-%d %H:%M:%S')
>>> time1_str
'2014-01-08 11:59:58'

(3) Time stamp to time object :

>>>time1 = time.localtime()
>>>time1_str = datetime.datetime.fromtimestamp(time1)

8、 data compression

The following modules directly support the general data packaging and compression format :zlib,gzip,bz2,zipfile, as well as tarfile.

>>> import zlib
>>> s = b'witch which has which witches wrist watch'
>>> len(s)
41
>>> t = zlib.compress(s)
>>> len(t)
37
>>> zlib.decompress(t)
b'witch which has which witches wrist watch'
>>> zlib.crc32(s)
226805979

9、 Performance metrics

Some users are interested in understanding the performance differences between different ways to solve the same problem .Python Provides a measurement tool , Provides direct answers to these questions .

for example , Using tuple encapsulation and unpacking to exchange elements looks more attractive than using traditional methods ,timeit It turns out that modern methods are faster .

>>> from timeit import Timer
>>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
0.57535828626024577
>>> Timer('a,b = b,a', 'a=1; b=2').timeit()
0.54962537085770791

be relative to timeit 's fine-grained ,:mod:profile and pstats Module provides a time measurement tool for larger blocks of code .


10、 Test module

One of the ways to develop high-quality software is to develop test code for each function , And in the development process often test

doctest Module provides a tool , Scan the module and perform the test according to the document string embedded in the program .

The test construct is like simply cutting and pasting its output into a document string .

Through examples provided by users , It reinforces the documentation , allow doctest Module confirms whether the result of the code is consistent with the document :

def average(values):
    """Computes the arithmetic mean of a list of numbers.

    >>> print(average([20, 30, 70]))
    40.0
    """
    return sum(values) / len(values)

import doctest
doctest.testmod()   #  Automatic verification of embedded tests 

 

【 Ask for comment 、 Please thumb up 、 For collection 】 


copyright notice
author[Don't eat tomatoes],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202020933004452.html

Random recommended