Python from entry to practice: python common modules

Table of contents

1. Time module

1. Timestamp

2. Format time

3. Structured time

4. Common usage: calculate the execution time of the program

Two, datetime module

Three, random module

Four, os module

1. The os module is responsible for the interaction between the program and the operating system

2. Key modules of os:

3. Os common operations: get the absolute path of the current path, get the parent path of the current path, parent path, etc.

Five, sys module

1. The sys module is responsible for the interaction between the program and the python interpreter

2.sys key module

3. A small case: realize the printing of the progress bar

Six, json and pickle modules

1. Serialization

2. Advantages of serialization:

3.json format

Seven, pickle format

1. Introduction to pickle

2. pickle operation

Eight, hashlib module

1. Introduction to hashlib

2. Features of hash value:

3. Basic operation of hash (take md5 as an example)

4. Crash library crack hash algorithm encryption

Nine, shutil module

1. Introduction to shutil

2. Copy files

3. Move files

4. Read compressed and archive compressed files

5. Unzip the file

Ten, subsprocess module

1. Introduction to subsprocess module

2. Basic operation of subsprocess

Eleven, xml and shelve module

1. Introduction to the shelve module

2. Basic operations of the shelve module

3.xml module

1. Introduction to xml module

4. Basic operation of xml module

5. You can create xml documents by yourself

12. configparser module

1. Basic operation of configparser module

Thirteen, logging module

1. Log level

2. The default level is warning, which is printed to the terminal by default

3. Specify the global configuration for the logging module, valid for all loggers, and control printing to the file

4. Formatter, Handler, Logger, Filter objects of the logging module

Five, the level of Logger and Handler

6. Application

1. Application 1

  2. Application 2: A Simple Case

Fourteen, re module

1. Introduction to regular expressions

2. Commonly used matching modes     

                             

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             


1. Time module

1. Timestamp

time_stamp = time.time()
print(time_stamp, type(time_stamp))

2. Format time

format_time = time.strftime("%Y-%m-%d %X")
print(format_time, type(format_time))

2022-08-31 <class 'str'>

3. Structured time

print('本地时区的struct_time:\n{}'.format(time.localtime()))
print('UTC时区的struct_time:\n{}'.format(time.gmtime()))



本地时区的struct_time:
time.struct_time(tm_year=2022, tm_mon=8, tm_mday=31, tm_hour=16, tm_min=14, tm_sec=41, tm_wday=2, tm_yday=243, tm_isdst=0)
UTC时区的struct_time:
time.struct_time(tm_year=2022, tm_mon=8, tm_mday=31, tm_hour=8, tm_min=14, tm_sec=41, tm_wday=2, tm_yday=243, tm_isdst=0)

4. Common usage: calculate the execution time of the program

# 推迟指定的时间运行,单位为秒
start = time.time()
time.sleep(3)
end = time.time()
 
print(end-start)

Two, datetime module

This module can be regarded as the addition and subtraction module of time

# 返回当前时间
print(datetime.datetime.now())

# 当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(3))
# 当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(-3))
# 当前时间+30分钟
print(datetime.datetime.now() + datetime.timedelta(minutes=30))
# 时间替换
c_time = datetime.datetime.now()
print(c_time.replace(minute=20, hour=5, second=13))

print(datetime.date.fromtimestamp(time.time()))  #换算成年月日
2019-03-07

Three, random module

# 大于0且小于1之间的小数
print(random.random())
# 大于等于1且小于等于3之间的整数
print(random.randint(1, 3))
# 大于1小于3的小数,如1.927109612082716
print(random.uniform(1, 3))
# 列表内的任意一个元素,即1或者‘23’或者[4,5]
print(random.choice([1, '23', [4, 5]]))
1

------------smaple-------比较常用在数据导入的时候像随机选择数据
# random.sample([], n),列表元素任意n个元素的组合,示例n=2
print(random.sample([1, '23', [4, 5]], 2))
[[4, 5], '23']

lis = [1, 3, 5, 7, 9]
# 打乱l的顺序,相当于"洗牌"
random.shuffle(lis)
print(lis)
[1, 3, 9, 7, 5]

Four, os module

1. The os module is responsible for the interaction between the program and the operating system

2. Key modules of os:

os.getcwd()

os.path.abspath('D:\python_base')\

os.path.split('C:\\WINDOWS\\system32\\cmd.exe')

os.path.join('C:\\WINDOWS\\system32\\cmd.exe','hello')

os.path.getsize('C:\\WINDOWS\\system32\\cmd.exe')

3. Os common operations: get the absolute path of the current path, get the parent path of the current path, parent path, etc.

# -*- coding: utf-8 -*-

'''
@Time    : 2022/09/01 10:25
@Author  : Rice
@CSDN : C_小米同学
@FileName: test.py
'''
import os
print(os.path.abspath(__file__)) #返回当前文件的绝对路劲(路径+文件名)
print(os.path.dirname(os.path.abspath(__file__))) #返回当前文件的父路径
print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))#返回当前文件的父路径的父路径
print(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

Five, sys module

1. The sys module is responsible for the interaction between the program and the python interpreter

2.sys key module

sys.path: Returns the search path for modules, initialized with the value of the PYTHONPATH environment variable

sys.version: Get the version information of the Python interpreter

sys.exit(n): Exit the program, exit(0) when exiting normally

3. A small case: realize the printing of the progress bar

#=========知识储备==========
#进度条的效果
[#             ]
[##            ]
[###           ]
[####          ]

#指定宽度
print('[%-15s]' %'#')
print('[%-15s]' %'##')
print('[%-15s]' %'###')
print('[%-15s]' %'####')

#打印%
print('%s%%' %(100)) #第二个%号代表取消第一个%的特殊意义

#可传参来控制宽度
print('[%%-%ds]' %50) #[%-50s]
print(('[%%-%ds]' %50) %'#')
print(('[%%-%ds]' %50) %'##')
print(('[%%-%ds]' %50) %'###')


#=========实现打印进度条函数==========
import sys
import time

def progress(percent,width=50):
    if percent >= 1:
        percent=1
    show_str=('[%%-%ds]' %width) %(int(width*percent)*'#')
    print('\r%s %d%%' %(show_str,int(100*percent)),file=sys.stdout,flush=True,end='')


#=========应用==========
data_size=1025
recv_size=0
while recv_size < data_size:
    time.sleep(0.1) #模拟数据的传输延迟
    recv_size+=1024 #每次收1024

    percent=recv_size/data_size #接收的比例
    progress(percent,width=70) #进度条的宽度70

Six, json and pickle modules

1. Serialization

The process of changing objects (variables) from memory to storage or transmission is called serialization. It is called pickling in Python, and it is also called serialization, marshalling, and flattening in other languages.

2. Advantages of serialization:

1. Persistently save state : memory cannot permanently save data. When the program has been running for a period of time, if we power off or restart the program, the data (with structure) about the program in the memory for a period of time before will be cleared. But before the power is turned off or the program is restarted, all the data in the current memory of the program are saved (saved in a file), so that the next program execution can load the previous data from the file, and then continue to execute. This is serialization .

2. Cross-platform data interaction : When serializing, not only can the serialized content be written to the disk, but also can be transmitted to other machines through the network. If the sending and receiving parties agree to use a serialized format, then it will break It overcomes the limitations brought by platform/language differentiation and realizes cross-platform data interaction.

3.json format

Json serialization is not unique to python, and json serialization is also involved in languages ​​such as java, so using json serialization can achieve the purpose of cross-platform data transmission. Correspondence table between ison data type and python data type

Seven, pickle format

1. Introduction to pickle

Pickle serialization, like all other programming language-specific serialization problems, can only be used for Python, and different versions of Python may not be compatible with each other. Therefore, only unimportant data can be saved with Pickle, that is, it cannot be successfully Deserialization doesn't matter either. But the advantage of pickle is that it can store all data types in Python, including objects, while json cannot . The serialization and deserialization process of the pickle module is shown in the figure below

2. pickle operation

Eight, hashlib module

1. Introduction to hashlib

Hash is an algorithm (in Python3. version, the hashlib module is used instead of the md5 module and sha module, mainly providing SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithms). This algorithm accepts the incoming content and obtains a string of hashes after calculation value.

2. Features of hash value:

1. As long as the input content is the same, the obtained hash value is the same, which can be used for password verification during non-plaintext password transmission

2. The hash value cannot be returned to the content, that is, the security of the non-plaintext password can be guaranteed

3. As long as the hash algorithm used remains unchanged, no matter how large the verification content is, the length of the obtained hash value is fixed and can be used for hash processing of text. The hash algorithm can actually be regarded as a factory as shown in the figure below. The factory receives the raw materials you sent, and the product returned after processing is the hash value

3. Basic operation of hash (take md5 as an example)

4. Crash library crack hash algorithm encryption

Although the hash encryption algorithm looks very powerful, it has certain flaws, that is, it can be reversed by credentialing, as shown in the following code.

Nine, shutil module

1. Introduction to shutil

Compared with osmodules, shutilmodules are used for advanced processing of files and directories, and provide functions such as supporting file assignment, moving, deleting, compressing and decompressing. 

2. Copy files

The main function of the shutil module is to copy files, and there are seven implementation methods:

1. shutil.copyfileobj(file1, file2) overwrite and copy
    the content of file1 overwrite file2, file1 and file2 represent the open file object.

2. shutil.copyfile(file1, file2) overwriting and copying
    is also overwriting, but there is no need to open the file, and the file name is directly overwritten (the source code is still called copyfileobj).

3. shutil.copymode(file1,file2) Permission copy
    only copies the file permission, does not change the file content, group and user, and returns no object.

4. shutil.copystart (file1, file2) status copy
    all the status information of the copied file, including permission, group, user and time, etc., no return object.

5. shutil.copy(file1, file2) content and permission copy
    the content and permission of the copied file, which is equivalent to executing copyfile first and then copysmode.

6. shutil.copy2(file1, file2) content and permissions copy
    the content and all status information of the copied file, which is equivalent to executing copyfile first and then copystart.

7. shutil.copytree() recursive copy
    recursively copy file content and status information

3. Move files

Use the function shutil.move()function to recursively move or rename files and return the target. If the target is an existing directory, src will be moved to the current directory; if the target already exists and is not a directory, it may be overwritten.

4. Read compressed and archive compressed files

Use the function shutil.make_archive() to create an archive and return the archived name.
The syntax is as follows:
shutil.make_archive(base_name,format[,root_dir[,base_dir[,verbose[,dry_run[,owner[,group[,logger]]]]]]])

1.base_name is the name of the file to be created, including the path
2.format indicates the compression format, optional zip, tar or bztar, etc.
3.root_dir is the directory of the archive

5. Unzip the file

Use functions shutil.unpack_archive(filename[,extract_dir[,format]])to analyze unpacking.

1.filename is the full path of the archive

2.extract_dir is the target directory name of the decompressed archive

3.format is the format of the decompressed file

Ten, subsprocess module

1. Introduction to subsprocess module

Subprocess is a built-in module of python. Popen in this module can check whether the command line entered by the user exists.

1. If it exists, write the content to the stdout pipeline

2. If it does not exist, write the information to the stderr pipe

It should be noted that the returned result of this module can only be viewed once by the developer. If you want to view it multiple times, you need to write all the information into variables when outputting for the first time.

2. Basic operation of subsprocess

Popen basic format: subprocess.Popen('command', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

shell=True means the command to run in the terminal

stdout=sbuprocess.PIPE means that when the command exists, write the result to the stdout pipe

stderr=sbuprocess.PIPE means that when the command does not exist, the result is sucked into the stderr pipe

import subprocess

r = subprocess.Popen('wget -q -o xxx', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

print(r.stdout.read().decode('utf8'))
print(r.stderr.read().decode('utf8'))

Eleven, xml and shelve module

1. Introduction to the shelve module

Shelve is similar to a key-value database, which can be conveniently used to save Python memory objects. It uses pickle to serialize data internally.

Simply put, users can save a list, dictionary, or user-defined class instance in the shelf, and take it out directly when they need it next time.

It is a Python memory object. It does not need to take out the data first like traditional databases, and then use the data to reconstruct the required objects.

2. Basic operations of the shelve module


import shelve
import datetime

d = shelve.open('test1')  # 打开一个文件
info = {
    "age":23,
    "color":"red"
}

name = ["tom", "bob", "lili"]

d["name"] = name  # 持久化列表
d["info"] = info  # 持久化字典
d["data"] = datetime.datetime.now()

d.close()
d = shelve.open('test1')  # 打开一个文件
print(d.get("name"))
print(d.get("info"))
print(d.get("data"))

3.xml module

1. Introduction to xml module

The xml protocol is supported in various languages. In python, the following modules can be used to operate xml

4. Basic operation of xml module

Let's give an example: we first create a new xml file (named xmltest.xml), the content is as follows:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

Let's look at the three most basic operations:



# print(root.iter('year')) #全文搜索
# print(root.find('country')) #在root的子节点找,只找一个
# print(root.findall('country')) #在root的子节点找,找所有

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)
 
#遍历xml文档
for child in root:
    print('========>',child.tag,child.attrib,child.attrib['name'])
    for i in child:
        print(i.tag,i.attrib,i.text)
 
#只遍历year 节点
for node in root.iter('year'):
    print(node.tag,node.text)
#---------------------------------------

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
 
#修改
for node in root.iter('year'):
    new_year=int(node.text)+1
    node.text=str(new_year)
    node.set('updated','yes')
    node.set('version','1.0')
tree.write('test.xml')
 
 
#删除node
for country in root.findall('country'):
   rank = int(country.find('rank').text)
   if rank > 50:
     root.remove(country)
 
tree.write('output.xml')
#在country内添加(append)节点year2
import xml.etree.ElementTree as ET
tree = ET.parse("a.xml")
root=tree.getroot()
for country in root.findall('country'):
    for year in country.findall('year'):
        if int(year.text) > 2000:
            year2=ET.Element('year2')
            year2.text='新年'
            year2.attrib={'update':'yes'}
            country.append(year2) #往country节点下添加子节点

tree.write('a.xml.swap')

5. You can create xml documents by yourself

import xml.etree.ElementTree as ET
 
 
new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = '33'
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '19'
 
et = ET.ElementTree(new_xml) #生成文档对象
et.write("test.xml", encoding="utf-8",xml_declaration=True)
 
ET.dump(new_xml) #打印生成的格式

12. configparser module

1. Basic operation of configparser module

We first create a configuration file (the suffix is ​​.ini/.confg)

The configuration file is as follows

Read operation:

import configparser

config=configparser.ConfigParser()
config.read('a.cfg')

#查看所有的标题
res=config.sections() #['section1', 'section2']
print(res)

#查看标题section1下所有key=value的key
options=config.options('section1')
print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary']

#查看标题section1下所有key=value的(key,value)格式
item_list=config.items('section1')
print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]

#查看标题section1下user的值=>字符串格式
val=config.get('section1','user')
print(val) #egon

#查看标题section1下age的值=>整数格式
val1=config.getint('section1','age')
print(val1) #18

#查看标题section1下is_admin的值=>布尔值格式
val2=config.getboolean('section1','is_admin')
print(val2) #True

#查看标题section1下salary的值=>浮点型格式
val3=config.getfloat('section1','salary')
print(val3) #31.0

We also have some modification operations:

import configparser

config=configparser.ConfigParser()
config.read('a.cfg',encoding='utf-8')


#删除整个标题section2
config.remove_section('section2')

#删除标题section1下的某个k1和k2
config.remove_option('section1','k1')
config.remove_option('section1','k2')

#判断是否存在某个标题
print(config.has_section('section1'))

#判断标题section1下是否有user
print(config.has_option('section1',''))


#添加一个标题
config.add_section('egon')

#在标题egon下添加name=egon,age=18的配置
config.set('egon','name','egon')
config.set('egon','age',18) #报错,必须是字符串


#最后将修改的内容写入文件,完成最终的修改
config.write(open('a.cfg','w'))

Based on the above method, add an ini file:

import configparser
  
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                      'Compression': 'yes',
                     'CompressionLevel': '9'}
  
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'     # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
   config.write(configfile)

Thirteen, logging module

1. Log level

CRITICAL = 50 #FATAL = CRITICAL
ERROR = 40
WARNING = 30 #WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0 #不设置

2. The default level is warning, which is printed to the terminal by default

import logging

logging.debug('调试debug')
logging.info('消息info')
logging.warning('警告warn')
logging.error('错误error')
logging.critical('严重critical')

'''
WARNING:root:警告warn
ERROR:root:错误error
CRITICAL:root:严重critical
'''

3. Specify the global configuration for the logging module, valid for all loggers, and control printing to the file

In the logging.basicConfig() function, you can change the default behavior of the logging module through specific parameters. The available parameters are filename 
: Create a FiledHandler with the specified file name (the concept of the handler will be explained in detail later), so that the log will be stored in the specified in the file. 
filemode: file opening mode, this parameter is used when filename is specified, the default value is "a" and "w" can also be specified. 
format: Specifies the log display format used by the handler. 
datefmt: Specifies the datetime format. 
level: Set the log level of rootlogger (the specific concept will be explained later) 
stream: Create a StreamHandler with the specified stream. You can specify output to sys.stderr, sys.stdout or a file, the default is sys.stderr. If both filename and stream parameters are listed, the stream parameter will be ignored. 


Format strings that may be used in the format parameter: 
%(name)s Logger name 
%(levelno)s Log level in numeric form 
%(levelname)s Log level in text form 
%(pathname)s Module that calls the log output function The full path name of 
%(filename)s, there may be no %(filename)s The file name of the module that calls the log output function 
%(module)s The module name that calls the log output function 
%(funcName)s The function name that calls the log output function 
%(lineno)d The line of code where the statement that calls the log output function is located
%(created)f The current time, represented by a UNIX standard floating-point number 
%(relativeCreated)d When outputting log information, the number of milliseconds since the Logger was created 
%(asctime)s The current time in the form of a string. The default format is "2003-07-08 16:49:45,896". After the comma is the millisecond 
%(thread)d thread ID. There may be no 
%(threadName)s thread names. There may be no 
%(process)d process ID. There may be no 
messages output by the %(message)s user
#========使用
import logging
logging.basicConfig(filename='access.log',
                    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',
                    level=10)

logging.debug('调试debug')
logging.info('消息info')
logging.warning('警告warn')
logging.error('错误error')
logging.critical('严重critical')
#========结果
access.log内容:
2017-07-28 20:32:17 PM - root - DEBUG -test:  调试debug
2017-07-28 20:32:17 PM - root - INFO -test:  消息info
2017-07-28 20:32:17 PM - root - WARNING -test:  警告warn
2017-07-28 20:32:17 PM - root - ERROR -test:  错误error
2017-07-28 20:32:17 PM - root - CRITICAL -test:  严重critical

4. Formatter, Handler, Logger, Filter objects of the logging module

 A few parameters we need to keep in mind:

1.logger: the object that generates the log 

2.Filter: the object that filters the log 

3.Handler: receives the log and then controls the printing to different places, FileHandler is used to print to the file, and StreamHandler is used to print to the terminal 

4.Formatter object: can Customize different log format objects, and then bind them to different Handler objects to control the log format of different Handlers

'''
critical=50
error =40
warning =30
info = 20
debug =10
'''


import logging

#1、logger对象:负责产生日志,然后交给Filter过滤,然后交给不同的Handler输出
logger=logging.getLogger(__file__)

#2、Filter对象:不常用,略

#3、Handler对象:接收logger传来的日志,然后控制输出
h1=logging.FileHandler('t1.log') #打印到文件
h2=logging.FileHandler('t2.log') #打印到文件
h3=logging.StreamHandler() #打印到终端

#4、Formatter对象:日志格式
formmater1=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',  #打印到文件
                    datefmt='%Y-%m-%d %H:%M:%S %p',) 

formmater2=logging.Formatter('%(asctime)s :  %(message)s',  #打印到文件
                    datefmt='%Y-%m-%d %H:%M:%S %p',)

formmater3=logging.Formatter('%(name)s %(message)s',)  #打印到终端


#5、为Handler对象绑定格式
h1.setFormatter(formmater1)
h2.setFormatter(formmater2)
h3.setFormatter(formmater3)

#6、将Handler添加给logger并设置日志级别
logger.addHandler(h1)
logger.addHandler(h2)
logger.addHandler(h3)
logger.setLevel(10)

#7、测试
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')

Five, the level of Logger and Handler

The logger is the first level of filtering, and then the handler can be reached. We can set the level for the logger and the handler at the same time, but it needs to be                                                                                                                                                         

import logging


form=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',)

ch=logging.StreamHandler()

ch.setFormatter(form)
# ch.setLevel(10)
ch.setLevel(0)

l1=logging.getLogger('root')
# l1.setLevel(20)
l1.setLevel(10)
l1.addHandler(ch)  #发送的权限要在接收的权限里面,也就是发送的权限要比接收的权限要小

l1.debug('l1 debug')

6. Application

1. Application 1

 Let's write a setting.py file first to configure loger

# -*- coding: utf-8 -*-

'''
@Time    : 2022/09/01 14:45
@Author  : Rice
@CSDN : C_小米同学
@FileName: setting.py
'''
"""
logging配置
"""

import os

# 1、定义三种日志输出格式,日志中可能用到的格式化串如下
# %(name)s Logger的名字
# %(levelno)s 数字形式的日志级别
# %(levelname)s 文本形式的日志级别
# %(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
# %(filename)s 调用日志输出函数的模块的文件名
# %(module)s 调用日志输出函数的模块名
# %(funcName)s 调用日志输出函数的函数名
# %(lineno)d 调用日志输出函数的语句所在的代码行
# %(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
# %(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
# %(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
# %(thread)d 线程ID。可能没有
# %(threadName)s 线程名。可能没有
# %(process)d 进程ID。可能没有
# %(message)s用户输出的消息

# 2、强调:其中的%(name)s为getlogger时指定的名字
standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \
                  '[%(levelname)s][%(message)s]'

simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'

test_format = '%(asctime)s] %(message)s'

# 3、日志配置字典
LOGGING_DIC = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': standard_format
        },
        'simple': {
            'format': simple_format
        },
        'test': {
            'format': test_format
        },
    },
    'filters': {},
    'handlers': {
        #打印到终端的日志
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',  # 打印到屏幕
            'formatter': 'simple'
        },
        #打印到文件的日志,收集info及以上的日志
        'default': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件,日志轮转
            'formatter': 'standard',
            # 可以定制日志文件路径
            # BASE_DIR = os.path.dirname(os.path.abspath(__file__))  # log文件的目录
            # LOG_PATH = os.path.join(BASE_DIR,'a1.log')
            'filename': 'a1.log',  # 日志文件
            'maxBytes': 1024*1024*5,  # 日志大小 5M
            'backupCount': 5,
            'encoding': 'utf-8',  # 日志文件的编码,再也不用担心中文log乱码了
        },
        'other': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',  # 保存到文件
            'formatter': 'test',
            'filename': 'a2.log',
            'encoding': 'utf-8',
        },
    },
    'loggers': {
        #logging.getLogger(__name__)拿到的logger配置
        '''
        如果都没找到,则用空的'',key是用户提供的
        '''
        '': {
            'handlers': ['default', 'console'],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
            'level': 'DEBUG', # loggers(第一层日志级别关限制)--->handlers(第二层日志级别关卡限制)
            'propagate': False,  # 默认为True,向上(更高level的logger)传递,通常设置为False即可,否则会一份日志向上层层传递
        },
        'kkk': {
            'handlers': ['default', 'console'],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
            'level': 'DEBUG',  # loggers(第一层日志级别关限制)--->handlers(第二层日志级别关卡限制)
            'propagate': False,  # 默认为True,向上(更高level的logger)传递,通常设置为False即可,否则会一份日志向上层层传递
        },
        'bbb': {
            'handlers': ['default',],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
            'level': 'DEBUG',  # loggers(第一层日志级别关限制)--->handlers(第二层日志级别关卡限制)
            'propagate': False,  # 默认为True,向上(更高level的logger)传递,通常设置为False即可,否则会一份日志向上层层传递
        },

        '专门的采集': {
            'handlers': ['other',],
            'level': 'DEBUG',
            'propagate': False,
        },
    },
}

Then, we create a new src.py to initialize and call the log

# -*- coding: utf-8 -*-

'''
@Time    : 2022/09/01 15:00
@Author  : Rice
@CSDN : C_小米同学
@FileName: src.py
'''


#拿到日志的产生者loggers-kkk/bbb
#先导入日志配置字典LOGGING_DIC
import setting
#import logging.config #注意,直接导入logging,然后在调config是不行的,这不是包,是文件,没有配置__init__文件

from logging import config,getLogger
#导入
config.dictConfig(setting.LOGGING_DIC)

# logger1 = getLogger('kkk')
#
# logger1.info('hello')  #info权限

logger2 = getLogger('bbb')
logger2.info('nihao')

  2. Application 2: A Simple Case

"""
MyLogging Test
"""

import time
import logging
import my_logging  # 导入自定义的logging配置

logger = logging.getLogger(__name__)  # 生成logger实例


def demo():
    logger.debug("start range... time:{}".format(time.time()))
    logger.info("中文测试开始。。。")
    for i in range(10):
        logger.debug("i:{}".format(i))
        time.sleep(0.2)
    else:
        logger.debug("over range... time:{}".format(time.time()))
    logger.info("中文测试结束。。。")

if __name__ == "__main__":
    my_logging.load_my_logging_cfg()  # 在你程序文件的入口加载自定义logging配置
    demo()

Fourteen, re module

1. Introduction to regular expressions

Regularization is a method of combining some symbols with special meanings (called regular expressions) to describe characters or strings. In other words: Regularity is the rule used to describe a class of things. (in Python) It is embedded in Python and implemented through the re module. Regular expression patterns are compiled into a series of bytecodes, which are then executed by a matching engine written in C.

2. Commonly used matching modes     

                             

# =================================匹配模式=================================
#一对一的匹配
# 'hello'.replace(old,new)
# 'hello'.find('pattern')

#正则匹配
import re
#\w与\W
print(re.findall('\w','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']
print(re.findall('\W','hello egon 123')) #[' ', ' ']

#\s与\S
print(re.findall('\s','hello  egon  123')) #[' ', ' ', ' ', ' ']
print(re.findall('\S','hello  egon  123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']

#\n \t都是空,都可以被\s匹配
print(re.findall('\s','hello \n egon \t 123')) #[' ', '\n', ' ', ' ', '\t', ' ']

#\n与\t
print(re.findall(r'\n','hello egon \n123')) #['\n']
print(re.findall(r'\t','hello egon\t123')) #['\n']

#\d与\D
print(re.findall('\d','hello egon 123')) #['1', '2', '3']
print(re.findall('\D','hello egon 123')) #['h', 'e', 'l', 'l', 'o', ' ', 'e', 'g', 'o', 'n', ' ']

#\A与\Z
print(re.findall('\Ahe','hello egon 123')) #['he'],\A==>^
print(re.findall('123\Z','hello egon 123')) #['he'],\Z==>$

 

指定匹配必须出现在字符串的开头或行的开头。

\A 
指定匹配必须出现在字符串的开头(忽略 Multiline 选项)。

$ 
指定匹配必须出现在以下位置:字符串结尾、字符串结尾的 \n 之前或行的结尾。

\Z 
指定匹配必须出现在字符串的结尾或字符串结尾的 \n 之前(忽略 Multiline 选项)。

#^与$
print(re.findall('^h','hello egon 123')) #['h']
print(re.findall('3$','hello egon 123')) #['3']

# 重复匹配:| . | * | ? | .* | .*? | + | {n,m} |
#.
print(re.findall('a.b','a1b')) #['a1b']
print(re.findall('a.b','a1b a*b a b aaab')) #['a1b', 'a*b', 'a b', 'aab']
print(re.findall('a.b','a\nb')) #[]
print(re.findall('a.b','a\nb',re.S)) #['a\nb']
print(re.findall('a.b','a\nb',re.DOTALL)) #['a\nb']同上一条意思一样

#*
print(re.findall('ab*','bbbbbbb')) #[]
print(re.findall('ab*','a')) #['a']
print(re.findall('ab*','abbbb')) #['abbbb']

#?
print(re.findall('ab?','a')) #['a']
print(re.findall('ab?','abbb')) #['ab']
#匹配所有包含小数在内的数字
print(re.findall('\d+\.?\d*',"asdfasdf123as1.13dfa12adsf1asdf3")) #['123', '1.13', '12', '1', '3']

#.*默认为贪婪匹配
print(re.findall('a.*b','a1b22222222b')) #['a1b22222222b']

#.*?为非贪婪匹配:推荐使用
print(re.findall('a.*?b','a1b22222222b')) #['a1b']

#+
print(re.findall('ab+','a')) #[]
print(re.findall('ab+','abbb')) #['abbb']

#{n,m}
print(re.findall('ab{2}','abbb')) #['abb']
print(re.findall('ab{2,4}','abbb')) #['abb']
print(re.findall('ab{1,}','abbb')) #'ab{1,}' ===> 'ab+'
print(re.findall('ab{0,}','abbb')) #'ab{0,}' ===> 'ab*'

#[]
print(re.findall('a[1*-]b','a1b a*b a-b')) #[]内的都为普通字符了,且如果-没有被转意的话,应该放到[]的开头或结尾
print(re.findall('a[^1*-]b','a1b a*b a-b a=b')) #[]内的^代表的意思是取反,所以结果为['a=b']
print(re.findall('a[0-9]b','a1b a*b a-b a=b')) #[]内的^代表的意思是取反,所以结果为['a=b']
print(re.findall('a[a-z]b','a1b a*b a-b a=b aeb')) #[]内的^代表的意思是取反,所以结果为['a=b']
print(re.findall('a[a-zA-Z]b','a1b a*b a-b a=b aeb aEb')) #[]内的^代表的意思是取反,所以结果为['a=b']

#\# print(re.findall('a\\c','a\c')) #对于正则来说a\\c确实可以匹配到a\c,但是在python解释器读取a\\c时,会发生转义,然后交给re去执行,所以抛出异常
print(re.findall(r'a\\c','a\c')) #r代表告诉解释器使用rawstring,即原生字符串,把我们正则内的所有符号都当普通字符处理,不要转义
print(re.findall('a\\\\c','a\c')) #同上面的意思一样,和上面的结果一样都是['a\\c']

#():分组
print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab']
print(re.findall('(ab)+123','ababab123')) #['ab'],匹配到末尾的ab123中的ab
print(re.findall('(?:ab)+123','ababab123')) #findall的结果不是匹配的全部内容,而是组内的内容,?:可以让结果为匹配的全部内容
print(re.findall('href="(.*?)"','<a href="http://www.baidu.com">点击</a>'))#['http://www.baidu.com']
print(re.findall('href="(?:.*?)"','<a href="http://www.baidu.com">点击</a>'))#['href="http://www.baidu.com"']

#|
print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company'))
# ===========================re模块提供的方法介绍===========================
import re
#1
print(re.findall('e','alex make love') )   #['e', 'e', 'e'],返回所有满足匹配条件的结果,放在列表里
#2
print(re.search('e','alex make love').group()) #e,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。

#3
print(re.match('e','alex make love'))    #None,同search,不过在字符串开始处进行匹配,完全可以用search+^代替match

#4
print(re.split('[ab]','abcd'))     #['', '', 'cd'],先按'a'分割得到''和'bcd',再对''和'bcd'分别按'b'分割

#5
print('===>',re.sub('a','A','alex make love')) #===> Alex mAke love,不指定n,默认替换所有
print('===>',re.sub('a','A','alex make love',1)) #===> Alex make love
print('===>',re.sub('a','A','alex make love',2)) #===> Alex mAke love
print('===>',re.sub('^(\w+)(.*?\s)(\w+)(.*?\s)(\w+)(.*?)$',r'\5\2\3\4\1','alex make love')) #===> love make alex

print('===>',re.subn('a','A','alex make love')) #===> ('Alex mAke love', 2),结果带有总共替换的个数


#6
obj=re.compile('\d{2}')

print(obj.search('abc123eeee').group()) #12
print(obj.findall('abc123eeee')) #['12'],重用了obj

 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

 

Guess you like

Origin blog.csdn.net/weixin_43507744/article/details/126625982