Can I use python with statement for conditional execution?

故事扮演 提交于 2019-12-03 09:16:43

问题


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?


回答1:


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.




回答2:


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



回答3:


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.




回答4:


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()


来源:https://stackoverflow.com/questions/4314338/can-i-use-python-with-statement-for-conditional-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!