The path to learning Python (iii)
The following Python 3.6 is used
First, the assembly part
A collection is an unordered, non-repeating collection of data used primarily for deduplication, as well as for relational testing: intersection, Difference set, and set, etc.
1.1 Relationship Operations 1.1.1 list go to heavy
You can give the list a weight, for example:
1 # List 2 # convert to a set, come and go heavy 3 Print (Set_demo)
1.1.2 Intersection Intersection () method
You can get the intersection portion of two sets, for example:
1 set_demo_1 = set ([2,3,4,5,4])2 set_demo_2 = set ([2,22,33,44,11])3 Print(set_demo_1.intersection (set_demo_2))
1.1.3 Collection Union () method
You can get a set of two sets, for example:
1 set_demo_1 = set ([2,3,4,5,4])2 set_demo_2 = set ([2,22,33,44,11])3 Print (Set_demo_1.union (Set_demo_2))
1.1.4 difference Set Difference () method
Removes the part of the calling object that is common to the parameters, that is, the difference set, for example:
1 set_demo_1 = set ([1,2,3,4,5])2 set_demo_2 = set ([])3Print (Set_demo_1.issuperset (set_demo_2))#Set_demo_1 is the parent set of Set_demo_2 returns True4Print (Set_demo_2.issubset (set_demo_1))#Set_demo_2 is a subset of Set_demo_1 returns True
1.1.5 subset, Parent set
You can use the Issubset,issuperset method, for example:
1 set_demo_1 = set ([1,2,3,4,5])2 set_demo_2 = set ([])3Print (Set_demo_1.issuperset (set_demo_2))#Set_demo_1 is the parent set of Set_demo_2 returns True4Print (Set_demo_2.issubset (set_demo_1))#Set_demo_2 is a subset of Set_demo_1 returns True
1.1.6 Symmetric difference Set
A collection of all elements that are not part of A∩B in collection A and collection B, for example:
1 set_demo_1 = set ([2,3,4,5,4])2 set_demo_2 = set ([2,22,33,44,11])3 Print# returns the part where the intersection of the two is removed
1.1.7 Non-intersecting
Two sets returns true if there is no intersection, for example:
1 set_demo_1 = set ([1,2,3,4,5])2 set_demo_2 = set ([11,22,33])3 Print (Set_demo_1.isdisjoint (Set_demo_2)) # no intersection returns True
1.1.8 using operators instead of method calls
1 # Fetch and set 2 # Take intersection 3 # take the difference set and remove the part of C in B 4 # symmetry Difference Set
1.2 Basic operations
1Set_demo = Set ([1,2,3,4,5])2Set_demo.add (66)#add an action, add an item3Set_demo.update ([11,22])#add operations, add multiple items4 5Set_demo.remove (66)#delete operation, delete the specified item, if not the error6Len (Set_demo)#Set Length7 8Set_demo.pop ()#delete operation, randomly delete one, and return the value9Set_demo.discard (666)#delete operation, delete the specified one, if not, do nothingTen Print(Set_demo)
Ii. part of the document
Basic process: Open File---File Close file
2.1 File Open mode
Mode |
meaning |
R |
Open a read-only file that must exist. |
r+ |
Open a read-write file that must be present. |
W |
Open write-only file, if the file exists, the file length is clear to 0, that is, the contents of the file will disappear. If the file does not exist, the file is created. |
w+ |
Open a read-write file, if the file exists, the file length is zero, that is, the contents of the file disappears. If the file does not exist, the file is created. |
A |
Open the write-only file in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. |
A + |
Opens a file that can be read and written in an additional way. If the file does not exist, the file will be created, and if the file exists, the data written will be added to the end of the file, that is, the original content of the file will be retained. |
Note: The above morphological strings can be added with a B character, such as RB, w+b, or ab+ combinations, and a B character is used to tell the library that the file opened is a binary file, not a plain text file. However, in a POSIX system, including Linux ignores the character.
2.2 File read and write operations
Read the file operation, the read (size) method, the parameter can enter the read length, does not fill the default read all
1 f = open ("file_demo", mode="R", encoding=' utf-8'# Create file handle reference 2print(F.read ()) 3 f.close ()
Write file operations
1 f = open ("file_demo", mode="w", encoding=' utf-8'# Create file handle reference 2 f.write (" test case \ n " )3 f.close ()
Read one line, multiple lines
1f = open ("File_demo", mode="R", encoding='Utf-8')#Create a file handle reference2 Print(F.readline ())#single-line read3 f.close ()4 5f = open ("File_demo", mode="R", encoding='Utf-8')#Create a file handle reference6 forLineinchF.readlines ():#read all the files of the entire line, stored in the file, if the file is large, the speed is very slow, generally not recommended ReadLines7 Print(Line.strip ())8F.close ()
2.3 Cursor (position action) of the file
The file location operation has the following methods: Tell,seek tell () can return to the current position seek (offset[, whence]), the offset parameter is the beginning of the offsets, representing a few bytes offset, whence default is 0, Indicates from which position to start the offset: 0 represents starting at the beginning of the file, 1 representing the beginning of the current position, and 2 representing the end of the file.
Flush () operation of 2.4 files
The flush () method refreshes the internal buffer of the file, directly writes the data of the internal buffer to the file immediately, rather than passively waiting for the output buffer to be written. You can prevent power outages and other conditions so that data is not written to the file.
1 f = open ("file_demo", mode="a", encoding=' utf-8'# creates a file handle reference 2 f.write (" This is the Append data " )3 f.flush ()# Write buffer data to file 4 f.close ()
2.5 truncate () method for files
Truncate ([size]): Used to truncate from the beginning of the first line of a file, truncate the file to size characters, no size indicates truncation from the current position, and after truncation, all subsequent characters are deleted.
2.6 Other operations
Fileno () Returns a shaped File descriptor encoding () returns the encoding of the Open file Isatty () returns True if the file is connected to an end device, otherwise returns FALSE if the file is readable, or returns true, otherwise False writeable () returns True if the file is writable, otherwise false
2.7 with Operation
With can automatically help close files and release resources
1 with open ("file_demo", mode="R", encoding=" utf-8" ) as F:2for in F:3 Print#strip to remove line breaks
Third, JSON serialization and deserialization
Serialization can be easily understood as storing objects in a file, and deserialization can simply be understood as the ability to read the file data with objects to store the serialization process
1 ImportJson#Import a JSON package2 3 #Serialization Process4 #Convert dictionary type to JSON object5info = {"name":"Codehu",6 " Age": 25}7With open ("Json.text","W") as F:8 F.write (Json.dumps (info))9 #json.dump (info,f) #或者使用dump
Anti-sequence process
1 import JSON 2 # deserialization 3 with open ( " json.text ", " r " ) as F: 4 Print (Json.loads (F.read ()) [ " age " ]) 5 # print (Json.load (f) ["age"]) #可以采用load方法读取
IV. Engineering Catalogue Specification
Excerpted from https://www.cnblogs.com/JayeHe/p/6696848.html
4.1 General directory Structure
Let's say your project is named Foo, the most convenient and quick directory structure:
foo/|--bin/| | --foo| | --foo/| | --tests/| | | --__init__.py| | | --test_main.py| | | | --__init__.py| | --main.py| | --docs/| | --conf.py| | --abc.rst| | --setup.py| --requirements.txt| --README
Briefly explain:
- Bin/: To store some executable files of the project, of course you can name script/and so on.
- foo/: Stores all source code for the project. (1) All modules and packages in the source code should be placed in this directory. Do not place the top level directory. (2) Its subdirectory tests/storage unit test code; (3) The entrance of the program is preferably named main.py.
- Docs/: Store some documents.
- setup.py: Scripts to install, deploy, and package.
- Requirements.txt: A list of external Python packages that store software dependencies.
- README: project documentation.
4.2 Readme File
This is a file that each project should have, with the aim of briefly describing the project's information so that the reader can quickly understand the project.
It needs to illustrate several things:
- Software positioning, the basic functions of the software.
- How to run your code: Installation environment, startup commands, and so on.
- Brief instructions for use.
- Code directory structure Description, more detailed can explain the basic principles of the software.
- FAQ's description.
4.3 setup.py
In general, use setup.py to manage the packaging, installation, and deployment of your code. The industry standard notation is to use Python's popular packaging tools Setuptools to manage these things. This approach is commonly used in open source projects. But the core idea here is not to use standardized tools to solve these problems, but to say that a project must have an installation deployment tool that can quickly and easily install the environment on a new machine, deploy the code, and run the program.
4.4 Requirements.txt
The purpose of this file is to facilitate the developer to maintain the software's package dependencies. Add the new packages in the development process to this list to avoid missing packages when the setup.py installation is dependent. Make it easy for readers to identify which Python packages the project uses. The format of this file is that each line contains a description of the package dependency, usually flask>=0.10, which requires that the format be recognized by the PIP, so that the PIP Install-r can be easily Requirements.txt to put all Python packages on the load.
The path to learning Python (iii)