I was playing around with Python context managers today and an idea
came to me. I wonder if they could be used in wxPython some how. In
more than one place in my app I might show a dialog for a short period
and you always need to remember to destroy it when you are done. With
context managers you could have the destroy happen "automatically".
Check out the included script as an example. I'm sure there are other
more clever uses but this was the first that came to mind.
--Mark
That's a very good idea, thanks for bringing it to our attention.
···
On 2/4/10 11:54 AM, Mark Guagenti wrote:
I was playing around with Python context managers today and an idea
came to me. I wonder if they could be used in wxPython some how. In
more than one place in my app I might show a dialog for a short period
and you always need to remember to destroy it when you are done. With
context managers you could have the destroy happen "automatically".
Check out the included script as an example. I'm sure there are other
more clever uses but this was the first that came to mind.
Ok, I'm going to go ahead and add the context manager protocol to wx.Dialog. There are also several helper classes in wxWidgets that seem to be a good fit for the pattern so I'll be adding it to them too[1]. Are there any other wx classes or other cases where it would also make sense to add the context manager methods? Suggestions are welcome.
[1] Although in these cases the __enter__ and __exit__ won't actually be doing anything as the C++ class will be doing the real work. Here are the other classes I've modified so far:
I was playing around with Python context managers today and an idea
came to me. I wonder if they could be used in wxPython some how. In
more than one place in my app I might show a dialog for a short period
and you always need to remember to destroy it when you are done. With
context managers you could have the destroy happen "automatically".
Check out the included script as an example. I'm sure there are other
more clever uses but this was the first that came to mind.
That's a very good idea, thanks for bringing it to our attention.
I was playing around with Python context managers today and an idea
came to me. I wonder if they could be used in wxPython some how. In
more than one place in my app I might show a dialog for a short period
and you always need to remember to destroy it when you are done. With
context managers you could have the destroy happen "automatically".
Check out the included script as an example. I'm sure there are other
more clever uses but this was the first that came to mind.
That's a very good idea, thanks for bringing it to our attention.
Ok, I'm going to go ahead and add the context manager protocol to wx.Dialog. There are also several helper classes in wxWidgets that seem to be a good fit for the pattern so I'll be adding it to them too[1]. Are there any other wx classes or other cases where it would also make sense to add the context manager methods? Suggestions are welcome.
There's no wx helper class for this, but I can imagine that window.Freeze(); do stuff; window.Thaw() is a common case that fits this pattern. Maybe something like "with wx.Freeze(window):"?
Regards,
Kevin
···
On Feb 6, 2010, at 12:33 PM, Robin Dunn wrote:
On 2/5/10 11:16 AM, Robin Dunn wrote:
On 2/4/10 11:54 AM, Mark Guagenti wrote:
[1] Although in these cases the __enter__ and __exit__ won't actually be doing anything as the C++ class will be doing the real work. Here are the other classes I've modified so far:
There's no wx helper class for this, but I can imagine that window.Freeze(); do stuff; window.Thaw() is a common case that fits this pattern. Maybe something like "with wx.Freeze(window):"?
I'm glad that you guys like my idea and are running with it!
--Mark
···
On Sun, Feb 7, 2010 at 7:54 PM, Robin Dunn <robin@alldunn.com> wrote:
On 2/6/10 12:55 PM, Kevin Ollivier wrote:
There's no wx helper class for this, but I can imagine that
window.Freeze(); do stuff; window.Thaw() is a common case that fits this
pattern. Maybe something like "with wx.Freeze(window):"?
For DC objects...
dc.SelectPen
dc.SetBrush
dc.SelectObject
ALL of these, according to the docs, must have a null object selected
into them in order to properly reset them....
so if I...
dc.SelectObject(some_bitmap_object)
...
later on I must
dc.SelectObject(wx.NullBitmap)
I think it'd be MUCH nicer if I could:
with dc.SelectObject(some_bitmap_object):
...
and have it understood that the wx.NullBitmap would be selected into
it at the end of the "with" block.
Chris.
···
On Sat, 06 Feb 2010 12:33:09 -0800, Robin Dunn <robin@alldunn.com> wrote:
On 2/5/10 11:16 AM, Robin Dunn wrote:
On 2/4/10 11:54 AM, Mark Guagenti wrote:
I was playing around with Python context managers today and an idea
came to me. I wonder if they could be used in wxPython some how. In
more than one place in my app I might show a dialog for a short period
and you always need to remember to destroy it when you are done. With
context managers you could have the destroy happen "automatically".
Check out the included script as an example. I'm sure there are other
more clever uses but this was the first that came to mind.
That's a very good idea, thanks for bringing it to our attention.
Ok, I'm going to go ahead and add the context manager protocol to
wx.Dialog. There are also several helper classes in wxWidgets that seem
to be a good fit for the pattern so I'll be adding it to them too[1].
Are there any other wx classes or other cases where it would also make
sense to add the context manager methods? Suggestions are welcome.
[1] Although in these cases the __enter__ and __exit__ won't actually be
doing anything as the C++ class will be doing the real work. Here are
the other classes I've modified so far:
Although it's almost never done, at least with pens and brushes, so it isn't really necessary. For bitmaps it's only an issue if you try to use the bitmap in another DC or in a DrawBitmap call while it is still selected into the first DC.
For the pens and brushes I've already mentioned that wx.DCPenChanger and wx.DCBrushChanger have been adapted for context manager protocol and they will fill the need you've identified. I'll give some thought to what to do for memory DCs and bitmaps.
···
On 2/9/10 2:46 AM, Chris Spencer wrote:
For DC objects...
dc.SelectPen
dc.SetBrush
dc.SelectObject
ALL of these, according to the docs, must have a null object selected
into them in order to properly reset them....