2019.8.16 learning content plus quizzes notes

summary

package

Is simple to understand the role of the packet is used to separate files, or had only one module of a document or file it in order to function inside the module (function) divided by the demand, the function will be of the same type (function) were placed in different file or module (package is based on the new file folders inside (the module))

Package, a module is to introduction into a plurality of same way to use the package after introducing the same way, the user can not feel change

Package is actually a folder (but this file within that folder must contain _ _ init_ _ .py)

* init can make an ordinary folder into a package, import the package is to import init

The first use of the package (not recommended), temporary for me, in what would be to use what kind of

* Init guide package is introduced

Package of environmental variables executable file as a reference, init package can only start importing from the package name

Import introducing relative and absolute, relative introduced only in the package

. (A point is the current directory)

.. (is a directory of the current directory (folder))

time module

time action module: between supports three different types of time, different forms can be converted each time

Commonly used time module usage

import time
# 时间戳
#print(time.time()) #1566008722.4859853

#格式化时间
#print(time.strftime('%Y_%m_%d %X')) #2019_08_17 10:27:44

#结构化时间
#localtime():表示中国时间
#print(time.localtime()) #time.struct_time(tm_year=2019, tm_mon=8, tm_mday=17, tm_hour=10, tm_min=29, tm_sec=8, tm_wday=5, tm_yday=229, tm_isdst=0)

# gmtime()标准时间
#print(time.gmtime()) #time.struct_time(tm_year=2019, tm_mon=8, tm_mday=17, tm_hour=2, tm_min=31, tm_sec=5, tm_wday=5, tm_yday=229, tm_isdst=0)

Three kinds of format conversion time (no need to remember)

Key j remember:

time.time()
time.sleep(2)

datetime module

For modification time: datetime module effect

import datetime
print(datetime.datetime.now())
now = datetime.datetime.now()
print(now + datetime.timedelta(3)) #默认+3天
print(now + datetime.timedelta(hours=3)) # +3小时
print(now+ datetime.timedelta(minutes=3)) #+3 分钟

print(now.replace(year=1900))

os module

os module role: to interact with the operating system, you can manipulate files

# import os
#print(os.getcwd()) # 获取当前文件目录

#os.mkdir('m3') # 创建文件
#os.rmdir('m3') # 删除文件

###############(重点,经常使用)
# res = os.listdir(r'E:\老男孩')
# print(res) #列出所有的文件

#os.rename('日常学习总结.py','总结.py')#修改文件名
# os.remove('price.txt') #删除文件

# __ file__只有pycharm才提供,python本身不支持
#print('os.path.abspath(__file__):',os.path.abspath(__file__)) # 显示当前文件路径,支持不同平台

#print(__file__)#E:/老男孩/日常使用.py

# print(os.path.exists('bao')) #True 判断文件是否存在

#print(os.path.isfile('bao')) #False 判断是否为文件
#print(os.path.isdir('bao')) #Trun 判断是否为文件夹

###############(经常使用),支持不同平台
# res = os.path.join(r'E:\老男孩') # 拼接文件路径
# res = os.path.join(r'E:\老男孩\ATM工程')# 拼接文件路径
# print(r'E:\老男孩+E:\老男孩\ATM工程')

##########(经常使用)
# print(os.path.abspath(__file__)) #E:\老男孩\日常使用.py 打印当前文件路径
# print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))#E:\ 打印了上上级目录(

# import os
# g = os.walk(r'E:\老男孩\日常使用.py')
# for i in g:
#     print(i)

sys module

sys module effect: interaction with the Interpreter python

import sys
#print(sys.path)

#print(sys.argv) # 接收参数(用cmd执行文件时才生效)

print(sys.modules)

json module

json action module: can be serialized and deserialized, a generic data type (e.g., a program written in python, a program written in java, and now these two programs to exchange, but does not recognize the different syntax, predetermined a multilingual unified data type, json string

Serialization: from python dictionary (the most common) becomes json string with dump

f deserialized: from json into python string dictionary (the most common), with load

import json
# dict = {'a': 1, 'b': 2, 'c': None}
# data = json.dumps(dict) # 序列化到内存中<class 'str'>
# #print(data,type(data)) #{"a": 1, "b": 2, "c": null}单引号变双引号d
# data = json.loads(data)# 从内存获取json串
# print(data,type(data))#{'a': 1, 'b': 2, 'c': None} <class 'dict'>

with open(f'{"text"}.josn','r',encoding='utf-8')as fr:
    data = json.load(fr)
    print(data) #{'a': 1, 'b': 2, 'c': None}

pickle module

pickle module functions: serialize python all objects (data types), but not cross-platform (json module cross-platform)

hashlib module

hashlib module role: password encryption, no matter what you throw the string, it returns a string of fixed-length string

import hashlib
m = hashlib.md5()
# m.update(b'123456')
# print(m.hexdigest()) #e10adc3949ba59abbe56e057f20f883e

m.update(b'123')
m.update(b'456')
print(m.hexdigest())#e10adc3949ba59abbe56e057f20f883e(有叠加性)

#1.变成固定的字符串、
# 2. 相同的字符串哈希后的结果一样、
# 3. 叠加性

hmac module

hmac module functions: to encrypt the password, you can add salt

import hmac
m = hmac.new(b'a')
m.update(b'123456')
print(m.hexdigest())#8c7498982f41b93eb0ce8216b48ba21d

#m = hamc.new(b'abc)#8c7498982f41b93eb0ce8216b48ba21d
#m = hamc.new(b'a) #3e391a1d7bf574cec59679244a2904fe

# 特性:1.不同的hamc,最后打印结果不同
    #2.同样具有叠加性

logging module

logging module role: log

import logging


# logging.debug('调试')  # 10
# logging.info('正常')   # 20  # 用这一个
# logging.critical('严重错误') # 30
# logging.error('错误') # 40
# logging.warning('警告') # 50
# 默认30以上的可以输出

# v2 日志信息记录在文件当中


# logging.basicConfig(filename='access.log',
#                     format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
#                     datefmt='%Y-%m-%d %H:%M:%S %p',
#                     level=10)

# logging.debug('调试信息')  # 10
# logging.info('正常信息')  # 20
# logging.warning('警告信息')  # 30
# logging.error('报错信息')  # 40
# logging.critical('严重错误信息')  # 50

# v3




# 1. 生成一个logger对象
logger = logging.getLogger('nick')

# 2. 生成格式
formmater1 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                               datefmt='%Y-%m-%d %H:%M:%S %p',)
formmater2 = logging.Formatter('%(asctime)s :  %(message)s',
                               datefmt='%Y-%m-%d %H:%M:%S %p',)
formmater3 = logging.Formatter('%(name)s %(message)s',)

# 3. 生成一个文件对象
h1 = logging.FileHandler('h1.log')
h2 = logging.FileHandler('h2.log')
sm = logging.StreamHandler()


# 4. 文件绑定格式
h1.setFormatter(formmater1)
h2.setFormatter(formmater2)
sm.setFormatter(formmater3)

Guess you like

Origin www.cnblogs.com/chmily/p/11420835.html