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 Thu, Nov 27, 2014 at 11:09 PM, Stefano Borini <stefano.borini@ferrara.linux.it> wrote:
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.
You could abuse try/finally for this purpose. try: whatever you need to do finally: my_widget.setFocus() ChrisA
On Thu, Nov 27, 2014 at 8:00 AM, Chris Angelico <rosuav@gmail.com> wrote:
You could abuse try/finally for this purpose.
try: whatever you need to do finally: my_widget.setFocus()
There's no abuse there. "finally" is there precisely for that sort of intention. I think that what the OP wants is to "announce" the final action at the -- Juancarlo *Añez*
On Thu, 27 Nov 2014 23:30:40 +1100 Chris Angelico <rosuav@gmail.com> wrote:
On Thu, Nov 27, 2014 at 11:09 PM, Stefano Borini <stefano.borini@ferrara.linux.it> wrote:
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.
You could abuse try/finally for this purpose.
Or just use it, since it's a normal use of try/finally ;-) Regards Antoine.
try: whatever you need to do finally: my_widget.setFocus()
ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
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:
You could abuse try/finally for this purpose.
try: whatever you need to do finally: my_widget.setFocus()
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.
On Thu, Nov 27, 2014 at 9:24 AM, Stefano Borini < stefano.borini@ferrara.linux.it> wrote:
On Thu, Nov 27, 2014 at 11:30:40PM +1100, Chris Angelico wrote:
You could abuse try/finally for this purpose.
try: whatever you need to do finally: my_widget.setFocus()
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.
If there is absolutely no possibility of an exception, you don't need any special construct. Just use
whatever you need to do my_widget.setFocus() Otherwise, you are tacitly agreeing that an exception *might* occur, so try/finally is not misleading.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
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:
On Thu, Nov 27, 2014 at 9:24 AM, Stefano Borini < stefano.borini@ferrara.linux.it> wrote:
On Thu, Nov 27, 2014 at 11:30:40PM +1100, Chris Angelico wrote:
You could abuse try/finally for this purpose.
try: whatever you need to do finally: my_widget.setFocus()
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.
If there is absolutely no possibility of an exception, you don't need any special construct. Just use
whatever you need to do my_widget.setFocus()
Otherwise, you are tacitly agreeing that an exception *might* occur, so try/finally is not misleading.
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
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Stefano Borini <stefano.borini@ferrara.linux.it> writes:
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.
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:
Stefano Borini <stefano.borini@ferrara.linux.it> writes:
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.
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
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:
with contextlib.context(enter=None, exit=lambda *args: my_widget.setFocus()): do what I need to do
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