其他不常用模块

18 其他不常用模块

猴子补丁
import json  # ujson
import ujson

def monkey_patch():
    json.loads = ujson.loads
    json.dumps = ujson.dumps

monkey_patch()


import shelve

# 序列化
f = shelve.open(r'sheve.txt')
f['stu1_info'] = {'name': 'EGON', 'age': 28, 'hobby': ['piao', 'smoking', 'drinking']}
f['stu2_info'] = {'name': 'gangdan', 'age': 53}
f['school_info'] = 123123123123123123123123123
f['test'] = {1,2,3,4,5}

# print(f['stu1_info']['hobby'])
f.close()


# 反序列化
import shelve

f=shelve.open(r'sheve.txt')


print(f['stu1_info']['name'])
print(f['school_info'])
print(f['test'])
f.close()


import configparser

config = configparser.ConfigParser()
config.read('config.ini')

res = config.sections()
print(res)

res = config.options('section1')
print(res)

res = config.items('section1')
print(res)

res = config.get('section1','is_admin')
res = bool(res)

res = config.getboolean('section1','is_admin')
print(res,type(res))

res = config.get('section1','salary')
res = config.getfloat('section1','salary')
res = config.getint('section1','salary')
print(res,type(res))

hashlib

# hash值有三大特点:
# 1、只要传入的原材料一样、并且hash算法一样,那么得到的hash值一定是一样的
# 2、hash值的长度取决于hash算法,与传入的内容大小无关
# 3、hash值不可逆

# 1+2==》文件完整性校验
import hashlib

with open(r'D:\全栈18期\day17\视频\01 日志模块.mp4',mode='rb') as f:
    m = hashlib.md5()

    for line in f:
        m.update(line)

    print(m.hexdigest())




# 3====》加密

import hashlib



# m = hashlib.md5()
# m.update("hello".encode('utf-8'))
# m.update("w".encode('utf-8'))
# m.update("orld".encode('utf-8'))  # "helloworld"
#
# print(m.hexdigest())
#
#
# m1 = hashlib.md5()
# m1.update("he".encode('utf-8'))
# m1.update("llo".encode('utf-8'))
# m1.update("world".encode('utf-8'))  # "helloworld"
#
# print(m1.hexdigest())



# m1 = hashlib.md5()
# m1.update("he".encode('utf-8'))
# m1.update("llo".encode('utf-8'))
# m1.update("world".encode('utf-8'))  # "helloworld"
# print(m1.hexdigest())



# pwd =  '123'
#
# m = hashlib.md5()
# m.update("天王盖地虎".encode('utf-8'))
# m.update(pwd.encode('utf-8'))
# m.update("小鸡炖蘑菇".encode('utf-8'))
# print(m.hexdigest())
#








posted @ 2021-08-17 19:16  迪迦张  阅读(30)  评论(0编辑  收藏  举报