我可以使用 python with 语句进行条件执行吗?
我正在尝试编写支持以下语义的代码:
with scope('action_name') as s:
do_something()
...
do_some_other_stuff()
范围以及其他事项(设置、清理)应决定此部分是否应运行。
例如,如果用户将程序配置为绕过“action_name”,则在评估 Scope() 后,将执行 do_some_other_stuff(),而不先调用 do_something()。
我尝试使用此上下文管理器来执行此操作:
@contextmanager
def scope(action):
if action != 'bypass':
yield
但得到了 RuntimeError: Generator did notield
异常(当 action
为 'bypass'
时)。
我正在寻找一种方法来支持这一点,而不需要退回到更详细的可选实现:
with scope('action_name') as s:
if s.should_run():
do_something()
...
do_some_other_stuff()
有谁知道我如何实现这一点?
谢谢!
PS我正在使用python2.7
编辑:
该解决方案不一定必须依赖 with
语句。我只是不知道如何在没有它的情况下确切地表达它。本质上,我想要上下文形式的东西(支持设置和自动清理,与所包含的逻辑无关),并允许基于传递给设置方法并在配置中选择的参数进行条件执行。
我还考虑了使用装饰器的可能解决方案。示例:
@scope('action_name') # if 'action_name' in allowed actions, do:
# setup()
# do_action_name()
# cleanup()
# otherwise return
def do_action_name()
do_something()
但我不想根据这些范围强制执行太多内部结构(即代码如何划分为函数)。
有人有一些创意吗?
I'm trying to write code that supports the following semantics:
with scope('action_name') as s:
do_something()
...
do_some_other_stuff()
The scope, among other things (setup, cleanup) should decide if this section should run.
For instance, if the user configured the program to bypass 'action_name' than, after Scope() is evaluated do_some_other_stuff() will be executed without calling do_something() first.
I tried to do it using this context manager:
@contextmanager
def scope(action):
if action != 'bypass':
yield
but got RuntimeError: generator didn't yield
exception (when action
is 'bypass'
).
I am looking for a way to support this without falling back to the more verbose optional implementation:
with scope('action_name') as s:
if s.should_run():
do_something()
...
do_some_other_stuff()
Does anyone know how I can achieve this?
Thanks!
P.S. I am using python2.7
EDIT:
The solution doesn't necessarily have to rely on with
statements. I just didn't know exactly how to express it without it. In essence, I want something in the form of a context (supporting setup and automatic cleanup, unrelated to the contained logic) and allowing for conditional execution based on parameters passed to the setup method and selected in the configuration.
I also thought about a possible solution using decorators. Example:
@scope('action_name') # if 'action_name' in allowed actions, do:
# setup()
# do_action_name()
# cleanup()
# otherwise return
def do_action_name()
do_something()
but I don't want to enforce too much of the internal structure (i.e., how the code is divided to functions) based on these scopes.
Does anybody have some creative ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在尝试修改基本语言构造的预期行为。这绝不是一个好主意,它只会导致混乱。
您的解决方法没有任何问题,但您可以稍微简化一下。
您的
scope
可以是一个类,其__enter__
方法返回有用的对象或None
并且将以相同的方式使用它。You're trying to modify the expected behaviour of a basic language construct. That's never a good idea, it will just lead to confusion.
There's nothing wrong with your work-around, but you can simplify it just a bit.
Your
scope
could instead be a class whose__enter__
method returns either a useful object orNone
and it would be used in the same fashion.以下似乎可行:
注意事项:
context_if_condition
不能在没有skippable
的情况下使用,但没有办法强制执行/删除The following seems to work:
Considerations:
context_if_condition
can't be used withoutskippable
but there's no way to enforce that/remove the redundancy我认为这是不可能的。我尝试将上下文管理器实现为一个类,但没有办法强制块引发异常,该异常随后会被
__exit__()
方法压制。I don't think this can be done. I tried implementing a context manager as a class and there's just no way to force the block to raise an exception which would subsequently be squelched by the
__exit__()
method.我和你有同样的用例,并且遇到了有人提供帮助的 条件库自从您发布问题以来,我们一直在开发。
从该网站来看,其用途如下:
I have the same use case as you, and came across the conditional library that someone has helpfully developed in the time since you posted your question.
From the site, its use is as: