contextmanager - How do I write a three-block context manager in Python? -


i have many functions exploit context manager pattern:

@contextmanager def f():     # preliminary stuff.     yield     # final stuff. 

i use exitstack call of these context managers.

i considering pattern:

@threeblock_contextmanager def f():     # preliminary stuff.     yield     # intermediary stuff.     yield     # final stuff. 

i've been looking @ the source , thought modify in way. there nice way accomplish this? other option add method each class returns context manager. disadvantage of adding method intuitive linear view of code lost because intermediary stuff in different method rather placed between preliminary , final stuff, consider more logical.


as requested, more code:

def fire(self, cluster_index, obs_count=1.0):     exitstack() stack:         link in self.terminal.out:             stack.enter_context(                 link.source_firing(cluster_index, obs_count))         link in self.terminal.in_:             stack.enter_context(                 link.target_firing(cluster_index, obs_count))          # @ point context managers have been run until         # yield.   saw signal neither updated @         # source nor target.          # update signals.          # todo: @ point, context managers need send signals         # want send , yield again.      # @ point context managers have been run entirely.     # on closing, saw signal has been updated @ both source     # , receiver.      self.ask_for_reschedule() 


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -