Python 3.6
1. Set part
A set is an unordered and non-repeated data set. It is mainly used for deduplication and link testing: intersection, difference set, and Union set.
1.1 link operation 1.1.1 list deduplication
The list can be de-duplicated, for example:
1 set_demo = [1, 2, 3, 4, 5, 3, 2, 1] # List 2 set_demo = set (set_demo) # convert it to a set to repeat 3 print (set_demo)
1.1.2 intersection () method
You can obtain the intersection 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 union () method
You can obtain the union 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
Remove the common part of the call object and the parameter, that is, the difference set. For example:
1 set_demo_1 = set ([1, 2, 3, 4, 5]) 2 set_demo_2 = set ([1, 2, 3]) 3 print (set_demo_1.issuperset (set_demo_2 )) # set_demo_1 is the parent set of set_demo_2 and returns true4 print (set_demo_2.issubset (set_demo_1) # set_demo_2 is a subset of set_demo_1 and returns true
1.1.5 subset, parent set
You can use the issubset and issuperset methods, for example:
1 set_demo_1 = set ([1, 2, 3, 4, 5]) 2 set_demo_2 = set ([1, 2, 3]) 3 print (set_demo_1.issuperset (set_demo_2 )) # set_demo_1 is the parent set of set_demo_2 and returns true4 print (set_demo_2.issubset (set_demo_1) # set_demo_2 is a subset of set_demo_1 and returns true
1.1.6 symmetric difference set
A set of all elements in A and B that do not belong to A member B. For example:
1 set_demo_1 = set ([2, 3, 4, 4]) 2 set_demo_2 = set ([2, 22, 33,44, 11]) 3 print (set_demo_1.1_ric_difference (set_demo_2 )) # Remove the intersection of the two
1.1.7 non-Intersecting
Returns true if there is no intersection between the two sets, for example:
1 set_demo_1 = set ([, 5]) 2 set_demo_2 = set ([, 33]) 3 print (set_demo_1.isdisjoint (set_demo_2) # true is returned if no intersection exists.
1.1.8 use operators instead of method calls
1 A = B | C # Get Union set 2 A = B & C # Get intersection 3 A = B-C # Get difference set, remove Part 4 A = B ^ C # From B and obtain the symmetric difference set.
1.2 Basic operations
1 set_demo = set ([, 5]) 2 set_demo.add (66) # add operation, add a 3 set_demo.update ([]) # add operation, add more than 4 5 set_demo.remove (66) # delete operation. delete a specified item. If no one exists, the error 6 len (set_demo) # set length 7 8 set_demo.pop () # delete operation, delete one randomly and return the value 9 set_demo.discard (666) # delete operation. Delete the specified one. If there is no such value, No 10 print (set_demo) will be performed)
Ii. File Section
Basic Process: open a file> operate a File> close a file
2.1 file opening Mode
| Mode |
Description |
| R |
Open a read-only file, which must exist. |
| R + |
Open a readable file, which must exist. |
| W |
Open the write-only file. If the file exists, the file length is 0, indicating that the file content will disappear. If the file does not exist, the file is created. |
| W + |
Open a readable/writable file. If the file exists, the file length is cleared to zero, meaning that the file content disappears. If the file does not exist, the file is created. |
| A |
Open and write-only files as an attachment. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. |
| A + |
Open readable and writable files as an attachment. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. |
Note: The preceding morphological strings can be added with a B character, such as a combination of rb, w + B, and AB +. The B character is added to indicate that the files opened by the function library are binary files, rather than text files. However, this character is ignored in the POSIX System in Linux.
2.2 read/write operations on files
Read File Operation, read (size) method. You can enter the read length for the parameter. If this parameter is left blank, all data is read by default.
1 f = open ("file_demo", mode = "r", encoding = 'utf-8') # create a file handle Reference 2 print (f. read () 3 f. close ()
File write operations
1 f = open ("file_demo", mode = "w", encoding = 'utf-8') # create a file handle Reference 2 f. write ("Test Case \ n") 3 f. close ()
Read one or more rows
1 f = open ("file_demo", mode = "r", encoding = 'utf-8') # create a file handle Reference 2 print (f. readline () # Read 3 f from a single row. close () 4 5 f = open ("file_demo", mode = "r", encoding = 'utf-8') # create a file handle reference 6 for line in f. readlines (): # Read all rows of all files and store them in files. If the file is large, the speed will be very slow. Generally, readlines7 print (line. strip () 8 f. close ()2.3 file cursor (position Operation)
The file location operation can be performed in the following ways: tell, seek tell () can return the current position seek (offset [, whence]). The offset parameter is the start offset, indicating the offset of several bytes, the default value of whence is 0, indicating the starting position of the Offset. 0 indicates that the offset starts from the beginning of the file, 1 indicates that the offset starts from the current position, and 2 indicates that the offset starts from the end of the file.
2.4 file flush () Operations
The flush () method refreshes the internal buffer of the file, directly writes the data in the internal buffer to the file immediately, instead of passively waiting for the output buffer to be written. It can prevent data from being written into files due to power outages.
1 f = open ("file_demo", mode = "a", encoding = 'utf-8') # create a file handle Reference 2 f. write ("this is the append data") 3 f. flush () # Write Data in the cache area to the file. 4 f. close ()2.5 truncate () method of the file
Truncate ([size]): Used to truncate from the beginning of the first line of the file. The truncated file is a size character. If no size is specified, it is truncated from the current position, all subsequent characters are deleted.
2.6 other operations
Fileno () returns an integer file descriptor encoding () returns the encoding method of the opened file isatty () returns True if the file is connected to a terminal device, otherwise False readable () if the file is readable, true is returned. Otherwise, False is returned. writeable () is returned. If the file is writable, True is returned. Otherwise, False is returned.
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: 2 for line in f: 3 print (line. strip () # strip is used to remove line breaksIii. JSON serialization and deserialization
Serialization can be simply understood as storing objects in files. deserialization can be simply understood as reading file data and storing the serialization process with objects.
1 import json # import json package 2 3 # serialization process 4 # convert dictionary type to JSON object 5 info = {"name": "CodeHu", 6 "age ": 25} 7 with open ("json. text "," w ") as f: 8 f. write (json. dumps (info) 9 # json. dump (info, f) # Or use dump
Reverse Sequence Process
1 import json2 # deserialization 3 with open ("json. text "," r ") as f: 4 print (json. loads (f. read () ["age"]) 5 # print (json. load (f) ["age"]) # You can use the load method to readIv. Project catalog specifications
From https://www.cnblogs.com/JayeHe/p/6696848.html
4.1 General directory structure
Suppose your project name is foo, which is the most convenient and convenient 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
Brief explanation:
- Bin/: stores some executable files of the project. Of course, you can name it script/or something like that.
- Foo/: stores all source code of the project. (1) All modules and packages in the source code should be stored in this directory. Do not place it in the top-level directory. (2) Its Sub-directory tests/Stores Unit Test code; (3) it is best to name the program entry main. py.
- Docs/: stores some documents.
- Setup. py: installation, deployment, and packaging scripts.
- Requirements.txt: External Python package list for storing software dependencies.
- README: Project description file.
4.2 README file
This is a file that every project should have. The purpose is to briefly describe the project information, so that readers can quickly understand the project.
It needs to explain the following:
- Software positioning: basic functions of the software.
- How to run the code: installation environment, startup command, etc.
- Brief Instructions for use.
- Code directory structure description. For more details, you can describe the basic principles of the software.
- FAQs.
4.3 setup. py
Generally, setup. py is used to manage code packaging, installation, and deployment problems. The industry standard is to use the popular Python packaging tool setuptools to manage these things. This method is widely used in open-source projects. However, the core idea here is not to use standardized tools to solve these problems, but to say that a project must have an installation and deployment tool, it can quickly and conveniently install the environment, deploy the code, and run the program on a new machine.
4.4 requirements.txt
The purpose of this file is to facilitate the developer to maintain the package dependency of the software. Add the packages added during the development process to this list to avoid missing software packages during setup. py dependency installation. This helps you determine which Python packages are used in the project. The format of this file is the description that each line contains a package dependency, which is usually flask> = 0.10. This format must be recognized by pip, in this way, you can simply install all Python package dependencies Through pip install-r requirements.txt.