Python notes 6

Source: Internet
Author: User

Log module

1 Logger object used to generate the log
Logger1 = Logging.getlogger ("Banking Business related")
Log names are log-related information used to differentiate records

2 Filter object for filtering logs
3 Formmatter Object Custom log format

4 The whereabouts of the handler object control log 1 file 2 terminal
FH1 = logging. Filehandler ("A1.log", "Encoding=utf-8")

ch = logging. Streamhandler ()

# #建立logger对象与handler的绑定对象
Logger1.addhandler (FH1)

Logger1.addhandler (CH)

#format object, customizing the format of the log
Formatter1 = logging. Formatter (
FMT = ""
DATEFMT =
)

To bind a log format to a handler object

Fh1.setformatter (Formatter)

# # D set Log level '
1 Logger
2 handler only 2 layers of control, the final log will enter the object
Logger.setlevel (10)

# #格式
Import OS
LOGGING = {
' Version ': 1,
' Disable_existing_loggers ': True,
' formatters ': {
' Lqm_format ': {
' Format ': '% (levelname) s% (asctime) s% (module) s% (process) d% (thread) d% (message) s '
},
' Simple ': {
' Format ': '% (levelname) s% (message) s '
},
},
' Filters ': {
},
' Handlers ': {
' System ': {
' Class ': ' Logging.handlers.TimedRotatingFileHandler ',
# # ' filename ': os.getenv (' Logs_path ', ' LOGS ') + "/" + "System.log",
' filename ': Os.path.join (os.getenv (' Logs_path ', ' LOGS '), "System.log"),
' Formatter ': ' Lqm_format ',
' When ': ' Midnight ',
' Interval ': 1,
' Backupcount ': 30,
},
' User ': {
' Class ': ' Logging.handlers.TimedRotatingFileHandler ',
' filename ': os.getenv (' Logs_path ', ' LOGS ') + "/" + "User.log",
' Formatter ': ' Lqm_format ',
' When ': ' Midnight ',
' Interval ': 1,
' Backupcount ': 30,
},
},
' Loggers ': {
' User ': {
' Handlers ': [' user ',],
' Level ': os.getenv (' logs_level ', ' DEBUG '),
' Propagate ': True,
},
' System ': {
' Handlers ': [' system ',],
' Level ': os.getenv (' logs_level ', ' DEBUG '),
' Propagate ': True,
},
}
}

Serialization of two
Json
Only some Python data types are supported, and the language type supports
Pickle
Python support only, including all Python types

DIC = {' name ': ' Esz '}
With open ("User.json", W) as F:
# #等于将dic的文件, write to F
Json.dump (DIC,F)
# # alone F data and put in Dict
DIC = Json.load (f)

1 Import Pickle
2
3 dic={' name ': ' Alvin ', ' age ': at all, ' sex ': ' Male '}
4
5 Print (Type (DIC)) #<class ' Dict ' >
6
7 J=pickle.dumps (DIC)
8 Print (Type (j)) #<class ' bytes ' >
9
10
One F=open (' serialized object _pickle ', ' WB ') #注意是w是写入str, WB is written bytes,j is ' bytes '
F.write (j) #-------------------equivalent to Pickle.dump (dic,f)
13
F.close ()
#-------------------------Deserialization
Import Pickle
F=open (' serialized object _pickle ', ' RB ')
18
Data=pickle.loads (F.read ()) # equivalent to Data=pickle.load (f)
20
21st
Print (data[' age ')

Three-hash module

 hash 1  校验文本内容      2  hash 固定      3 明文转密码进行传输m = hashlib.md5()m.update(b"hello")   这个等于  m.update("hello".encode("utf-8"))m.update(b"ss")m.hexdigest() 获取到密文密码import hmacm = hmac.new(‘sss‘.encode("utf-8"))m1=hashlib.sha256()m1.update(‘hello‘.encode(‘utf-8‘))print(m1.hexdigest())m2=hashlib.sha512()m2.update(‘hello‘.encode(‘utf-8‘))print(m2.hexdigest())

Four RE modules
Regular with a bunch of special symbols to go to the contents of a text/string matching symbol rule
Re.findall ("\w", str)

重复匹配  。 ? * + {m,n}print(re.findall)

Import re

Line breaks do not match

Print (Re.findall ("A.C", "123131asc,ssfafaa-c"))

Even if line breaks are matched

Print (Re.findall ("A.C", "123131asc,ssf\nafaa-c", re. Dotall))

? Match 0 or one, there must match, if not also can

Print (Re.findall (' ab? ', "AA Ssafsfaabbssa"))

* Match 0 or infinity + match one or infinity {n,m}print (Re.findall (' \d+. \d+ '). * Greedy match, select the most value

Print (Re.findall ("A.*c", ' assfafasfcssssfafsfsf111223c '))

Match to assfafasfcssssfafsfsf111223c. A non-greedy match that matches the value of a compound that is less early
Print (Re.findall ("A.
C ", ' assfafasfcssssfafsfsf111223c ')) match to ASFC Group () with! Conpan (ies|y) can match ies with Yconpan (? ies|y) can be matched to conpanies and Conpany

Print (Re.findall (' href= ' (.) "', ' click ')) #[' http://www.baidu.com ']
Print (Re.findall (' href= ' (?:.
) "', ' click ')) #[' href=" http://www.baidu.com "']

[] The match is also a character, but the character must be defined within []

Print (Re.findall (' A[a-za-z]c ', "ABCSACADCAOC"))
Rex = ' a[+-*/] ' # #去匹配 +,-*,/

Print (Re.findall (' e ', ' Alex Make Love ')

Python notes 6

Related Article

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.