Python: sequence and module; python: sequence Module

Source: Internet
Author: User

Python: sequence and module; python: sequence Module

I. serialization Module

What is serialization? the process of converting the original dictionary, list, and other content into a string is calledSerialization.

For example, if a piece of data we compute in python code needs to be used by another program, how can we provide it? The method we can think of now is to exist in the file, and then another python program will read it from the file. But we all know that there is no dictionary for a file, so we can only convert the data into a dictionary and put it into a file. You will certainly ask, it is very easy to convert the dictionary into a string, that is, str (dic) can be done. Why should we learn the serialization module? The serialization process is a process from dic to str (dic. Now you can use the str (dic) method to convert a dictionary named dic into a string. But how do you convert a string into a dictionary? Clever, you must have thought of eval (). If we pass a string-type dictionary str_dic to eval, we will get a returned dictionary type. Eval () functions are very powerful, but what does eval do? The official demo of eval explains: Evaluate string 'str' as a valid expression and return the calculation result. BUT! Powerful functions have a cost. Security is its biggest drawback. Imagine that if we read from a file not a data structure, but a destructive statement similar to "delete a file", the consequences would be disastrous. Using eval bears this risk. Therefore, we do not recommend using the eval Method for deserialization (converting str to the data structure in python)
Why serialization?

Serialization Purpose

1. Make the custom object persistent in a certain storage form; 2. Pass the object from one place to another. 3. Make the program more maintainable.

 

1. json Module

The Json module provides four functions: dumps, dump, loads, and load.

1) dumps and loads

# Json dumps serialization method loads deserialization method dic = {1: "a", 2: 'B'} print (type (dic), dic) import jsonstr_d = json. dumps (dic) # serialized print (type (str_d), str_d) # '{"kkk": "v"}' # Note: kkk uses "" (double quotation marks) dic_d = json. loads (str_d) # deserialization print (type (dic_d), dic_d) ''' <class 'dict '> {1: 'A', 2: 'B'} <class 'str'> {"1": "a", "2": "B"} <class 'dict '> {'1 ': 'A', '2': 'B '}'''
Dumps and loads

Dumps is a serialization method, and loads deserialization Method

2) dump and load

import json# json dump loaddic = {1:"a",2:'b'}f = open('fff','w',encoding='utf-8')json.dump(dic,f)f.close()f = open('fff')res = json.load(f)f.close()print(type(res),res)'''<class 'dict'> {'2': 'b', '1': 'a'}'''
Dump and load

3) differences between dumps, loads, dump, and load

If there is s, the data type is operated directly in the memory. If there is no s, the data type is read and written directly in the file.

Dump requires a file handle, and the load cannot be loaded multiple times.

4) ensure_ascii keyword Parameter

Import jsonf = open ('file', 'w') json. dump ({'nationality ': 'China'}, f) ret = json. dumps ({'nationality ': 'China'}) f. write (ret + '\ n') json. dump ({'nationality ': 'u.s.'}, f, ensure_ascii = False) ret = json. dumps ({'nationality ': 'u.s.'}, ensure_ascii = False) f. write (ret + '\ n') f. close ()
Ensure_ascii keyword Parameter

5) formatted json output

Import jsondata = {'username': ['Li Hua ', 'second-by'], 'sex': 'male', 'age': 16} json_dic2 = json. dumps (data, sort_keys = True, indent = 2, separators = (',', ':'), ensure_ascii = False) print (json_dic2)
Json formatted output

2, pickle

# All data types in python can be converted into strings # Only python can understand the content of pickle serialization # And part of deserialization depends on python code # Step-by-Step dump and step-by-step load # serialization and deserialization requires the same environment pickle module to provide four functions: dumps, dump, loads, load1) dumps and loads of pickle
Import pickledic = {'k1 ': 'v1', 'k2': 'v2', 'k3 ': 'v3'} str_dic = pickle. dumps (dic) print (str_dic) # a string of binary content dic2 = pickle. loads (str_dic) print (dic2) # dictionary

2) step-by-step dump and load

Dump and load must be enabled using wb and rb.

import timestruct_time1  = time.localtime(1000000000)struct_time2  = time.localtime(2000000000)f = open('pickle_file','wb')pickle.dump(struct_time1,f)pickle.dump(struct_time2,f)f.close()f = open('pickle_file','rb')struct_time1 = pickle.load(f)struct_time2 = pickle.load(f)print(struct_time1.tm_year)print(struct_time2.tm_year)f.close()
Step-by-Step dump and load

3. shelve

# Serialization handle # use a handle for direct operations. It is very convenient that shelve is also a serialization tool provided to us by python. It is easier to use than pickle. Some shelve only provides us with an open method, the open method obtains a file handle, and the operation is similar to the dictionary. It is accessed with a key, which is similar to a dictionary.
Import shelve = shelve. open ('shelve _ file') f ['key'] = {'int': 10, 'float': 9.5, 'string ': 'sample data'} # store data directly by operating on the file handle. close ()
Import shelvef1 = shelve. open ('shelve _ file') existing = f1 ['key'] # You only need to use the key to retrieve data. However, if the key does not exist, an error f1.close () is returned () print (existing)

Ii. Modules

1. What is a module?

A module is a file that contains python definitions and declarations. The file name is the extension of the module name and. py.

2. The import module is divided into four general categories:

1. code written in python (. py file)

2 C or C ++ extensions that have been compiled as shared libraries or DLL

3. Pack a group of modules

4. Use C to write and link to the python interpreter's built-in modules

3, import

1) Example file: Custom module my_module.py, file name my_module.py, Module name my_module

#my_module.pyprint('from the my_module.py')money=1000def read1():    print('my_module->read1->money',money)def read2():    print('my_module->read2 calling read1')    read1()def change():    global money    money=0
View Code

The module can contain executable statements and function definitions. The purpose of these statements is to initialize the module, they are executed only when the module name encounters an import Statement for the first time (the import statement can be used anywhere in the program, and it is imported many times for the same module, to prevent repeated import, the python optimization method is: after the first import, the module name is loaded into the memory, the subsequent import statement only adds a reference to the module object that has been loaded into large memory, and does not re-execute the statements in the module)

# Demo. pyimport my_module # execute the code in my_module.py only during the first import. the explicit effect here is to print 'from the my_module.py 'only once. Of course, other top-level code is also executed, however, no effect is displayed. import my_moduleimport my_module ''' execution result: from the my_module.py ''' # the call is performed multiple times, but only one result is printed.

From sys. in modules, find the currently loaded module, sys. modules is a dictionary that contains the ing between the module name and the module object. This dictionary determines whether to re-import the module.

2) each module is an independent namespace, which defines the functions in this module and regards the namespace of this module as a global namespace. In this way, when writing our own modules, there is no need to worry that the global variables defined in our module will conflict with the user's global variables when being imported.

# Test 1: There is no conflict between money and my_module.money # demo. py # In my_module: from the my_module.pyimport my_modulemoney = 10 print (my_module.money) ''' execution result: from the my_module.py '''

3) Test 2: read1 does not conflict with my_module.read1

# Test 2: no conflict between read1 and my_module.read1 # demo. py # my_module: # print ('from the my_module.py ') # def read1 (): # print ('money 1000') import my_moduledef read1 (): print ('=') my_module.read1 () ''' execution result: from the my_module.pymy_module-> read1-> money 1000 '''

4) Test 3: The global variable money for the my_module.change () operation is still in my_module.

# Test 3: The global variable money for executing the my_module.change () operation is still in my_module # demo. pyimport my_modulemoney = 1my_module.change () print (money) ''' execution result: from the my_module.py '''

5)

Conclusion: three things will be done when the module my_module is imported for the first time:

1. Create a New namespace for the source file (my_module Module). If the functions and methods defined in my_module use global, access the namespace.

2. Execute the Code contained in the module in the newly created namespace. For details, see import my_module

What is the execution of the import module? In fact, the function definition is also an executed statement. The execution of the module-Level Function Definition puts the function name into the global namespace table of the module, which can be viewed using globals ().

3. Create a namespace named my_module to reference it.

1. This name is no different from the variable name. It is 'first class' and uses my_module. you can access the name defined in my_module.py. name and test. the name in py comes from two completely different places.

6) alias for the module name, equivalent to m1 = 1; m2 = m1

import my_module as smprint(sm.money)

Example 1:

There are two SQL modules: mysql and oracle. Different SQL functions are selected based on user input.

# Mysql. pydef sqlparse (): print ('from mysql sqlparse') # oracle. pydef sqlparse (): print ('from oracle sqlparse') # test. pydb_type = input ('>>:') if db_type = 'mysql': import mysql as dbelif db_type = 'oracle ': import oracle as dbdb. sqlparse () Copy code

4,From... import...

From my_module import read1, read2
In this way, you can directly use read1 and read2 at the current location. During execution, the global namespace of my_module.py file is still used.
from demo import money,readprint(money)read()money = 200read()
# Test 1: The imported function read1 is still returned to my_module.py to find the global variable money # demo. pyfrom my_module import read1money = 1000read1 () ''' execution result: from the my_module.pyspam-> read1-> money 1000 ''' # Test 2: import function read2, call read1 () during execution and return to my_module.py to find read1 () # demo. pyfrom my_module import read2def read1 (): print ('=') read2 () ''' execution result: from the my_module.pymy_module-> read2 calling read1my_module-> read1-> money 1000 '''
In this way, you can directly use read1 and read2 at the current location. During execution, the global namespace of my_module.py file is still used.

If there are duplicate read1 or read2 names, it will overwrite the data.

# Test 3: The imported function read1 is overwritten by read1 defined in the current position # demo. pyfrom my_module import read1def read1 (): print ('=') read1 () ''' execution result: from the my_module.py ========== '''

It should be particularly emphasized that variable assignment in python is not a storage operation, but a binding relationship, as shown below:

From my_module import money, read1money = 100 # bind the name of the current location to 100 print (money) # print the current name read1 () # Read the name money in my_module.py, still 1000 ''' from the my_module.pymy_module-> read1-> money 1000 '''

1)

Also supports

from my_module import read1 as read

2) multi-line input is also supported.

from my_module import (read1,                  read2,                 money)

5. Execute the module as a script

We can view the module name through the global variable _ name _ of the module:
Run as a script:
_ Name _ equals to '_ main __'

Import as a module:
_ Name __= Module name

Role: used to control the execution of different logic for. py files in different application scenarios
If _ name _ = '_ main __':

def fib(n):       a, b = 0, 1    while b < n:        print(b, end=' ')        a, b = b, a+b    print()if __name__ == "__main__":    print(__name__)    num = input('num :')    fib(int(num))
6 Module search path

The python interpreter automatically loads some modules at startup, which can be viewed using sys. modules.

When a module is imported for the first time (for example, my_module), the system first checks whether the module has been loaded into the memory (the memory corresponding to the namespace of the current execution file). If yes, It is referenced directly.

If not, the interpreter searches for built-in modules with the same name. If not, the interpreter searches for the my_module.py file from the directory list given by sys. path.

Therefore, the search sequence of the summary module is: modules loaded in the memory> built-in modules> modules included in the sys. path.

Note that the custom Module name should not be the same as the built-in Module name. Although it is said every time, there will still be people making mistakes constantly.

 

Module summary:

1. Both import and from import support as rename, and both support multi-name import.

2. After a module is imported, it will not be imported again. Because sys. mouldes records all imported modules

3. sys. path records all the paths found during the import module.

4. The module is a py file.

5 ,__ all _ must be used *

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.