[11.6] Advanced --yield from generator

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 final_result = {}
 5 
 6 
 7 # 子生成器
 8 def sales_sum(pro_name):
 9     total = 0
10     nums = []
11     while True:
12         x = yield
13         print(pro_name+'销量: ', x)
14         if not x:
15             break
16         total += x
17         nums.append (X)
 18 is      return Total, the nums
 . 19  
20 is  
21 is  # delegate generator 
22 is  DEF Middle (Key):
 23 is      the while True:
 24          final_result [Key] = the yield  from sales_sum (Key)
 25          Print (Key + ' sales statistics completed! !. ' )
 26 is  
27  
28  # caller 
29  DEF main ():
 30      data_sets = {
 31 is          ' millet 9 ' : [1200, 1500, 3000 ],
 32          'Millet 8 ' : [28, 55, 98, 108 ],
 33          ' millet. 5 ' : [200 is, 560., 778, 70 ],
 34 is      }
 35      for Key, data_set in data_sets.items ():
 36          Print ( ' Start Key : ' , Key)
 37 [          m = middle (Key)
 38 is          # WPW middle coroutine 
39          m.send (None)
 40          for value in data_set:
 41 is              # passed to the set value of each coroutine 
42 is              m.send (value)
 43          # end
44         m.send(None)
45     print('final_result:', final_result)
46 
47 
48 if __name__ == '__main__':
49     main()
start key: 9 millet 
millet 9 sales: 1200 
millet 9 sales: 1500 
millet 9 sales: 3000 
millet 9 sales: None 
millet 9 complete sales statistics! ! . 
Start Key: 8 millet 
millet sales 8: 28 
Millet 8 sales: 55 
Millet 8 sales: 98 
Millet 8 sales: 108 
Millet 8 sales: None 
millet 8 sales statistics complete! ! . 
Start Key: 5 millet 
millet 5 sales: 200 
Millet 5 sales: 560 
Millet 5 sales: 778 
Millet 5 sales: 70 
Millet 5 sales: None 
millet 5 sales statistics complete! ! . 
Final_result: { 'millet 9': (5700, [1200, 1500, 3000]), 'Millet 8': (289, [28, 55, 98, 108]), 'millet 5': (1608, [200 , 560, 778, 70])}

 

yield from the main usage

. 1  # main caller, g1 (commissioned generator), gen syndrome generator 
2  # the yield from a bidirectional channel will be established between the caller and the child producer 
. 3  DEF G1 (Gen):
 . 4      the yield  from Gen
 . 5  
. 6  
. 7  DEF main ():
 . 8      G = G1 ()
 . 9      g.send (None)

yield from the process StopIteration exception

yield from other uses

. 1  # ! / Usr / bin / Python the env 
2  # - * - Coding: UTF-. 8 - * - 
. 3  # python3.3 yield from added new syntax 
. 4  
. 5  
. 6  from the itertools Import catena alberghiera
 . 7  
. 8  
. 9 my_list = [. 1, 2 ,. 3 ]
 10 my_dict = {
 . 11      ' ZY1 ' : ' http://projectsedu.com ' ,
 12 is      ' ZY2 ' : ' http://www.imooc.com ' 
13 is  }
 14  
15  
16  # yield from iterable(可迭代对象)
17 def my_chain(*args, **kwargs):
18     for my_iterable in args:
19         yield from my_iterable
20         # for value in my_iterable:
21         #     yield value
22 
23 
24 for value in my_chain(my_list, my_dict, range(5, 10)):
25     print(value)
1 
2 
3 
zy1 
zy2 
5 
6 
7 
8 
9

 

Guess you like

Origin www.cnblogs.com/zydeboke/p/11347945.html