The Python common module (VI) module and the logging module and Re packet

5.10 re module

  1. re (regular) Description: Regular use is of some symbol combinations having special meaning will be described a character string together or method (referred to as regular expressions). Or: Regular rule is used to describe a class of things.

  2. re metacharacters

    • 元字符 匹配内容
      \w Match letter (containing Chinese), or numbers, or underscores
      \W Matching non-alphabetic (contains Chinese), or numbers, or underscores
      \s Matches any whitespace
      \S Matches any non-whitespace
      \d Matching numbers
      \D Matching non-numeric
      \A From the beginning of the string match
      \n Matches a newline
      \t A matching tab
      ^ Matches the beginning of the string
      $ End of the string
      . Matches any character except newline, when re.DOTALL flag is specified, it will match any character comprises a newline.
      [...] Matches the character set of characters
      [^] Matches all characters except the characters in the character set
      * Match zero or more characters to the left.
      + Match one or more characters left.
      Matches zero or one character to the left, non-greedy way.
      {n} Precisely matches the n preceding expression.
      {n,m} N m times to match the regular expression by the preceding definition segment, greedy manner
      a|b Matches a or b
      () Matching expression in parentheses, also represents a group
    s = "meet 黑哥_dsb"
    print(re.findall("\w",s))
    # 结果:['m', 'e', 'e', 't', '黑', '哥', '_', 'd', 's', 'b']
    s = "meet @黑哥!_dsb"
    print(re.findall("\W",s))
    # 结果:[' ', '@', '!']
    s = "meet 黑哥\n_dsb\t"
    print(re.findall("\s",s))
    # 结果:[' ', '\n', '\t']
    s = "meet 黑哥\n_dsb\t"
    print(re.findall("\S",s))
    # 结果:['m', 'e', 'e', 't', '黑', '哥', '_', 'd', 's', 'b']
    s = "meet1 黑哥2_dsb3"
    print(re.findall("\d",s))
    # 结果:['1', '2', '3']
    s = "meet1 黑哥2_dsb3"
    print(re.findall("\D",s))
    # 结果:['m', 'e', 'e', 't', ' ', '黑', '哥', '_', 'd', 's', 'b']
    s = "meet 黑哥_dsb"
    print(re.findall("\Am",s))
    print(re.findall("\Ad",s))
    # 结果:
    ['m']
    []
    s = "meet \n黑哥\t_\ndsb"
    print(re.findall("\n",s))
    # 结果:
    ['\n', '\n']
    s = "meet \n黑哥\t_\ndsb"
    print(re.findall("\t",s))
    # 结果:
    ['\t']
    s = "meet 黑哥_dsb"
    print(re.findall("^m",s))
    print(re.findall("^d",s))
    # 结果:
    ['m']
    []
    s = "meet 黑哥_dsb"
    print(re.findall("b$",s))
    print(re.findall("sb$",s))
    # 结果:
    ['b']
    ['sb']
    s = "meet 黑哥_dsb"
    print(re.findall("m..",s))
    # 结果:['mee']
    s = "meet1 黑哥2_dsb3"
    print(re.findall("[1-3]",s))
    # 结果:['1', '2', '3']
    s = "meet1 黑哥2_dsb3"
    print(re.findall("[^(1-3)]",s))
    # 结果:['m', 'e', 'e', 't', ' ', '黑', '哥', '_', 'd', 's', 'b']
    s = "meet 黑m哥_dsb meet meee"
    print(re.findall("me*",s))
    # 结果:['mee', 'm', 'mee', 'meee']
    s = "meet 黑m哥_dsb meet meee"
    print(re.findall("me+",s))
    # 结果:['mee', 'mee', 'meee']
    s = "meet 黑m哥_dsb meet meee"
    print(re.findall("me*?",s))
    print(re.findall("me+?",s))
    # 结果:
    ['m', 'm', 'm', 'm']
    ['me', 'me', 'me']
    s = "meet 黑m哥_dsb meet meee"
    print(re.findall("e{3}",s))
    # 结果:
    ['eee']
    s = "meet 黑m哥_dsb meet meee"
    print(re.findall("e{1,3}",s))
    # 结果:
    ['ee', 'ee', 'eee']
    s = "2019-7-26 20:30:30"
    print(re.split(":|-|\s",s))
    # 结果:
    ['2019', '7', '26', '20', '30', '30']
    s = "meet 黑m哥_dsb meet meee"
    print(re.findall("m(.*?)t",s))
    # 结果:
    ['ee', '哥_dsb mee']
  3. re module common method

    • findall all find returns a list

    • search from anywhere in the string to find a match stopped, it returns an object. for a matching content must .group () for obtaining

    • import re
      print(re.search("sb|nb","alexsb meetnb"))
      print(re.search("sb|nb","alexsb meetnb").group())
      # 结果
      <_sre.SRE_Match object; span=(4, 6), match='sb'>
      sb
    • match matching string from a start position

    • import re
      print()re.match("sb|nb","alexdsb,alex_sb,alexnb,al_ex")
      print(re.match("sb|nb","alexdsb,alex_sb,alexnb,al_ex").group())
      # 结果:
      None
      AttributeError: 'NoneType' object has no attribute 'group'  # 'NoneType'对象没有属性'group'
    • split the partition can be separated in any delimiter

    • import re
      s = "2019-7-26 20:30:30"
      print(re.split(":|-|\s",s))
      # 结果:
      ['2019', '7', '26', '20', '30', '30']
    • sub replacement

    • import re
      s = "meet是一位好老师,meet教会了我们很多知识"
      print(re.sub("meet","苍老师",s))
      # 结果:
      苍老师是一位好老师,苍老师教会了我们很多知识
    • compile -defined matching rules

    • import re
      fn = "\d+"
      s = "太白123meet456"
      print(re.split(fn,s))
      # 结果:
      ['太白', 'meet', '']
    • finditer Returns an iterator

    • import re
      s = "太白123"
      g = re.finditer("\w",s)
      for i in g:
          print(i)
          print(next(i))
      # 结果:
      <_sre.SRE_Match object; span=(0, 1), match='太'>
      太
      <_sre.SRE_Match object; span=(1, 2), match='白'>
      白
      <_sre.SRE_Match object; span=(2, 3), match='1'>
      1
      <_sre.SRE_Match object; span=(3, 4), match='2'>
      2
      <_sre.SRE_Match object; span=(4, 5), match='3'>
      3
    • To group a name

    • import re
      ret = re.search("<(?P<tag_name>\w+)>\w+</\w+>","<h1>hello</h1>")  # 给分组1取名tag_name
      print(ret.group("tag_name"))
      print(ret.group())
      # 结果:
      h1
      <h1>hello</h1>
      
      import re
      ret = re.search(r"<(\w+)>\w+</\1>","<h1>hello</h1>")   # \1填充分组1中的内容
      print(ret.group(1))
      print(ret.group())
      # 结果:
      h1
      <h1>hello</h1>

5.11 logging modules and packages

  1. logging module Description: logging module to record the various states of our software, you can also record various transaction information; in fact, every software is an error log, developers can modify his program through the contents of the error log

  2. Log Level

    • import logging
      
      logging.debug('调试debug')           # DEBUG = 10
      logging.info('消息info')             # INFO = 20
      logging.warning('警告warn')          # WARNING = 30
      logging.error('错误error')           # ERROR = 40
      logging.critical('严重critical')     # CRITICAL = 50
      
      '''
      WARNING:root:警告warn
      ERROR:root:错误error
      CRITICAL:root:严重critical
      '''
    • Python's logging module logs printed by default to the standard output, and only shows a greater than or equal WARNING level log, indicating that the default log level WARNING (log level Level CRITICAL> ERROR> WARNING> INFO> DEBUG), the default log format for the log level: Logger name: user output messages.
  3. Flexible configuration log level, the log format, output location

    • 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')
  4. basicConfig () function can be changed by the logging module specific parameters default behavior parameters are available:

    • filename: Creating FiledHandler with the specified file name, such logs are stored in the specified file
    • filemode: File Open, this parameter is specified in the filename, the default value "a" may also be designated as "w".
    • format: log handler specified display format.
    • datefmt: specify the date and time format.
    • Set logging levels: level
    • stream: Creating StreamHandler with the specified stream. You can specify output to
    • sys.stderr, sys.stdout or file (f = open ( 'test.log', 'w')), default sys.stderr. If both lists the filename and stream two parameters, the stream parameter is ignored.
  5. The format parameter string format might be used

    • % (Name) s Logger name
    • % (Levelno) s log level digital form
    • % (Levelname) s log level text form
    • % (Pathname) s call to the full path name of the log output function module may not
    • % (Filename) s call log output function module filename
    • % (Module) s call log output function module name
    • Function names% (funcName) s call log output function
    • OK code statements% (lineno) d log output function call where
    • % (Created) f the current time, represented by a standard floating point representation UNIX Time
    • Since the number of milliseconds Logger created when the d output log information% (relativeCreated)
    • % (Asctime) of the current time string s. The default format is "2003-07-0816: 49: 45,896." Milliseconds after the comma
    • % (Thread) d thread ID. Maybe not
    • % (ThreadName) s thread name. Maybe not
    • % (Process) d process ID. Maybe not
    • Message% (message) s user output
  6. logging object configuration

    • import logging
      
      logger = logging.getLogger()
      # 创建一个handler,用于写入日志文件
      fh = logging.FileHandler('test.log',encoding='utf-8') 
      
      # 再创建一个handler,用于输出到控制台 
      ch = logging.StreamHandler() 
      formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
      
      fh.setLevel(logging.DEBUG)
      
      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')
    • logging library provides a number of components: Logger, Handler, Filter, Formatter. Logger object provides an interface application can be used directly, Handler send logs to the appropriate destination, Filter provides a method of filtering log information, Formatter display format specified log. Further, by: logger.setLevel (logging.Debug) set the level, of course, can also

    • fh.setLevel (logging.Debug) set a single-file stream level.

  7. package

    • About package: the package is a python module is organized by namespace using the 'module name'. Specifically: a package that contains the __init__.py file folder, so in fact our purpose is to create a package with the file folder / modules organized

    • It is emphasized that:

      1. In python3 in, even if no package __init__.py file, import the package is still not being given, but in python2, the next package must have the file, or import the package error
      2. The purpose is not to create a package to run, but was introduced into use, remember, is only one form of bag module only, the package is a modular nature
    • Why use package

    • The nature of the package is a folder, the folder only function is to organize your files together
      with the function write more and more, so we could not function are placed in a file, so we use the module to organize functions, and as more and more modules, we need to use the module file folders to organize themselves in order to improve the structural and maintainability program

    • glance/                   #Top-level package
      
      ├── __init__.py      #Initialize the glance package
      
      ├── api                  #Subpackage for api
      
      │   ├── __init__.py
      
      │   ├── policy.py
      
      │   └── versions.py
      
      ├── cmd                #Subpackage for cmd
      
      │   ├── __init__.py
      
      │   └── manage.py
      
      └── db                  #Subpackage for db
      
          ├── __init__.py
      
          └── models.py
    • Note entry:

      • #1.关于包相关的导入语句也分为import和from ... import ...两种,但是无论哪种,无论在什么位置,在导入时都必须遵循一个原则:凡是在导入时带点的,点的左边都必须是一个包,否则非法。可以带有一连串的点,如item.subitem.subsubitem,但都必须遵循这个原则。但对于导入后,在使用时就没有这种限制了,点的左边可以是包,模块,函数,类(它们都可以用点的方式调用自己的属性)。
        
        #2、import导入文件时,产生名称空间中的名字来源于文件,import 包,产生的名称空间的名字同样来源于文件,即包下的__init__.py,导入包本质就是在导入该文件
        
        #3、包A和包B下有同名模块也不会冲突,如A.a与B.a来自俩个命名空间
  8. File Content: Press Ctrl + C copy the code; exemplary execution file with the file in the same directory

  9. Use of the package import

    • import glance.db.models
      glance.db.models.register_models('mysql') 
    • Not imported package all in all submodules when introduced alone package name, as

    • #在与glance同级的test.py中
      import glance
      glance.cmd.manage.main()
      
      '''
      执行结果:
      AttributeError: module 'glance' has no attribute 'cmd'
      
      ''' 
    • Solution:

    • #glance/__init__.py
      from . import cmd
      
      #glance/cmd/__init__.py
      from . import manage
    • carried out:

    • #在于glance同级的test.py中
      import glance
      glance.cmd.manage.main()
  10. Use of the package from ... import ...

    • Note that after the import module import from, must be clear not a little, or there will be a syntax error, such as: from a import bc wrong syntax

    • from glance.db import models
      models.register_models('mysql')
      
      from glance.db.models import register_models
      register_models('mysql')
  11. from glance.api import *

    • When talking about the module, we have already discussed all imported from within a module , here we import all research from a package .

    • Here is all you want to import from the package api, the fact that the statement will only import the package api under __init name .py file defined, we can define in this file _all :

    • # 在__init__.py中定义
      x=10
      
      def func():
          print('from api.__init.py')
      
      __all__=['x','func','policy']
    • At this point we glance peer file is executed from glance.api import * would import the contents of __all__ (versions still can not import).

    • # 在__init__.py中定义
      x = 10
      
      def func():
          print('from api.__init.py')
      
      __all__=['x','func','policy']
    • At this point we glance peer file is executed from glance.api import * would import the contents of __all__ (versions still can not import).

    • Exercise:

    • # 执行文件中的使用效果如下,请处理好包的导入
      from glance import *
      
      get()
      create_resource('a.conf')
      main()
      register_models('mysql')
  12. Importing absolute and relative import

    • Our top package glance was written by someone else, and then there will be demand for imported between each other in mutual glance inside the package, this time there is an absolute and relative import import in two ways:

    • Absolute imports: the glance as a starting

    • Introducing relative: .. manner or with most start (only in a package, it can not be used within different directories).

    • For example: We want to import glance / cmd / manage.py at glance / api / version.py in

    • 在glance/api/version.py
      
      # 绝对导入
      from glance.cmd import manage
      manage.main()
      
      # 相对导入
      from ..cmd import manage
      manage.main()
    • Test results: Be sure to note that glance peer file test

    • from glance.api import versions 
    • And means packet included in the packet is intended to be introduced, rather than being directly executed. The environment variables are subject to execution of a file

    • For example, we want to import glance / api / policy.py at glance / api / versions.py, some students maybe a pumping module is in the same directory, very happy to do it, do it directly

    • # 在version.py中
      
      import policy
      policy.get()
    • Yes, we run a separate version.py is not a little problem, run version.py the search path is the current path from the beginning, it was able to find in the import policy in the current directory

    • But you think, ah, you version.py sub-module package is likely to be imported into other documents a glance package at the same level, such as a test.py that we file under the same level of import version.py glance, as follows

    • from glance.api import versions
      
      '''
      执行结果:
      ImportError: No module named 'policy'
      '''
      
      '''
      分析:
      此时我们导入versions在versions.py中执行
      import policy需要找从sys.path也就是从当前目录找policy.py,
      # 这必然是找不到的
      '''
  13. Absolute and relative import import summary

    • 绝对导入与相对导入
      
      # 绝对导入: 以执行文件的sys.path为起始点开始导入,称之为绝对导入
      #       优点: 执行文件与被导入的模块中都可以使用
      #       缺点: 所有导入都是以sys.path为起始点,导入麻烦
      
      # 相对导入: 参照当前所在文件的文件夹为起始开始查找,称之为相对导入
      # 符号: .代表当前所在文件的文件加,..代表上一级文件夹,...代表上一级的上一级文件夹
      #       优点: 导入更加简单
      #       缺点: 只能在导入包中的模块时才能使用
      # 注意:
      #  1. 相对导入只能用于包内部模块之间的相互导入,导入者与被导入者都必须存在于一个包内
      #    2. attempted relative import beyond top-level package # 试图在顶级包之外使用相对导入是错误的,言外之#       意,必须在顶级包内使用相对导入,每增加一个.代表跳到上一级文件夹,而上一级不应该超出顶级包

Guess you like

Origin www.cnblogs.com/zhangdadayou/p/11415293.html