Python module-logging, serialization module, re module

Source: Internet
Author: User

Markdownpad Document Logging module
import logging  logging.debug(‘debug message‘)  logging.info(‘info message‘)  logging.warning(‘warning message‘)  logging.error(‘error message‘)  logging.critical(‘critical message‘)运行结果:C:\Python36\python.exe C:/Users/Administrator/PycharmProjects/py_fullstack_s4/day34/test.pyWARNING:root:warning messageERROR:root:error messageCRITICAL:root:critical message

You can see that there is a default rating: Debug--info--warning (default)--error--critical

two ways to configure:1. Congfig function
import logging  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=‘/tmp/test.log‘,                      filemode=‘w‘)logging.debug(‘debug message‘)  logging.info(‘info message‘)  logging.warning(‘warning message‘)  logging.error(‘error message‘)  logging.critical(‘critical message‘)

Level for log levels, the choice of debug will be all printed out, the most important is the content of format, the specific configuration parameters are as follows:

可见在logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。format:指定handler使用的日志显示格式。 datefmt:指定日期时间格式。 level:设置rootlogger(后边会讲解具体概念)的日志级别 stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件(f=open(‘test.log‘,‘w‘)),默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。format参数中可能用到的格式化串:%(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. Logger Object

In the above examples we have learned about Logging.debug (), Logging.info (), logging.warning (), Logging.error (), Logging.critical ( ) (for logging different levels of log information), Logging.basicconfig () (using the default log format (Formatter) to establish a default stream processor (Streamhandler) for the log system, Set the base configuration (such as log level, etc.) and add to root logger (root logger) These several logging module-level functions, plus a module-level function is Logging.getlogger ([name]) (return a Logger object, If you do not specify a name will return root logger) First look at the simplest procedure:

  Import Logginglogger = Logging.getlogger () # Creates a handler that is used to write to the log file FH = logging. Filehandler (' Test.log ') # again creates a handler for output to the console ch = logging. Streamhandler () formatter = logging. Formatter ('% (asctime) s-% (name) s-% (levelname) s-% (message) s ') Fh.setformatter (Formatter) ch.setformatter (Formatter ) Logger.addhandler (FH) #logger对象可以添加多个fh和ch对象logger. AddHandler (CH) logger.debug (' Logger debug message ') Logger.info (' Logger info message ') logger.warning (' Logger warning message ') logger.error (' Logger error message ') logger.critical (' Logger critical message ') Run Result: 2017-04-27 09:19:56,145-root-warning-logger WARNING message2017-04-27 09:19:56,146-r Oot-error-logger ERROR message2017-04-27 09:19:56,146-root-critical-logger CRITICAL message  

For a brief introduction, the logging library provides multiple components: Logger, Handler, Filter, Formatter. The Logger object provides an interface that the application can use directly, handler sends logs to the appropriate destination, and filter provides a way to filter the log information formatter specify the log display format.

Logger is a tree-like hierarchy, and the output information is preceded by a Logger (which is automatically created and uses the root Logger if none of the displayed fetches, as shown in the first example).

Logger = Logging.getlogger () returns a default logger, root logger, and applies the default log level, handler, and formatter settings. Of course, you can also specify the lowest log level through Logger.setlevel (LEL), with the available log levels logging. DEBUG, Logging.info, logging. WARNING, logging. ERROR, logging. CRITICAL.

Logger.debug (), Logger.info (), logger.warning (), Logger.error (), logger.critical () output different levels of logging, Only logs with log levels greater than or equal to the set log level will be output.

Only the output

2014-05-06 12:54:43,222 - root - WARNING - logger warning message2014-05-06 12:54:43,223 - root - ERROR - logger error message2014-05-06 12:54:43,224 - root - CRITICAL - logger critical message

From this output you can see that logger = Logging.getlogger () returns the logger named Root. There is no use of logger.setlevel (logging. Debug) to set the log level for logger, so use the default log level warniing, so the results only output information greater than or equal to warniing level.

Serialization module (JSON, Pickle) what is serialization?

The process of changing an object (variable) from memory to a storage or transfer is called serialization, and in Python it is called pickling, which is also called serialization,marshalling,flattening in other languages, and so on.

After serialization, the serialized content can be written to disk or transferred over the network to another machine.

In turn, re-reading the variable contents from the serialized object into memory is called deserialization, i.e. unpickling.

JSON module

If we are going to pass objects between different programming languages, we have to serialize the object into a standard format, such as XML, but the better way is to serialize it to JSON, because JSON represents a string that can be read by all languages, easily stored to disk, or transmitted over a network. JSON is not only a standard format, but also faster than XML, and can be read directly in the Web page, very convenient.

JSON represents objects that are standard JavaScript language objects, and JSON and Python have built-in data types that correspond to the following:

650) this.width=650; "Width=", "height=" 530 "style=" WIDTH:731PX;HEIGHT:296PX; "src=" http://i.imgur.com/ Qblgmcl.png "alt=" Qblgmcl.png "/>

#----------------------------序列化import jsondic={‘name‘:‘alvin‘,‘age‘:23,‘sex‘:‘male‘}print(type(dic))#<class ‘dict‘>j=json.dumps(dic)print(type(j))#<class ‘str‘>f=open(‘序列化对象‘,‘w‘)f.write(j)  #-------------------等价于json.dump(dic,f)f.close()#-----------------------------反序列化<br>import jsonf=open(‘序列化对象‘)data=json.loads(f.read())#  等价于data=json.load(f)

D = {' name ': ' Alvin ', ' age ': at all, ' sex ': ' Male '}

f = open ("FileName", ' W ')

Json.dump (d,f) #与dumps的区别在于将两步合成一步

F.close ()

Pickle Module
##----------------------------序列化import pickledic={‘name‘:‘alvin‘,‘age‘:23,‘sex‘:‘male‘}print(type(dic))#<class ‘dict‘>j=pickle.dumps(dic)print(type(j))#<class ‘bytes‘>f=open(‘序列化对象_pickle‘,‘wb‘)#注意是w是写入str,wb是写入bytes,j是‘bytes‘f.write(j)  #-------------------等价于pickle.dump(dic,ff.close()#-------------------------反序列化import picklef=open(‘序列化对象_pickle‘,‘rb‘)data=pickle.loads(f.read())#  等价于data=pickle.load(f)print(data[‘age‘])  

The problem with Pickle is the same as for all other programming language-specific serialization problems, that is, it can only be used in Python, and may be incompatible with each other in Python, so it's okay to save only those unimportant data with pickle and not successfully deserialize it.

Re module

In essence, a regular expression (or RE) is a small, highly specialized programming language (in Python) that is embedded in Python and implemented through the RE module. The regular expression pattern is compiled into a sequence of bytecode, which is then executed by a matching engine written in C.

Character match (normal character, metacharacters):

1 Ordinary characters: Most characters and letters will match themselves >>> Re.findall (' Alvin ', ' Yuanalesxalexwupeiqi ') [' Alvin ']

2 meta characters:. ^ $ * + ? { } [ ] | ( ) \

Re.findall ("(?: AD) +yuan", "Adadyuangfsdui") #在 (AD) Groupings include: '?: ' to remove the matching default priority, to match the string exactly, or to match only the contents of the grouping in parentheses

Pipe Break: | That matches the content on either side of it.

650) this.width=650; "src=" Http://i.imgur.com/lnTzTy9.png "alt=" Lntzty9.png "/>

元字符之转义符反斜杠后边跟元字符去除特殊功能,比如\.反斜杠后边跟普通字符实现特殊功能,比如\d\d  匹配任何十进制数;它相当于类 [0-9]。\D 匹配任何非数字字符;它相当于类 [^0-9]。\s  匹配任何空白字符;它相当于类 [ \t\n\r\f\v]。\S 匹配任何非空白字符;它相当于类 [^ \t\n\r\f\v]。\w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。\W 匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]\b  匹配一个特殊字符边界,比如空格 ,&,#等

When using \b, it is important to note that because it has a special meaning in the ASCII code table, which means backspace, using regular expressions in Python, the code is given to the Python interpreter for interpretation, and the interpreter supports the ' \ ' escape symbol, which is then given to the regular expression to match, It should be used in the following form:

ret=re.findall(‘c\\\\l‘,‘abc\le‘)print(ret)执行结果为:[‘c\\l‘]

650) this.width=650; "Width=", "height=" 641 "style=" width:729px;height:390px; "src=" http://i.imgur.com/ Rvkvgi3.png "alt=" Rvkvgi3.png "/>

Common methods under the RE module
  Import Re#1re.findall (' A ', ' Alvin Yuan ')     #返回所有满足匹配条件的结果, placed in the list #2re.search (' A ', ' Alvin Yuan '). Group ()   #函数会在字符串内查找模式匹配, only to find the first match and then return an object that contains matching information, the object can get a matching string by calling the group () method, or None if the string does not match. #3re. Match (' A ', ' ABC '). Group ()     #同search, but do match #4ret=re.split at the beginning of the string (' [ab] ', ' ABCD ')     #先按 ' a ' Split get ' and ' BCD ', in pairs ' and ' BCD ' are split by ' B ' respectively, can be followed by the number of split-parameter print (ret) #[', ' ', ' CD '] #5ret =re.sub (' \d ', ' abc ', ' Alvin5yuan6 ', 1) print ( RET) #alvinabcyuan6ret =re.subn (' \d ', ' abc ', ' Alvin5yuan6 ') print (ret) # (' Alvinabcyuanabc ', 2) #6obj =re.compile (' \d{3} ') ret=obj.search (' abc123eeee ') print (Ret.group ()) #123import reret=re.finditer (' \d ', ' ds3sy4784a ') print (ret)   #<callable_iterator object at 0x10195f940>print (Next (ret). Group ()) print (Next (ret). Group ()) Import reret= Re.findall (' www. ( baidu|oldboy). com ', ' www.oldboy.com ') print (ret) #[' Oldboy ')   This is because FindAll will first return the contents of the matching result set, and if you want to match the results, cancel the permission ret= Re.findall (' www. (?: baidu|oldboy). com ', ' www.oldboy.com ') print (ret) #[' www.oldboy.com ']  
Named groupings

650) this.width=650; "Width=" 795 "height=" style= "width:684px;height:100px; src=" http://i.imgur.com/ Q6d7y97.png "alt=" Q6d7y97.png "/>

Python module-logging, serialization module, re module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.