1. 程式人生 > >序列化 ,hashlib ,configparser ,logging ,collections模塊

序列化 ,hashlib ,configparser ,logging ,collections模塊

.config try def parser \n -- test setting pass

# 實例化  歸一化 初始化 序列化
# 列表 元組 字符串
# 字符串
# .......得到一個字符串的結果 過程就叫序列化
# 字典 / 列表 / 數字 /對象 -序列化->字符串
# 為什麽要序列化
# 1.要把內容寫入文件 序列化
# 2.網絡傳輸數據 序列化
# 字符串-反序列化->字典 / 列表 / 數字 /對象

# 方法
# dic = {‘k‘:‘v‘}
# str_dic = str(dic)
# print(dict(str_dic))
# print([eval(str_dic)])
# eval不能隨便用

# json
import json
# 只提供四個方法
# dic = {‘aaa‘:‘bbb‘,‘ccc‘:‘ddd‘}

# str_dic = json.dumps(dic)
# print(dic)
# print(str_dic,type(str_dic))
# with open(‘json_dump‘,‘w‘) as f:
# f.write(str_dic)
# ret = json.loads(str_dic)
# print(ret,type(ret))
# print(ret[‘aaa‘])

# dic = {‘aaa‘:‘bbb‘,‘ccc‘:‘ddd‘}
# with open(‘json_dump2‘,‘w‘) as f:
# json.dump(dic,f)

# with open(‘json_dump2‘) as f:

# print(type(json.load(f)))
import json
# json格式的限制1,json格式的key必須是字符串數據類型
# json格式中的字符串只能是""

# 如果是數字為key,那麽dump之後會強行轉成字符串數據類型
# dic = {1:2,3:4}
# str_dic = json.dumps(dic)
# print(str_dic)
# new_dic = json.loads(str_dic)
# print(new_dic)

# json是否支持元組,對元組做value的字典會把元組強制轉換成列表
# dic = {‘abc‘:(1,2,3)}
# str_dic = json.dumps(dic)

# print(str_dic)
# new_dic = json.loads(str_dic)
# print(new_dic)

# json是否支持元組做key,不支持
# dic = {(1,2,3):‘abc‘}
# str_dic = json.dumps(dic) # 報錯

# 對列表的dump
# lst = [‘aaa‘,123,‘bbb‘,12.456]
# with open(‘json_demo‘,‘w‘) as f:
# json.dump(lst,f)
# with open(‘json_demo‘) as f:
# ret = json.load(f)
# print(ret)

# 能不能多次dump數據到文件裏,可以多次dump但是不能load出來了
# dic = {‘abc‘:(1,2,3)}
# lst = [‘aaa‘,123,‘bbb‘,12.456]
# with open(‘json_demo‘,‘w‘) as f:
# json.dump(lst,f)
# json.dump(dic,f)
# with open(‘json_demo‘) as f:
# ret = json.load(f)
# print(ret)

# 想dump多個數據進入文件,用dumps
# dic = {‘abc‘:(1,2,3)}
# lst = [‘aaa‘,123,‘bbb‘,12.456]
# with open(‘json_demo‘,‘w‘) as f:
# str_lst = json.dumps(lst)
# str_dic = json.dumps(dic)
# f.write(str_lst+‘\n‘)
# f.write(str_dic+‘\n‘)

# with open(‘json_demo‘) as f:
# for line in f:
# ret = json.loads(line)
# print(ret)

# 中文格式的 ensure_ascii = False
# dic = {‘abc‘:(1,2,3),‘country‘:‘中國‘}
# ret = json.dumps(dic,ensure_ascii = False)
# print(ret)
# dic_new = json.loads(ret)
# print(dic_new)

# with open(‘json_demo‘,‘w‘,encoding=‘utf-8‘) as f:
# json.dump(dic,f,ensure_ascii=False)

# json的其他參數,是為了用戶看的更方便,但是會相對浪費存儲空間
# import json
# data = {‘username‘:[‘李華‘,‘二楞子‘],‘sex‘:‘male‘,‘age‘:16}
# json_dic2 = json.dumps(data,sort_keys=True,indent=4,separators=(‘,‘,‘:‘),ensure_ascii=False)
# print(json_dic2)

# set不能被dump/dumps

********************************************************************************************
import  pickle
# dump的結果是bytes,dump用的f文件句柄需要以wb的形式打開,load所用的f是‘rb‘模式
# 支持幾乎所有對象的序列化
# 對於對象的序列化需要這個對象對應的類在內存中
# 對於多次dump/load的操作做了良好的處理

# pic_dic = pickle.dumps(dic)
# print(pic_dic) # bytes類型
# new_dic = pickle.loads(pic_dic)
# print(new_dic)

# pickle支持幾乎所有對象的
# class Student:
# def __init__(self,name,age):
# self.name = name
# self.age = age
#
# alex = Student(‘alex‘,83)
# ret = pickle.dumps(alex)
# 小花 = pickle.loads(ret)
# print(小花.name)
# print(小花.age)

# class Student:
# def __init__(self,name,age):
# self.name = name
# self.age = age
#
# alex = Student(‘alex‘,83)
# with open(‘pickle_demo‘,‘wb‘) as f:
# pickle.dump(alex,f)
# with open(‘pickle_demo‘,‘rb‘) as f:
# 旺財 = pickle.load(f)
# print(旺財.name)

# 學員選課系統 pickle模塊來存儲每個學員的對象

# with open(‘pickle_demo‘,‘wb‘) as f:
# pickle.dump({‘k1‘:‘v1‘}, f)
# pickle.dump({‘k11‘:‘v1‘}, f)
# pickle.dump({‘k11‘:‘v1‘}, f)
# pickle.dump({‘k12‘:[1,2,3]}, f)
# pickle.dump([‘k1‘,‘v1‘,‘l1‘], f)

# with open(‘pickle_demo‘,‘rb‘) as f:
# while True:
# try:
# print(pickle.load(f))
# except EOFError:
# break
******************************************************************************************** 
import shelve
f = shelve.open(‘shelve_demo‘)
f[‘key‘] = {‘k1‘:(1,2,3),‘k2‘:‘v2‘}
f.close()

# f = shelve.open(‘shelve_demo‘)
# content = f[‘key‘]
# f.close()
# print(content)

# shelve 如果你寫定了一個文件
# 改動的比較少
# 讀文件的操作比較多
# 且你大部分的讀取都需要基於某個key獲得某個value
********************************************************************************************  

# 摘要算法的模塊
import hashlib
# 能夠把 一個 字符串 數據類型的變量
# 轉換成一個 定長的 密文的 字符串,字符串裏的每一個字符都是一個十六進制數字

# 對於同一個字符串,不管這個字符串有多長,只要是相同的,
# 無論在任何環境下,多少次執行,在任何語言中
# 使用相同的算法\相同的手段得到的結果永遠是相同的
# 只要不是相同的字符串,得到的結果一定不同

# 登錄的密文驗證
# ‘alex3714‘ # -> ‘127649364964908724afd‘

# 字符串 --> 密文
# 密文 不可逆的 字符串

# 1234567 - > ‘127649364964908724afd‘
# 算法 : 對於同一個字符串,用相同的算法,相同的手段去進行摘要,獲得的值總是相同的
# 1234567 - > ‘127649364964908724afd‘


# s1 = ‘alex3714‘ # aee949757a2e698417463d47acac93df
# s2 = ‘alex3714qwghkdblkasjbvkhoufyowhdjlbvjnjxc‘ # d2d087c10aeba8276b21f8697ad3e810
# md5是一個算法,32位的字符串,每個字符都是一個十六進制
# md5算法 效率快 算法相對簡單
# md5_obj = hashlib.md5()
# md5_obj.update(s1.encode(‘utf-8‘))
# res = md5_obj.hexdigest()
# print(res,len(res),type(res))

# 數據庫 - 撞庫
# 111111 --> 結果
# 666666
# 123456
# alex3714 --> aee949757a2e698417463d47acac93df

# s1 = ‘123456‘
# md5_obj = hashli b.md5()
# md5_obj.update(s1.encode(‘utf-8‘))
# res = md5_obj.hexdigest()
# print(res,len(res),type(res))

# 加鹽 # alex3714 d3cefe8cdd566977ec41566f1f11abd9
# md5_obj = hashlib.md5(‘任意的字符串作為鹽‘.encode(‘utf-8‘))
# md5_obj.update(s1.encode(‘utf-8‘))
# res = md5_obj.hexdigest()
# print(res,len(res),type(res))


# 惡意用戶 註冊500個賬號
# 張三|123456 ‘任意的字符串作為鹽‘.encode(‘utf-8‘) d3cefe8cdd566977ec41566f1f11abd8
# 李四|111111


# 動態加鹽
# username = input(‘username : ‘)
# passwd = input(‘password : ‘)
# md5obj = hashlib.md5(username.encode(‘utf-8‘))
# md5obj.update(passwd.encode(‘utf-8‘))
# print(md5obj.hexdigest())
# ee838c58e5bb3c9e687065edd0ec454f


# sha1也是一個算法,40位的字符串,每個字符都是一個十六進制
# 算法相對復雜 計算速度也慢
# md5_obj = hashlib.sha1()
# md5_obj.update(s1.encode(‘utf-8‘))
# res = md5_obj.hexdigest()
# print(res,len(res),type(res))


# 文件的一致性校驗
# md5_obj = hashlib.md5()
# with open(‘5.序列化模塊_shelve.py‘,‘rb‘) as f:
# md5_obj.update(f.read())
# ret1 = md5_obj.hexdigest()
#
# md5_obj = hashlib.md5()
# with open(‘5.序列化模塊_shelve.py.bak‘,‘rb‘) as f:
# md5_obj.update(f.read())
# ret2 = md5_obj.hexdigest()
# print(ret1,ret2)

# 如果這個文件特別大,內存裝不下
# 8g 10g
# 按行讀 文本 視頻 音樂 圖片 bytes
# 按字節讀 23724873 10240

# md5_obj = hashlib.md5()
# md5_obj.update(‘hello,alex,sb‘.encode(‘utf-8‘))
# print(md5_obj.hexdigest())

# md5_obj = hashlib.md5()
# md5_obj.update(‘hello,‘.encode(‘utf-8‘))
# md5_obj.update(‘alex,‘.encode(‘utf-8‘))
# md5_obj.update(‘sb‘.encode(‘utf-8‘))
# print(md5_obj.hexdigest())

# 大文件的已執行校驗

md5_obj = hashlib.md5()
with open(‘5.序列化模塊_shelve.py.bak‘,‘rb‘) as f:
md5_obj.update(f.read())
# 循環 循環的讀取文件內容
# 循環的來update
print(md5_obj.hexdigest())
********************************************************************************************

import configparser
# file類型
# f = open(‘setting‘)

# 有一種固定格式的配置文件
# 有一個對應的模塊去幫你做這個文件的字符串處理

# settings.py 配置

# import configparser
#
# config = configparser.ConfigParser()
#
# config["DEFAULT"] = {‘ServerAliveInterval‘: ‘45‘,
# ‘Compression‘: ‘yes‘,
# ‘CompressionLevel‘: ‘9‘,
# ‘ForwardX11‘:‘yes‘
# }
#
# config[‘bitbucket.org‘] = {‘User‘:‘hg‘}
#
# config[‘topsecret.server.com‘] = {‘Host Port‘:‘50022‘,‘ForwardX11‘:‘no‘}
#
# with open(‘example.ini‘, ‘w‘) as f:
# config.write(f)


import configparser

config = configparser.ConfigParser()
# print(config.sections()) # []
config.read(‘example.ini‘)
# print(config.sections()) # [‘bitbucket.org‘, ‘topsecret.server.com‘]
# print(‘bytebong.com‘ in config) # False
# print(‘bitbucket.org‘ in config) # True
# print(config[‘bitbucket.org‘]["user"]) # hg
# print(config[‘DEFAULT‘][‘Compression‘]) #yes
# print(config[‘topsecret.server.com‘][‘ForwardX11‘]) #no
# print(config[‘bitbucket.org‘]) #<Section: bitbucket.org>
# for key in config[‘bitbucket.org‘]: # 註意,有default會默認default的鍵
# print(key)
# print(config.options(‘bitbucket.org‘)) # 同for循環,找到‘bitbucket.org‘下所有鍵
# print(config.items(‘bitbucket.org‘)) #找到‘bitbucket.org‘下所有鍵值對
# print(config.get(‘bitbucket.org‘,‘compression‘)) # yes get方法Section下的key對應的value
********************************************************************************************
import logging
# 功能
# 1. 日誌格式的規範
# 2. 操作的簡化
# 3. 日誌的分級管理

# logging不能幫你做的事情
# 自動生成你要打印的內容
# 需要程序員自己在開發的時候定義好 :
# 在哪些地方需要打印,要打印的內容是什麽,內容的級別

# logging模塊的使用 :
# 普通配置型 簡單的 可定制化差
# 對象配置型 復雜的 可定制化強

# 認識日誌分級

# import logging
# logging.debug(‘debug message‘) # 調試模式
# logging.info(‘info message‘) # 基礎信息
# logging.warning(‘warning message‘) # 警告
# logging.error(‘error message‘) # 錯誤
# logging.critical(‘critical message‘)# 嚴重錯誤


# import logging
# logging.basicConfig(level=logging.DEBUG)
# logging.debug(‘debug message‘) # 調試模式
# logging.info(‘info message‘) # 基礎信息
# logging.warning(‘warning message‘) # 警告
# logging.error(‘error message‘) # 錯誤
# logging.critical(‘critical message‘)# 嚴重錯誤



# 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=‘test.log‘)
# logging.debug(‘debug message‘) # 調試模式
# logging.info(‘info message‘) # 基礎信息
# logging.warning(‘warning message‘) # 警告
# logging.error(‘error message‘) # 錯誤
# logging.critical(‘critical message‘)# 嚴重錯誤

# basicConfig
# 不能將一個log信息既輸出到屏幕 又輸出到文件

# logger對象的形式來操作日誌文件

# 創建一個logger對象
# 創建一個文件管理操作符
# 創建一個屏幕管理操作符
# 創建一個日誌輸出的格式

# 文件管理操作符 綁定一個 格式
# 屏幕管理操作符 綁定一個 格式

# logger對象 綁定 文件管理操作符
# logger對象 綁定 屏幕管理操作符

# import logging
# # 創建一個logger對象
# logger = logging.getLogger()
# # 創建一個文件管理操作符
# fh = logging.FileHandler(‘logger.log‘,encoding=‘utf-8‘)
# # 創建一個屏幕管理操作符
# sh = logging.StreamHandler()
# # 創建一個日誌輸出的格式
# format1 = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)
#
# # 文件管理操作符 綁定一個 格式
# fh.setFormatter(format1)
# # 屏幕管理操作符 綁定一個 格式
# sh.setFormatter(format1)
# logger.setLevel(logging.DEBUG)
# # logger對象 綁定 文件管理操作符
# logger.addHandler(fh)
# # logger對象 綁定 屏幕管理操作符
# logger.addHandler(sh)
#
# logger.debug(‘debug message‘) # 調試模式
# logger.info(‘我的信息‘) # 基礎信息
# logger.warning(‘warning message‘) # 警告
# logger.error(‘error message‘) # 錯誤
# logger.critical(‘critical message‘)# 嚴重錯誤

import logging
a=logging.getLogger()
fh=logging.FileHandler(‘a1‘,encoding=‘utf-8‘)
sh=logging.StreamHandler()

format1=logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)
fh.setFormatter(format1)
sh.setFormatter(format1)

a.setLevel(logging.DEBUG)

a.addHandler(fh)
a.addHandler(sh)

a.debug(‘debug message‘) # 調試模式
a.info(‘我的信息‘) # 基礎信息
a.warning(‘warning message‘) # 警告
a.error(‘error message‘) # 錯誤
a.critical(‘critical message‘)# 嚴重錯誤


input("")
import logging
logging.basicConfig(level=logging.DEBUG, #修改 DEBUG,INFO
format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘,
datefmt=‘%a, %d %b %Y %H:%M:%S‘)
exp = 3+4/4
logging.debug(str(4/4))
print(4)
********************************************************************************************

# collections模塊
# 數據類型的擴展模塊

# 什麽是隊列
# 先進先出
# import queue
# q = queue.Queue()
# print(q.qsize())
# q.put(1)
# q.put(‘a‘)
# q.put((1,2,3))
# q.put(({‘k‘:‘v‘}))
# print(q.qsize())
# print(‘q : ‘,q)
# print(‘get : ‘,q.get())
# print(q.qsize())

# deque 雙端隊列
# from collections import deque
# dq = deque()
# dq.append(2)
# dq.append(5)
# dq.appendleft(‘a‘)
# dq.appendleft(‘b‘)
# print(dq)
# # print(dq.pop())
# # print(dq)
# # print(dq.popleft())
# # print(dq)
# print(dq.remove(‘a‘))
# print(dq.insert(2,‘123‘))
# print(dq)

# 總結
# 在insert remove的時候 deque的平均效率要高於列表
# 列表根據索引查看某個值的效率要高於deque
# append 和pop對於列表的效率是沒有影響
























序列化 ,hashlib ,configparser ,logging ,collections模塊