Chapter 1.12 generator module and

A. Builder

1. Generator

  • Builder is also an iterator;
  • Generating container as it is not stored data, but data generated Algorithm

2. Create generator

  • Call the function with the yield keyword, you can get a generator

Note: As long as there is a function in yield, no matter will not encounter (execution), then call this function, the function body will not be executed, and get a generator

3. The generator produces data

  • A number of data and what data generator can produce, look after executing the function generator is associated encountered several times yield in executed,
  • After several generates can yield several transactions; yield each encounter, the following data is the corresponding element

4. A data generator generates Law

  • Obtaining a first element when the statement is executed from a first function, the first encounters a stop yield, and the yield value of the current element as the latter acquired;
  • When acquiring the next element, then the end of a local execution, the next encountered yield is stopped, and the yield value of the current element as the latter acquired;
  • And so on ...
  • When the yield will not function body, indicating that the data has been generated is taken over, the error will be run again
# 例:
def func3():
    print('======')
    yield 1
    print('++++++')
    yield 10
    print('******')
    yield 100

gen2 = func3()
print(next(gen2))
print(next(gen2))
print(next(gen2))
print(next(gen2))   # 元素取完后报错


# 练习:写一个生成器能产生一个班所有学生的学号
def students_num(n):
    length = len(str(n))
    for num in range(1,n+1):
        yield str(num).zfill(length)

nums = students_num(50)
for num1 in nums:
    print(num1)

The formula

  • 1) the formula is the builder; like functions and anonymous functions, just writing simple
"""
1)语法①:
生成器 = (表达式 for 变量 in 序列)
 展开:
 def 函数名():
    for 变量 in 序列:
        yield 表达式
生成器 = 函数名()

  
 语法②:
 生成器 = (表达式 for 变量 in 序列 if 条件语句)
  展开:
 def 函数名():
    for 变量 in 序列:
        if 条件语句:
            yield 表达式
生成器 = 函数名()


语法③:
 生成器 = (表达式 for 变量1 in 序列1 for 变量2 in 序列2)
  展开:
 def 函数名():
    for 变量1 in 序列1:
        for 变量2 in 序列2:
            yield 表达式
生成器 = 函数名()

"""

# 例:
gen1 = (num for num in range(1, 11))

gen4 = (x for x in range(10) if x % 2)
  • 2) a list of formula
  • The syntax above all parentheses programming brackets, the result will become a list



II. Module

1. Module (Python, one file is a module py)

CROSS-REFERENCE (imported module) between the modules 2

To use the contents of another module, the corresponding module to be imported in the current module

1) :( module import method to import the module code is generally placed on the very top of the file, put the file system on top, in the middle of the third party, wrote the final release)

①: import module name
- after introducing all global variables can be introduced into the module
- in 'module name variable name.' Manner using the corresponding module
②: from import module name variable name 1, variable name 2, ...
- Import variables can be used after the specified import
- directly using
③: from import module name *
- after introduction of the module can be introduced into all global variables
- directly using

2) rename module
  • import module name as a new module name ------> sometimes introduced module or function names will be the same name within the parameters of this module, then the module is used by introducing a new name module
  • Can also rename module variable in this way

    3) prevent the import
  • When the execution code into the module, the system performs all code module is introduced, in order to prevent unnecessary consumption of all code execution result, to prevent the need to use imported
  • Blocking Import: The code does not need to be introduced within the module called into *** if __name__ == '__main __' *** statement can; Code in the if statement can not be referenced by other modules, but in the present within the module can run.
  • Stop principle: module when it is created, the system will be added as a module __name__ attribute is used to save the name of the module; __name__ default is the file name of the file, when you run the module directly, __name__ will become __main__, this when the if statement is true, execute the code behind



III. Packet (packet management module is used)

Package containing __init __. Py ** file file folder **

Modules within the package introduction

①:. Import module package (available as renamed)
package module code name to call..
②: import module from the package 1, module 2, ...
module code name to call.
③: import module package code from the call. name
④: from module import package variables.

2.init file
  • After introducing the package, the package will only perform its original state __init__ file, you need to import other modules within the package in the __init__ file.



Four. Hashlib module

1.hashlib

  • hashlib is provided by python library to encrypt a hash algorithm
  • Hashing algorithm known as discrete, mainly contains MD5, sha two types of algorithms

    2. hashing algorithm encryption features

    1) after the encrypted ciphertext (Abstract) irreversible
    2) The same data through the same encryption algorithm ciphertext is the same as
    3) The data of different lengths by the same encryption algorithm ciphertext It is the same length

    3. ciphertext generating process (digest) of (how to encrypt)

    1) Create a hashlib objects: hashlib algorithm name ()
    2) the need to add encrypted data: hash object .update (data (must be binary data))
    . 3) to produce ciphertext (Summary): hash object. hexdigest ()
hash = hashlib.md5()
pw = '123456'
hash.update(pw.encode())
result = hash.hexdigest()
print(result)



>>>>>>> knowledge expansion

bytes of binary data type is
1. String transfer binary
①: bytes (string, encoding = 'UTF-. 8')
②: string .encode ()
2. Binary string
①: str (binary, encoding = ' . 8-UTF ')
②: binary .decode (encoding =' utf-8 ')

Guess you like

Origin www.cnblogs.com/anjhon/p/11892363.html