day13 - modules and packages

Posted by Zoran_Dimov on Wed, 05 Jan 2022 06:58:51 +0100

Iterators, generators, modules, and packages

1. Iterator

1. What is an iter ator

Iterator is a kind of container data type, which can save multiple data at the same time; Can be traversed; It can also be converted to lists and tuples. When printing an iterator, you cannot print the elements inside; Iterator does not support len operation; If you need an element in the iterator, you must take the element out of the iterator, and once the element is taken out, the element does not exist in the iterator.

2. How to create iterators

  • Convert other sequences to iterators through iter
  • Create a build object (the generator can be thought of as a special iterator)

3. Get the elements in the iterator

'''
1)Get a single element: next(iterator )
2)for Loop traversal
'''
iter1 = iter('abc')
print(itre1)		# <str_iterator object at 0x000002801F805580>
print(next(iter1))

for x in iter1:
    print(x)

2. Generator

1. What is a generator

A generator is a container capable of generating multiple data. The generator is the same as the iterator when getting data.

2. How to create a generator

Call a function with the yield keyword to get a generator object. If there is yield in the called function, the function will not execute the function body or get the return value, but get a generator.

def func1():
    print('----------')
    yield
    print('==============')
    
    
result = func1()
print(result)		# <generator object func1 at 0x00000267B4A0C820>

3. Control the ability of the generator to generate data

When executing the function corresponding to the generator, you will encounter a yield several times. How many data can the generator generate? Each time you encounter a yield, the value behind the yield is the corresponding data that can be generated.

def func2():
    yield 100
    for i in range(4):
        yield i
    yield


gen2 = func2()
for x in gen2:
    print(f'====:{x}')  # A total of 6 yield s are encountered, so they will be printed 6 times

4. Generator principle

When the data is obtained through the generator object, the program will execute the corresponding function of the generator. Each time it encounters a yield, it will stop, take the data behind the yield as the data obtained this time, record the end position, and execute from the last end position when obtaining the data next time.

Application: reading file contents

3. Module

'''
1.What is a module
python One in py A file is a module.

2.How to use the content of another module in one module
 Note: 1) if you want a module to be used by another module, the module name of this module must be an identifier and not a keyword
	 2)One module can use all global variables in another module, but it must be imported before use.
	 
3.Import module
1)import Module name	-	After importing the specified module, you can'Module name.'Use all global variables in the module in the same way
2)from Module name import Global variable 1, global variable 2,...
	Import the specified module. After importing, you can directly use the specified global variables
3)from Module name import * -  Import all global variables
4)import Module name as New module name 	-	Rename the module and use the new module name when using the module after renaming
5)from Module name import Variable name as New variable name
'''
# Mode 1:
import test1
print(test1.a)
# Mode 2:
from test1 import a, x
print(a, x)
# Mode III
from test1 import *
print(a, x)
print(func1)
# Mode 4
from test1 import a as a1, x
print(a1, x)
# Mode 5
import test1 as test
print(test.a)

'''
4.Principle of import module
 When passed import perhaps from-import When importing a module, the system will automatically execute all the code in the module.

5.Avoid unnecessary code execution
if __name__ == '__main__':
	The code in this statement will not be executed when imported by other modules, but will be executed when directly running the current module
 At the beginning, all py Document__name__Are equal to the default value (file name). When running in a file,__name__be equal to'__main__',So when other files call other files if __name__ == '__main__':,The result is False,So the following code will not be executed.
	However, it can be run in this document.
'''

4. Package

'''
1.What is a bag
 A package contains__init__.py File folder

2.Imported content in package (used)
1)import Package name 	- 	Import package directly
2)import package.modular		-	Can pass'package.modular.'To use all global variables in the specified module
3)from package import Module 1, module 2,... -	Can pass'modular.variable'To use all global variables in the specified module
4)from package.Module name import Variable 1, variable 2,... -	You can directly use the specified variables in the specified module

3.How to import packages
 When importing modules in the package through the package, the program will execute the modules in the package first__init__.py File, and then execute the code in the corresponding module
'''
# 1. Import package directly
import files
# 2. Import module through package
import files.excel as f_excel
print(f_excel.read_excel)
# 3. Import module through package
from files import excel, plist
print(excel.read_excel)
# 4. Import the contents of the module through the package
from files.ecxel import read_excel
print(read_excel)

Application of package: in__ init__ Create shortcut keys in

  • In init_ Py, import variables in advance, which can be imported directly in the module
# __ init__.py file:
from files.excel import read_excel
# In the module:
from files import read_excel
print(read_excel)

  • In init_ Py file to define common functions in subordinate modules
# __ init__.py file:
def open_file():
    print('Open file')
# In the module:
import files
files.open_file()

Topics: Python