我可以使用 python with 语句进行条件执行吗?

发布于 2024-10-05 00:39:29 字数 1222 浏览 0 评论 0原文

我正在尝试编写支持以下语义的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤君无依 2024-10-12 00:39:29

您正在尝试修改基本语言构造的预期行为。这绝不是一个好主意,它只会导致混乱。

您的解决方法没有任何问题,但您可以稍微简化一下。

@contextmanager 
def scope(action): 
  yield action != 'bypass'

with scope('action_name') as s: 
  if s: 
    do_something() 
    ... 
do_some_other_stuff() 

您的 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.

@contextmanager 
def scope(action): 
  yield action != 'bypass'

with scope('action_name') as s: 
  if s: 
    do_something() 
    ... 
do_some_other_stuff() 

Your scope could instead be a class whose __enter__ method returns either a useful object or None and it would be used in the same fashion.

知你几分 2024-10-12 00:39:29

以下似乎可行:

from contextlib import contextmanager

@contextmanager
def skippable():
    try:
        yield
    except RuntimeError as e:
        if e.message != "generator didn't yield":
            raise

@contextmanager
def context_if_condition():
    if False:
        yield True

with skippable(), context_if_condition() as ctx:
    print "won't run"

注意事项:

  • 需要有人想出更好的名称
  • context_if_condition 不能在没有 skippable 的情况下使用,但没有办法强制执行/删除
  • 它的 冗余可以从比预期更深的函数捕获并抑制 RuntimeError (自定义异常可以帮助解决这个问题,但这使得整个构造仍然更加混乱),
  • 这并不比仅使用 @Mark Ransom 的版本更清晰

The following seems to work:

from contextlib import contextmanager

@contextmanager
def skippable():
    try:
        yield
    except RuntimeError as e:
        if e.message != "generator didn't yield":
            raise

@contextmanager
def context_if_condition():
    if False:
        yield True

with skippable(), context_if_condition() as ctx:
    print "won't run"

Considerations:

  • needs someone to come up with better names
  • context_if_condition can't be used without skippable but there's no way to enforce that/remove the redundancy
  • it could catch and suppress the RuntimeError from a deeper function than intended (a custom exception could help there, but that makes the whole construct messier still)
  • it's not any clearer than just using @Mark Ransom's version
街道布景 2024-10-12 00:39:29

我认为这是不可能的。我尝试将上下文管理器实现为一个类,但没有办法强制块引发异常,该异常随后会被 __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.

梨涡 2024-10-12 00:39:29

我和你有同样的用例,并且遇到了有人提供帮助的 条件库自从您发布问题以来,我们一直在开发。

从该网站来看,其用途如下:

with conditional(CONDITION, CONTEXTMANAGER()):
    BODY()

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:

with conditional(CONDITION, CONTEXTMANAGER()):
    BODY()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文