Introduction to Python - DAY19-Common Module 2

shelve

What is the shelve module
  It is also a serialization method.
Use method
  1.opne 2.Read and
  write
  3.close
Features: The use method is relatively simple to provide a file name to start reading
  and writing The reading and writing method is consistent with the dictionary,
  you can use it as a tape Dictionary with automatic serialization function
Principle: Pickle is used internally, so there is also a problem of poor cross-platform performance. Only you know how to get it.

When to use: When writing a stand-alone program, consider


import shelve
# Serialization
# sl = shelve.open("shelvetest.txt")
# sl["date"] = "8-13"
# sl["list1"] = ["123","456"]
# sl.close()


# 反序列化
s2 = shelve.open("shelvetest.txt")
print(s2.get("list1"))
s2.close()

xml

What XML   is
XML: the full name of Extensible Markup Language
  Mark refers to the character representing a certain meaning   XML The syntax specification to write XML syntax format:   1. Any start tag must have an end tag.   <tagname></tagname>   <tagname></tagname>   <tagname/> Simplified writing







  Second, another simplified syntax can be used, and the start and end tags can be expressed in one tag at the same time. This syntax is followed by a slash (/) immediately before the greater-than symbol, such as <Baidu Encyclopedia entry/>. The XML parser will translate it into <Baidu Encyclopedia entry></Baidu Encyclopedia entry>.
  3. Tags must be nested in a proper order, so the end tag must match the start tag in the mirrored order, for example, this is a sample string in Baidu Encyclopedia. It's like thinking of opening and closing tags as opening and closing parentheses in mathematics: you can't close the outer brackets without closing all the inner brackets.
<tag1>
  <tag2>
    <tag3>
    </tag3>
  </tag2>
</tag1>

The vernacular closing tags should be closed layer by layer from the inside out. The order of closing should not be chaotic
  . Fourth, all features must have values.
  Attributes refer to attributes
    <person name="">
    </person>
  5. All attributes must be enclosed in double quotes around the value.

  Note: There is only one tag in the outermost layer. This tag is called the root tag.
  The first line should have a document declaration for high-speed computers
  . For example: <?xml version="1.0" encoding="utf-8"?>
  When When tags are nested, there will be a hierarchical relationship. If a tag is not wrapped by any other tags, it is the root tag (outermost)


  Usage scenarios:
  1. Configuration file
  2. Regular data exchange such as getting a piece of news from the server

  The difference with json: the
    function is the same, it is a data format
    xml is born before
    json, the data of json is smaller than xml
    currently json is the mainstream

XML processing in python
   The module
ElmentTree used represents the element tree of the entire file

Elment represents a node
attribute
1.text the text between the start tag and the end
tag2.attrib all attribute dictionary
types3.tag tag name
method
get Get the value of an attribute1
. Parse XML to
find the tag
find in the subtag The name matches the first
findall Get all tags whose names match in
subtags iter(tagname) Find in the full text [all tags that match return an iterator
2. Generate XML Parse a file
with ElmentTree
parse()
getroot() Get the root The label
write() is written to the file
3. Modify the xml
set an attribute
remove a label
append a label

# Grammar format exercise: Require the mobile phone information of your deskmate to be described in xml
"""
import xml.etree.ElementTree as et

# Read the xml document into memory to get a node tree with all the data
# Each tag is called a node or element
# tree = et.parse("text.xml")
# # Get the root tag
# root = tree .getroot()
# # get all the countries find the first one
# print(root.find("country"))
# # find all
# print(root.findall("country"))
#
# # get year
# print(root.iter("country"))
# for i in root.iter("country"):
# print(i)
#
#
# # Traverse the entire xml
# for country in root:
# print(country.tag ,country.attrib,country.text)
# for t in country:
# print(t.tag, t.attrib, t.text)
#
#
#
# print(root.find("country").get("name" ))

# ============================================== Modify all the The year text of country is changed to plus 1
# Read into memory
tree = et.parse("text.xml")
for country in tree.findall("country"):
# yeartag = country.find("year")
# yeartag .text = str(int(yeartag.text) + 1) modify the tag text

# country.remove(country.find("year")) remove the label

# Add
subtag newtag = et.Element("newTag")
# Text
newtag.text = "123"
#Attribute
newtag.attrib["name"] = "DSB"
#Add
country.append(newtag)

# Write back to memory
tree.write("text.xml",encoding="utf-8",xml_declaration=False)

Generate XML

"""
Generate an xml document with code

"""
import xml.etree.ElementTree as et
# create root tag
root = et.Element("root")
# create node tree
t1 = et.ElementTree(root)

# add a peron tag
persson = et.Element("person")
persson.attrib["name"] = "yyh"
persson.attrib["sex"] = "man"
persson.attrib["age"] = "20 "
person.text = "This is a person tag"

root.append(persson)

# Write the file
t1.write("newXML.xml",encoding="utf-8",xml_declaration=True)

hashlib

What is hashlib
hash?
It is an algorithm
for compressing and mapping data of any length to a fixed-length character (extracting features)


Features of hash:
1. If the input data is different, the obtained hash value may be the same
2. The input value cannot be obtained through the hash value
3. If the algorithm is the same, regardless of the length of the input data, the obtained hash value has the same length

Because of the above characteristics, the hash algorithm is often used for encryption and file verification
. Enter the user name and password in the code to determine whether it is the same as in the database.
Think that when your data needs to be transmitted in the network, it may be attacked by
hackers through packet capture tools. You can intercept the data you send and receive,
so if your data involves privacy, it should be encrypted before sending.

There are many ways to encrypt, the
commonly used MD5 is a hash algorithm


A commonly used method to improve security is to add salt
, which is to make some changes to the data before encryption, such as reversing the order.

library

"""
import hashlib
md = hashlib.md5()
md.update("hello are you so awesome, you try to crack me? DSB".encode("utf-8"))
print(md.hexdigest())


# To crack MD5, you can try the principle of credential stuffing: There is a database that stores the correspondence between common plaintext and ciphertext,
# So I can take a ciphertext and take the database to find out if there is any existing plaintext if there is a credential stuffing function Can't be cracked by luck
# Assuming I have got a password from one of many accounts, I can test all your accounts with this password one by one. You may not have luck
pwd_dic = {"123456":"e10adc3949ba59abbe56e057f20f883e","hello":"5d41402abc4b2a76b9719d911017c592 "}

for i in pwd_dic:
if pwd_dic[i] == "5d41402abc4b2a76b9719d911017c592":
print(i)
''

# In the future, when we write some programs that require network transmission, if we want to encrypt the encryption algorithm, it is better to make the encryption algorithm more complicated
# The length of the password is 6 bits
# abcdef
# Add an abc in the front and a cba in the back, and then encrypt the
pwd after the completion. = "abcdef"

# pwd = "abc"+pwd+"cba"

md2 = hashlib.md5()
md2.update("121".encode("utf-8"))
md2.update(pwd.encode("utf-8"))
md2.update("akjasjkasa".encode("utf-8"))
print(md2.hexdigest())


# Encryption can actually do is to make the hacker's cracking cost greater than his profit


#
# sha = hashlib.sha512()
# sha.update("abcd".encode("utf-8"))
# print(len(sha.hexdigest()))
#

import hmac
h = hmac.new("121212".encode("utf-8"))
h.update("abcd".encode("utf-8"))
print(h.hexdigest())

 

configparser

What is configparser? Configuration file parsing module
What is a configuration file? What is
the use of the file suffix ini cfg used to provide some information required for program operation
? It is
convenient for users to modify such as timeout

The content format of the configuration file
includes only two elements
section section
option options
A file can have multiple sections and
a section can have multiple options

Core functions
1.sections get all
sections2.options get all
options3.get get a value and pass in section option

Note: case insensitive

"""

# Pretend to be a download function The maximum link speed can be controlled by the user. The user will not read the code, so provide a configuration file
import configparser
# Get the configuration file object
cfg = configparser.ConfigParser()
# Read a configuration file
cfg.read("download .ini")

print(cfg.sections())
print(cfg.options("section1"))

print(type(cfg.get("section1","maxspeed")))
print(type(cfg.getint("section1","maxspeed")))
print(cfg.getint("section2","minspeed"))


# Modify the maximum speed to 2048
cfg.set("section1","maxspeed","2048")

cfg.write(open("download.ini","w",encoding="utf-8"))