yield from

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: yield_from_example
Description :
Author : Administrator
date: 2019/8/11 0011
-------------------------------------------------
"""
final_results = {}


def sales_sum(pro_name):
"""
子生成器
:param pro_name:
:return:
"""
total = 0
nums = []
while True:
x = yield
print(pro_name + "销量", x)
if not x:
break
total += x
nums.append(x)
return (total,nums)


def middle(key):
"""
委托生成器
:param key:
:return:
"""
while True:
final_results[key] = yield from sales_sum(key)
print(key + "销量统计完成 !.")


def main():
data_sets = {
"bobby牌面膜": [1200, 1500, 3000],
"bobby牌手机": [1200, 1500, 4000],
"bobby牌大一": [1800, 1500, 3000],
}
for key, data_set in data_sets.items():
print("start key:", key)
m = middle(key)
m.send(None)
for value in data_set:
m.send(value)
m.send(None)

print("final result is:", final_results)


if __name__ == "__main__":
main()
posted @ 2019-08-11 15:00  tny_leyon  阅读(126)  评论(0编辑  收藏  举报