8. Modules and packages

1. Module

A module is a collection of functions. In Python, a py file is a module

 

2. Use the module

1. import import module

 

Three things happen when you import a module for the first time:

1), create a namespace for the module

2) Execute the file corresponding to the module, and store the generated name in the namespace in 1

3), get a module name in the current execution file, the module name points to the namespace of 1

Subsequent imports will directly refer to the results of the first import, and will not repeat the execution of the file

 

To invoke a name in the module namespace, the syntax:

module name. The name in the module namespace

 

Aliasing the module name:

import module name as alias

 

2. from...import...import module

 

Three things happen:

1), create a namespace for the module

2) Execute the file corresponding to the module, and store the generated name in the namespace in 1

3), get the name in the module directly in the current namespace, you can use it directly without adding any prefix

 

3. The search path of the module

The search order of modules is: modules already loaded in memory---->built-in modules----->modules included in the sys.path path

The first path of sys.path is the folder where the current file is located

 

4. The value of __name__

1), when the file is executed, equal to '__main__'

2), when the file is imported, it is equal to the module name

 

5. Built-in modules

1), logging module:

logging.debug() debug, log level 10

logging.info() log level 20

logging.warning() warning message, log level 30

logging.error() error, log level 40

logging.critical() log level 50

 

logging.basicConfig()

 

logger: responsible for generating logs

filter: filter logs (not commonly used)

handler: control log printing to file orvsdr

formatter: controls the format of the log

 

Log level: There are two levels of checkpoints, only if they are passed, can they be released

 

2), json module, pickle module

Serialization: data structure in memory ----> convert to an intermediate format ----> save to file

That is, the process of changing objects (variables) from memory to storable or transferable is called serialization

Serialization can: 1. Persist state; 2. Cross-platform data interaction

 

json module

json.dump() serialization, i.e. encoding the data

json.dump() deserialization, that is, decoding the data

 

pickle module

Like the json module, the pickle module is of type byte

 

3), os module

os.path.dirname(path) returns the file path

os.path.join(path1,path2...) Combine the directory and file name into a path

os.path.exists(path) Returns True if the path exists, and returns False if the path is damaged

os.path.isdir(path) Determine whether the path is a directory

os.mkdir(path) generates a single-level directory

os.listdir lists all files and subdirectories in the specified directory, including hidden files, and prints them in a list

 

Software Development Directory Specification

Add the project's root directory to the format template in the environment variable:

import os,sys

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

sys.path.append(BASE_DIR)

 

4), time module

time.time() timestamp

time.strftime('') ​​formatted string  

time.localtime() object

 

5), datetime module

datetime.datetime.now() 

 

6), random module

7), sys module

8), shutil module

9) Shelve module

10), xml module

11), re module

regular expression

 

.*       Greedy matching, which means to match any length, any character

.*?      turns a greedy match into a non-greedy match

()        grouping

[]        matches a character within the specified range (this one character comes from the parentheses defined). When - needs to be matched as a normal symbol, it can only be placed on the leftmost or rightmost of []. ^ in [] means negation

Add ?: in brackets to take all the content that matches successfully, not just the content in brackets

 

12), hashlib module

Hash is an algorithm that receives incoming content and obtains a string of hash values ​​after operation.

Features:

1. As long as the incoming content is the same, the obtained hash value must be the same. File integrity check

2. The hash value cannot be reversed into content. You can make the password into a hash value, do not transmit it in clear text

3. As long as the hash value algorithm used remains unchanged, no matter how large the content of the verification is, the length of the obtained hash value is fixed

 

13), password salting module hmac

hmac.new()

 

14), subprocess module

15), configparser module

 

6. Package

a form of module

Three things happen when you import a package:

1), generate a package namespace

2), execute the __init__.py file under the package, and store the generated name in the package namespace

3), get the name of the package in the current execution file, the name points to the namespace of the package

 

When importing with a dot, the left side of the dot must be all packages, which is a specific syntax for importing packages.

Imports between modules within a package should use from...import...

from...import..., must be an explicit name after import, without any prefix

 

Relative import and absolute import of modules, absolute import is recommended

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324855978&siteId=291194637