- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 python,我正在尝试找到一种优雅地将多个生成器链接在一起的方法。问题的一个例子是有一个根生成器,它提供某种数据,每个值都像级联一样传递给它的“ child ”,这反过来可能会修改他们接收到的对象。我可以选择这条路线:
for x in gen1:
gen2(x)
gen3(x)
但是很丑,也不优雅。我正在考虑一种更实用的做事方式。
最佳答案
您可以将生成器变成协程,这样它们就可以send()
并从彼此接收值(使用(yield)
表达式)。这将使每个人都有机会更改他们收到的值,和/或将它们传递给下一个生成器/协程(或完全忽略它们)。
请注意,在下面的示例代码中,我使用了一个名为 coroutine
的装饰器来“初始化”生成器/协程函数。这导致它们执行到恰好在它们的第一个 yield
表达式/语句之前。它是这个 youtube 视频中显示的一个非常有启发性的演讲的一个略微修改的版本,标题为 A Curious Course on Coroutines and Concurrency Dave Beazley 在 PyCon 2009 上给出的。
正如您应该能够从生成的输出中看到的那样,数据值正在由通过单个 send()
配置到头部协程的每个管道处理,然后有效地“多路复用“它在每条管道中。由于每个子协程也这样做,因此可以建立一个精心设计的进程“树”。
import sys
def coroutine(func):
""" Decorator to "prime" generators used as coroutines. """
def start(*args,**kwargs):
cr = func(*args,**kwargs) # Create coroutine generator function.
next(cr) # Advance to just before its first yield.
return cr
return start
def pipe(name, value, divisor, coroutines):
""" Utility function to send values to list of coroutines. """
print(' {}: {} is divisible by {}'.format(name, value, divisor))
for cr in coroutines:
cr.send(value)
def this_func_name():
""" Helper function that returns name of function calling it. """
frame = sys._getframe(1)
return frame.f_code.co_name
@coroutine
def gen1(*coroutines):
while True:
value = (yield) # Receive values sent here via "send()".
if value % 2 == 0: # Only pipe even values.
pipe(this_func_name(), value, 2, coroutines)
@coroutine
def gen2(*coroutines):
while True:
value = (yield) # Receive values sent here via "send()".
if value % 4 == 0: # Only pipe values divisible by 4.
pipe(this_func_name(), value, 4, coroutines)
@coroutine
def gen3(*coroutines):
while True:
value = (yield) # Receive values sent here via "send()".
if value % 6 == 0: # Only pipe values divisible by 6.
pipe(this_func_name(), value, 6, coroutines)
# Create and link together some coroutine pipelines.
g3 = gen3()
g2 = gen2()
g1 = gen1(g2, g3)
# Send values through both pipelines (g1 -> g2, and g1 -> g3) of coroutines.
for value in range(17):
print('piping {}'.format(value))
g1.send(value)
输出:
piping 0
gen1: 0 is divisible by 2
gen2: 0 is divisible by 4
gen3: 0 is divisible by 6
piping 1
piping 2
gen1: 2 is divisible by 2
piping 3
piping 4
gen1: 4 is divisible by 2
gen2: 4 is divisible by 4
piping 5
piping 6
gen1: 6 is divisible by 2
gen3: 6 is divisible by 6
piping 7
piping 8
gen1: 8 is divisible by 2
gen2: 8 is divisible by 4
piping 9
piping 10
gen1: 10 is divisible by 2
piping 11
piping 12
gen1: 12 is divisible by 2
gen2: 12 is divisible by 4
gen3: 12 is divisible by 6
piping 13
piping 14
gen1: 14 is divisible by 2
piping 15
piping 16
gen1: 16 is divisible by 2
gen2: 16 is divisible by 4
关于python - 如何使用生成器形成多个管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45924087/
我正在阅读 deduction guides在 C++17 中。假设我们有以下示例: template struct Custom { }; template struct Person {
我在我的 xamarin 表单项目中使用选项卡式页面。我正在尝试在 Android 的 MyTabsRenderer 类中使用 OnTabReselected 事件。但不会调用 OnTabSelect
我对 NSPredicate 有疑问。想不出一种写法,也找不到类似的东西。我有这个谓词: [NSPredicate predicateWithFormat:@"followedBy.username
我的模态中有一个小表单,如下所示: Name
我正在尝试制作用于表单验证的 jquery 插件(用于学习)。在此表单中,我无法获取类名称为“required”的所有表单字段。代码如下: (function( $ ) { $.fn.kValidat
在我的 Android 应用中,我从 Google Place API 获取附近餐馆的列表。 但不幸的是,这个列表没有给出餐厅的菜单。 我有 T_RESTAURANT 和 T_MENU 表。 假设我在
我正在尝试使用 angular.js 和 devise 设置登录。 这是我的表单 html: Email Password
谁能告诉我如何让生成的文档从表单中提取数据并将其作为标题?我已经查看了 Google Script 文档,但到目前为止我还没有找到任何可以回答我或向我展示类似示例的内容。到目前为止,我在这里找到了相关
当我有这样的表格时: “.”是什么意思?在行动中代表什么? 最佳答案 action 属性告诉表单将表单数据发布到哪里。 . 代表当前目录,所以我会说这是发布到当前目录中的默认文档。 相对路径有几
Mockito 似乎是一个非常漂亮的 Java stub /模拟框架。唯一的问题是我找不到任何关于使用他们的 API 的最佳方式的具体文档。测试中常用的方法包括: doXXX(???) : Stubb
我有 2 份表格。我从一种形式创建并展示了另一种形式。效果很好。但是,当我尝试从创建该表单的表单中关闭或处理该表单时,出现以下异常: Exception : Value Dispose() can
将我的应用程序上传到 TestFlight 时出现以下错误。 但是,我没有看到 missing 的任何位置Xamarin Assets 菜单中的图标。 (76x76、167x167 和 152x152
我的models.py文件看起来像这样 from django.db import models from django.template.defaultfilters import slugify
问题 学习 Xamarin 大学类(class) XAM120 .在将我的 IDial 实现添加到我的 UWP 项目时遇到障碍。出于某种原因,我的项目没有在我的系统上使用 PhoneCallManag
我在应用程序的列表页面上使用了 FloatingActionButton,我还想添加一个 searchBar。但我无法向他们展示该页面。我的代码和屏幕截图已关闭。如何显示搜索栏? FAB Github
实体产品和类别之间存在经典的多对多关系,其中一个产品可能包含在多个类别中。我们想在 UI 中使用带有 UITableViewController 或 UICollectionView 的 NSFetc
html 代码: js代码: function show(){ $.ajax({
我有一个用户列表。现在任何一个名字很长的用户都在搞乱排列/排列。 我认为通过为名称设置大小可以达到目的: .invitee .name{ height: 50px; width: 115px;
我正在使用 Flask 框架和 WTforms 库,我在更改选择字段中每个选项的颜色时遇到了问题,因为它总是显示为黑色而不是红色 我在模板中有下一个表单
Dugen Chen 写了一篇有用的文章,介绍如何将 HTML5 验证中的“required”属性添加到 Django 表单字段。 http://duganchen.ca/elegantly-addi
我是一名优秀的程序员,十分优秀!