Chapter 4 Exercises

1. How many log levels does the logging module have?

debug

info

warning

error

critical

2. Please configure the logging module to print logs in the following formats on the screen and in files at the same time

2017-10-18 15:56:26,613 - access - ERROR - account [1234] too many login attempts
 1 import logging
 2 
 3 logger = logging.getLogger('access')
 4 logger.setLevel(logging.ERROR)
 5 
 6 
 7 
 8 ch = logging.StreamHandler()
 9 fh = logging.FileHandler('homework-logging')
10 
11 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s' )
12 ch.setFormatter(formatter)
13 fh.setFormatter(formatter)
14 
15 logger.addHandler(ch)
16 logger.addHandler(fh)
17 
18 
19 logger.error('account [1234] too many login attempts')
View Code

3. What are the differences between json, pickle, and shelve?

The json pickle shelve is a serial number, which converts the data type in memory into a string, or deserializes it from a file into its original format.

json is a platform, and only supports str, int list, dict, tuple data types
pickle is the same as json, supports all data types, but can only be used in python
Shelve, like pickle, supports all data types, can only be used in python, can be dumped and local multiple times, and can also be modified

4. What is the role of json

JSON is used for data transmission between platforms. Only strings can be used for data transmission. Using JSON, the data in the content can be converted into strings or from strings to original data types.

Guess you like

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