pickle \ json, configparser, hashlib module

Common python module

json module \ pickle module

First talk about serialization and de-serialization.

  1. Serialization: the data content into a specific format.
  2. Deserialization: The particular format is converted into the data content.

In fact, we learned before serialization and de-serialization method, data will be in memory of the deposit into a format string into the file, in the method from a file using the eval () deserialization out. It is also a way, but now there is a more convenient way.

json serialization module is a module, there are many types of languages ​​available in the market, every language has its own unique structure, when the exchange of data between the different languages, can be employed json module, and it is converted into a variable content special format, show it is a kind of string that can be used in all languages, plays an important role in cross-platform communication.

The other is that if we play the game, you want to save your current status, help us next time you go to play, you can also use serialization to our information temporarily stored up again when the next time you log into anti-sequence current content.

Depending on the purpose of use, we can use different modules, if we just to store the current state of the case, it is recommended to use the pickle module, but if the data transmission if the cross-platform, then it would need to use a json module.

How to use the serialization and de-serialization of it?

import json

# json序列化
a_json = json.dumps("{1,2,3,4,5}")

# json反序列化
json.loads(a_json)

dumps and loads are corresponding to the above transformation only in memory, if you want to store, then you need to use the following this method.

import json

a = {1,2,3,4,5}
# json序列化到文件
with open("a.txt","w",encoding="utf-8") as f:
	f.dump(a,f)
	
# json反序列化文件
with open("a.txt","r",encoding="utf-8") as f:
	f.loads(f)

json do not need to understand the unique structure of each language, it is responsible for the content into a form that we all know, the perfect realization of data sharing in different locales.

pickle is a python own unique serialization module, and use his hair as a json, json can use the reference method, if you do not generally involve cross-platform, cross-lingual exchange of data, it is recommended to use the pickle module.

tips:

If we are after a lot of use of a module A, suddenly I found a better one than this module + module A function of, not only to achieve the function A, still more perfect and efficient. At this time, how should we do it?

Of course, choose to use the new, so the question is, if we use the new features, so one by one before us can not be used to change it, this time we should use a programming thinking: monkey patch. In the software directory specification, we all start files started the whole process, if we change this functionality directly at the entrance, we can once and for all solve this new problem.

# 使用ujson来替换json中的dump和load。
import json
import ujson
def monkey_patch():
	json.dumps = ujson.dumps
	json.loads = ujson.loads
monkey_patch()

configparser module

The amount of this module is the main role of content in a specific format to read the end of the .ini file. In the document content mainly sections: options to store the format.

# a.ini中的内容
[section1]  # 这个相当于分割线,是一个名为section1变量的内容
name="tom"
age=18

[section2]
name="jerry"
age=12

DETAILED similar section1 = { "name": "tom", "age" = 18}, but the latter section is the former option..

import configparser

config = congigparser.ConfigParser()
config.read("a.ini")

# 获取sections
print(config.sections())

# 获取options
print(config.options())

# 取出tom
name = config.get("section1","name")

hashlib module

a hash algorithm incoming value inside a series of complex algorithms to obtain a value of a fixed length.

  1. The same as long as the incoming content, then the hash value obtained is the same.
  2. hash worth length is fixed.
  3. Value can not be launched by the anti-enter.

The first two characteristics are mainly used to verify the integrity of files, while the third is perfect performance characteristics of its application to set a password. After all, we who do not want their passwords easily be exploited by others.

# hashlib模块的使用。
import hashlib

m = hashlib.md5() # 得到一个工厂。
m.update("123".encode("utf-8"))
m.update("456".encode("utf-8"))
hasd_value = m.hexdigest()  # 123456的hash值

"Hit Library" is a way to guess passwords, user names plaintext password to guess some of the possible use, and then hit the jackpot with the way one by one these values ​​to obtain the hash value by the same algorithm and then to test whether you can be verified.

To combat this behavior when a user password can be entered via the '' Salt '' operation to increase the difficulty of guessing. That added to the user's password in something extra, then obtain the hash value stored in the database is such a hash value.

subprocess module

This module can be run according to different user commands result, different input return.

import subprocess

obj = subprocess.Popen("echo 123;ls/;ls/root",shell=True,
						stdout=subprocess.PIPE,
						stdeer=subprocess.PIPE,)

# 命令执行成功
msg = obj.stdout.read().decode("GBK")

# 命令执行失败
msg = obj.stdeer.read().decode("GBK")

Decoding the specified character encoding is character encoding system itself.

Guess you like

Origin www.cnblogs.com/liqianxin/p/12606971.html