
I have a situation where, no matter how the routine ends, I need to return the focus to a widget. It would be rather clever to do this with a context, but I would expect something like this to be a possible strategy with contextlib.context(enter=None, exit=lambda *args: my_widget.setFocus()): do what I need to do as far as I know, at the moment it's not possible. Am I right? I think it would be an easy and practical addition to the contextlib module to quickly register two routines for enter and exit.

On Fri, Nov 28, 2014 at 12:17 AM, Antoine Pitrou <solipsis@pitrou.net> wrote:
You could abuse try/finally for this purpose.
Or just use it, since it's a normal use of try/finally ;-)
I guess so. I normally think of try/finally as an implication that an exception is expected (rather than a function-level "atexit" type of thing), but that's what's happening here. So yes, take off the "ab" and just use try/finally. ChrisA

On Thu, Nov 27, 2014 at 11:30:40PM +1100, Chris Angelico wrote:
I thought about this solution, but I am concerned about communication of intent. Using the try communicates to a code reader that the code inside is expected to throw, which is not the case. However, I agree there are good alternative strategies.

Hi Clint, (this is a bit of nit-picking, but kind of supports the OPs concerns about intent) On 27.11.2014 17:01, Clint Hepner wrote:
Not neccessarily. try/finally can also be used to do something in all return paths: def foo(): try: return True finally: print("foo") will print foo and return True. There is no way this can throw, nevertheless try/finally can be useful if you have to do some clean up and have several return paths. regards, jwi

Stefano Borini <stefano.borini@ferrara.linux.it> writes:
with ExitStack() as stack: stack.callback(my_widget.setFocus) cm = stack.enter_context(run_your_enter_routine()) https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack -- Akira

On 27 November 2014 at 23:04, Akira Li <4kir4.1i@gmail.com> wrote:
Right, while the underlying try/except/finally construct is generally a better option for simple cases of inline ad hoc exception handling, ExitStack is useful when you really do want to reproduce the context management protocols "pass the exception details to a callback" behaviour. For example, the feature requested by the OP can be implemented as: @contextmanager def context(enter=None, exit=None): enter_result = enter() if enter is not None else None with ExitStack() as stack: if exit is not None: stack.push(exit) yield enter_result However, I'm hard pressed to think of a case where using such a construct would be clearer than writing out a suitable try/except/finally block. A lot of the value of context managers lies in our ability to give them *names*, such that it's immediately clear what they're doing (or which docs to look up to find out more). Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On Thu, Nov 27, 2014 at 7:39 AM, Stefano Borini < stefano.borini@ferrara.linux.it> wrote:
You can easily define your own context manager to do what you want: @contextmanager def finally_focus(widget): try: yield finally: widget.setFocus() with finally_focus(my_widget): # do what I need to do -- Juancarlo *Añez*

On Fri, Nov 28, 2014 at 12:17 AM, Antoine Pitrou <solipsis@pitrou.net> wrote:
You could abuse try/finally for this purpose.
Or just use it, since it's a normal use of try/finally ;-)
I guess so. I normally think of try/finally as an implication that an exception is expected (rather than a function-level "atexit" type of thing), but that's what's happening here. So yes, take off the "ab" and just use try/finally. ChrisA

On Thu, Nov 27, 2014 at 11:30:40PM +1100, Chris Angelico wrote:
I thought about this solution, but I am concerned about communication of intent. Using the try communicates to a code reader that the code inside is expected to throw, which is not the case. However, I agree there are good alternative strategies.

Hi Clint, (this is a bit of nit-picking, but kind of supports the OPs concerns about intent) On 27.11.2014 17:01, Clint Hepner wrote:
Not neccessarily. try/finally can also be used to do something in all return paths: def foo(): try: return True finally: print("foo") will print foo and return True. There is no way this can throw, nevertheless try/finally can be useful if you have to do some clean up and have several return paths. regards, jwi

Stefano Borini <stefano.borini@ferrara.linux.it> writes:
with ExitStack() as stack: stack.callback(my_widget.setFocus) cm = stack.enter_context(run_your_enter_routine()) https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack -- Akira

On 27 November 2014 at 23:04, Akira Li <4kir4.1i@gmail.com> wrote:
Right, while the underlying try/except/finally construct is generally a better option for simple cases of inline ad hoc exception handling, ExitStack is useful when you really do want to reproduce the context management protocols "pass the exception details to a callback" behaviour. For example, the feature requested by the OP can be implemented as: @contextmanager def context(enter=None, exit=None): enter_result = enter() if enter is not None else None with ExitStack() as stack: if exit is not None: stack.push(exit) yield enter_result However, I'm hard pressed to think of a case where using such a construct would be clearer than writing out a suitable try/except/finally block. A lot of the value of context managers lies in our ability to give them *names*, such that it's immediately clear what they're doing (or which docs to look up to find out more). Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On Thu, Nov 27, 2014 at 7:39 AM, Stefano Borini < stefano.borini@ferrara.linux.it> wrote:
You can easily define your own context manager to do what you want: @contextmanager def finally_focus(widget): try: yield finally: widget.setFocus() with finally_focus(my_widget): # do what I need to do -- Juancarlo *Añez*
participants (8)
-
Akira Li
-
Antoine Pitrou
-
Chris Angelico
-
Clint Hepner
-
Jonas Wielicki
-
Juancarlo Añez
-
Nick Coghlan
-
Stefano Borini