Day 26

Today, I talked about three modules: configparser, logging, and collections.

1, configparser: configuration module

A dictionary in the form of

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

This is going to be written, this is the effect;

That is to say, the content in the square brackets to the left of the equal sign is used as the title, and the key-value pair is used as the assignment (what is this called, I don't know, anyway, that's what it means). If there is a need for this case, do not use this

 

2. Logging module: form logs

Note that the content of the log is written by the programmers themselves, not he will automatically write it for you

There are two main categories of logging:

(1) Simple configuration mode:

import logging
# By default, only warnings and above will be displayed
# logging.basicConfig(level=logging.DEBUG,
# format='%(asctime)s %(filename)s[line:%(lineno)d] %( levelname)s %(message)s',
# datefmt='%a, %d %b %y %H:%M:%S',
# filename = 'userinfo.log'
# )
# logging.debug('debug message') # debug debug mode is the lowest level
# logging.info('info message') # info displays normal information
# logging.warning('warning message') # warning displays warning information
# logging.error('error message') # error Display error message
# logging.critical('critical message') # critical Display critical error message

Simple is very simple, but this makes it inflexible, and its encoding form cannot be changed, nor can it be output on the screen while being written in a file.

(2) logger: configure the logger object

The degree of freedom is very high, but I can't understand

import logging
logger = logging.getLogger() # instantiates a logger object

fh = logging.FileHandler('test.log',encoding='utf-8') # instantiates a file handle
sh = logging.StreamHandler()

fmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(fmt) # Format is associated with file handle or screen handle
sh.setFormatter (fmt)
sh.setLevel(logging.WARNING)

# Suction star Dafa
logger.addHandler(fh) # Only the handle associated with logger
logger.addHandler(sh)
logger.setLevel(logging.DEBUG)

logger.debug('debug message') # debug debug mode is the lowest level
logger.info('info message') # info displays normal information
logger.warning('warning message') # warning displays warning message
logger.error('error message ') # error display error message
logger.critical('critical message')

 The logging module provides 5 log levels, from low to high: debug info warning error critical
 By default, the display starts from warning mode and
 only displays some basic information. We can also configure the displayed format.

3. Collections module

from collections import deque

This is a double ended queue

This involves the concept of a queue (queue)

The so-called queue is an ordered list, first in first out is called queue, first in last out is called stack

Personally, I think it is most appropriate to understand it as a list.

Its methods are append (add right) appendleft (add left)

There is also an example of namedtuple, which is a tuple with a name. The following example is the best explanation

# from collections import namedtuple
# Point = namedtuple('Point',['x','y'])
# p = Point(1,2)
# p = Point(1,2)
# print(p)
# print(p.x)
# print(p.y)

This is an example of x, y coordinates, through this method, it can look more vivid than a simple tuple

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324740038&siteId=291194637