Question about Namespace

Hello,

I have written many programs with Python and wxPython, from examples I have seen on different sites.
This examples were on the form :
from wxPython.wx import *

I just understood that It would be better to modify in the next form :
import wxPython.wx

Is it only better or absoluement necessary ?
**Is there an impact on the performances ?
Is it possible, upgrades will be blocked in the future, if namespace not modified ?

Sorry for my bad English language.
Thanks in advance for your help.

Yours.

···

--

Hugues JEAN-BAPTISTE (hjb@agorinfo.fr)
AGORINFO S.A.
1330, route de Neufchâtel
F-76230 QUINCAMPOIX
Tél : +33 (0) 2.32.80.87.87
Fax : +33 (0) 2.32.80.87.88
Site : http://www.agorinfo.fr

the use of 'from wxPython.wx import *' and 'import wxPython.wx' are both deprecated the recomanded way is:

import wx

I don't think performance is an issue BUT ease of upgrading IS.

Also for new apps I would suggest using the 2.5 branch of wxpython, it is quite stable and since 2.4 branch is closed (I understood that 2.4.2.4 is the last 2.4 release) it is a wise choice too.

support for 2.5 is excelent as attention of the wxpython developers is set on it.

for tutorial purposes use the demo.

···

On Tue, 20 Apr 2004 09:54:57 +0200, Hugues JEAN-BAPTISTE <hjb@agorinfo.fr> wrote:

Hello,

I have written many programs with Python and wxPython, from examples I have seen on different sites.
This examples were on the form :
from wxPython.wx import *

I just understood that It would be better to modify in the next form :
import wxPython.wx

Is it only better or absoluement necessary ?
**Is there an impact on the performances ?
Is it possible, upgrades will be blocked in the future, if namespace not modified ?

Sorry for my bad English language.
Thanks in advance for your help.

Yours.

--
Peter Damoc
Hacker Wannabe
http://www.sigmacore.net/about.html

Hugues JEAN-BAPTISTE wrote:

Hello,

I have written many programs with Python and wxPython, from examples I have seen on different sites.
This examples were on the form :
from wxPython.wx import *

I just understood that It would be better to modify in the next form :
import wxPython.wx

Other than placing a copy of everything in the local namespace, these two imports are basically the same. What has changed is that there is a new top level package named "wx" and the names within it have had the "wx" prefix stripped off. So instead of

  from wxPython.wx import *
  ...
  f = wxFrame(...)

You can now do

  import wx
  ...
  f = wx.Frame(...)

For more details of what's new please read the Migration Guide at http://wxpython.org/MigrationGuide.html

Is it only better or absoluement necessary ?

Both will be supported for a while, but eventually the "wxPython" top level package will be deprecated and removed.

**Is there an impact on the performances ?

Probably not enough to notice.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Peter Damoc wrote:

Also for new apps I would suggest using the 2.5 branch of wxpython, it is quite stable and since 2.4 branch is closed (I understood that 2.4.2.4 is the last 2.4 release) it is a wise choice too.

I think that there would be some value in a 2.4.2.5 release, (or maybe a 2.4.3 release if the C++ side of things moves forward too.) It's just a matter of finding the time to work on it. Right now I'm still neck-deep in 2.5 issues, plus getting ready for OSCON, (3 presentaions and a BOF! Was I stupid?! <wink>) plus the book, plus...

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

It might be *some* value in another 2.4 release BUT I think the time is way better spent working on 2.5
I also think that 2.5 needs more promotion, more good vibe around it, I think a lot of people get scared by the odd number after the dot, I know I did at first :smiley:

···

On Tue, 20 Apr 2004 10:03:01 -0700, Robin Dunn <robin@alldunn.com> wrote:

Peter Damoc wrote:

Also for new apps I would suggest using the 2.5 branch of wxpython, it is quite stable and since 2.4 branch is closed (I understood that 2.4.2.4 is the last 2.4 release) it is a wise choice too.

I think that there would be some value in a 2.4.2.5 release, (or maybe a 2.4.3 release if the C++ side of things moves forward too.) It's just a matter of finding the time to work on it. Right now I'm still neck-deep in 2.5 issues, plus getting ready for OSCON, (3 presentaions and a BOF! Was I stupid?! <wink>) plus the book, plus...

--
Peter Damoc
Hacker Wannabe
http://www.sigmacore.net/about.html

Peter Damoc writes:

It might be *some* value in another 2.4 release BUT I think
the time is way better spent working on 2.5
I also think that 2.5 needs more promotion, more good vibe
around it, I think a lot of people get scared by the odd
number after the dot, I know I did at first :smiley:

I for one have to say that I will never willingly go back to 2.4
after using 2.5 for the past few weeks. I've been able to
simplify lots of things greatly, from my mixin classes that can
now be Python newstyle classes (so can use super() and define
properties), to the new Window.Bind() syntax, to all sorts of
things that just work now instead of causing headaches, such as
wxMac.

Perhaps I'm just graduating from newbie to intermediate wxPython
user, but 2.5 feels *so much better* than 2.4, and I loved 2.4
too. :wink:

···

--
Paul

I also think that 2.5 needs more promotion, more good vibe around it, I
think a lot of people get scared by the odd number after the dot, I know I
did at first :smiley:

The thing that has stopped me is not being able to support 2.4 and 2.5
on the same developer machine.

I then tried to make my code work with both 2.4 and 2.5 (ie whatever
anyone has installed). I had already done the wx namespace transition
months ago.

Unfortunately it required too many code changes like this:

   OLD: mdc.DrawBitmap(bmp, 10, 20)

   NEW: if wx.VERSION>=(2,5):
             mdc.DrawBitmap(bmp, (10, 20))
         else:
             mdc.DrawBitmap(bmp, 10, 20)

I started littering my code like that but eventually gave up since
there were too many spots scattered throughout the code.

If there was a 2.4.2.5 release that added the XY forms, then I could
quite happily make my code work with both releases. I would still need
the if statements for adding space in sizers, but that isn't done
in many places so I can live with it.

The new Bind stuff also baffles me. I don't see *ANY* benefit to changing
my existing code to use it.

Roger

Roger Binns wrote:

I then tried to make my code work with both 2.4 and 2.5 (ie whatever
anyone has installed). I had already done the wx namespace transition
months ago.

I can see the desire to support both, and it is a pain if you use the new namespace

If there was a 2.4.2.5 release that added the XY forms, then I could
quite happily make my code work with both releases.

but then it would only support a NEW 2.4.* release, and if you're going to download and install a new 2.4.*, why not just install 2.5.* ?

The new Bind stuff also baffles me. I don't see *ANY* benefit to changing
my existing code to use it.

I, for one, really like the syntax better, and for menus it is a major improvement.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Roger Binns writes:

The new Bind stuff also baffles me. I don't see *ANY*
benefit to changing my existing code to use it.

I love the new Bind because it correctly states the action
(Bind) being made by the object, and the parameters
are what event we are binding and who is receiving the
event. IOW, the parameters are the two ends of the
connection. Much much cleaner, although admittedly the
result is the same and the line isn't much shorter.

Also, the id optional parameters are at the end, and
aren't even usually needed, so most binds can look like:

checkbox.Bind(wx.EVT_CHECKBOX, frame.onSexToggle)

The checkbox's event is getting bound to the frame's method, and that is readily apparent by the cleaner syntax.

One thing that baffles me is the following in the docstring:
Bind(self, event, handler, source=None, id=-1, id2=-1)

···

     Bind an event to an event handler.

>
> event One of the EVT_* objects that specifies the
> type of event to bind,
>
> handler A callable object to be invoked when the event
> is delivered to self. Pass None to disconnect an
> event handler.
>

--
Paul

Sorry, disregard my previously incompleted message.

Roger Binns writes:

The new Bind stuff also baffles me. I don't see *ANY*
benefit to changing my existing code to use it.

I love the new Bind because it correctly states the action
(Bind) being made by the object, and the parameters
are what event we are binding and who is receiving the
event. IOW, the parameters are the two ends of the
connection. Much much cleaner, although admittedly the
result is the same and the line isn't much shorter.

Also, the id optional parameters are at the end, and
aren't even usually needed, so most binds can look like:

checkbox.Bind(wx.EVT_CHECKBOX, frame.onSexToggle)

The checkbox's event is getting bound to the frame's
method, and that is readily apparent by the cleaner syntax.

One thing that baffles me is the following in the docstring:
Bind(self, event, handler, source=None, id=-1, id2=-1)
> Bind an event to an event handler.
>
> event One of the EVT_* objects that specifies the
> type of event to bind,
>
> handler A callable object to be invoked when the event
> is delivered to self. Pass None to disconnect an
> event handler.
>

Can't an event be bound to multiple handlers, as in:

checkbox.Bind(wx.EVT_CHECKBOX, frame.method1)
checkbox.Bind(wx.EVT_CHECKBOX, frame.method2)

At which point:
checkbox.Bind(wx.EVT_CHECKBOX, None)

has exactly what effect? Does it unbind both?

I'd like to see something much more explicit, as in:
checkbox.UnBind(wx.EVT_CHECKBOX, frame.method1)

... and that would raise an exception if not previously bound.

···

--
Paul

Chris Barker wrote:

Roger Binns wrote:
> If there was a 2.4.2.5 release that added the XY forms, then I could
> quite happily make my code work with both releases.

but then it would only support a NEW 2.4.* release, and if you're going
to download and install a new 2.4.*, why not just install 2.5.* ?

My project does not exist in isolation, or only on one platform! If it
did then things would be easy :slight_smile:

I would assume an update to 2.4.2.4 wouldn't break anything, and
there would be no issue for anyone installing it.

> The new Bind stuff also baffles me. I don't see *ANY* benefit to changing
> my existing code to use it.

I, for one, really like the syntax better, and for menus it is a major
improvement.

The syntax isn't notably different, more of a reordering of existing
parameters (and Bind added near the begining). My coding style is to
put almost all the EVT_ stuff at the end of __init__ so the new menu
stuff wouldn't help.

As for Paul's comment on being able to see what it being linked(bound)
to what, that is clear with the old syntax as well. (If it wasn't
someone would have noticed by now :slight_smile:

What I would really like to do is move over to using XRC for as much
of the UI as possible. For some bizarre reason, it won't let me make
a menubar a child of a frame! We also have code that saves and restores
window sizes and positions and I don't know how much work that would
be to get working with XRC.

Roger

Roger Binns writes:

As for Paul's comment on being able to see what it being
linked(bound) to what, that is clear with the old syntax as
well. (If it wasn't someone would have noticed by now :slight_smile:

It is simply better grammar with the new style, that's all. :wink:

···

--
Paul

Roger Binns wrote:

My project does not exist in isolation, or only on one platform! If it did then things would be easy :slight_smile:

I would assume an update to 2.4.2.4 wouldn't break anything, and
there would be no issue for anyone installing it.

good point, and this is exactly why I'm trying to get a proposal together to have multiple versions of wxPython exist in one install. The app you're working on now could use 2.5.*, and any pre-existing apps would keep using 2.4.*

Let's make this happen!

The syntax isn't notably different, more of a reordering of existing
parameters (and Bind added near the begining).

true.

My coding style is to
put almost all the EVT_ stuff at the end of __init__ so the new menu
stuff wouldn't help.

What's that got to do with it? What I was referring to is that it's a whole lot easier to bind a menu event without hard-coding an ID for the menu:

item = file_menu.Append(-1, "&Close","Close this frame")
self.Bind(wx.EVT_MENU, self.OnQuit, item)

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

> My coding style is to
> put almost all the EVT_ stuff at the end of __init__ so the new menu
> stuff wouldn't help.

What's that got to do with it?

It means that the creation of menu items and the code that does
the binding are relatively far apart, and hence I have to use
ids (typically static class members).

What I was referring to is that it's a
whole lot easier to bind a menu event without hard-coding an ID for the
menu:

item = file_menu.Append(-1, "&Close","Close this frame")
self.Bind(wx.EVT_MENU, self.OnQuit, item)

That is fine, but causes me problems in two places. One is that I
would like it to be possible for there to be a toolbar button
for every menu item. Therefore I just use the same ID for all menu
items and toolbar items. It is a static class member.

Secondly I have increasing amounts of my user interface as
HTML, with widgets embedded. (See the "With Widgets" bit of
the wx.HtmlWindow in the demo). Again that requires me to have
named constants.

Roger

Paul McNett wrote:

> One thing that baffles me is the following in the docstring:
>
> Bind(self, event, handler, source=None, id=-1, id2=-1) | Bind
> an event to an event handler. | | event One of the EVT_*
> objects that specifies the | type of event to
> bind, | | handler A callable object to be invoked when the
> event | is delivered to self. Pass None to
> disconnect an | event handler. |
>
> Can't an event be bound to multiple handlers, as in:

I don't think so. This is a bit of a pain for me, I generally write
mixin-classes to add certain utilities to my objects, but if the
utility rebinds an event I have to remember to call the appropriate
functions.

I would rather have a chaining mechanism so the events are more like
observables and any interested object can catch the event. This is
easy to do, but still requires book keeping. I would prefer some
mechanism like "Bind" that could handle chaining of handlers.

Here is some test code to show what I mean:

import wx, types

app = wx.PySimpleApp()
frame = wx.Frame(None, -1, "Test")
checkbox = wx.CheckBox(frame, -1, "foo")
def foo(evt):
    wx.MessageDialog(None, "Hello!").ShowModal()
def foo2(evt):
    wx.MessageDialog(None, "Goodbye!").ShowModal()

class EventChain(types.ListType):
    def __call__(self, evt):
        for func in self:
            try:
                func(evt)
            except:
                import traceback
                traceback.print_exc()

checkbox_event = EventChain()
checkbox_event.append(foo)
checkbox_event.append(foo2)

checkbox.Bind(wx.EVT_CHECKBOX, checkbox_event)

frame.Show()
app.MainLoop()

···

> checkbox.Bind(wx.EVT_CHECKBOX, frame.method1)
> checkbox.Bind(wx.EVT_CHECKBOX, frame.method2)
>
> At which point: checkbox.Bind(wx.EVT_CHECKBOX, None)
>
> has exactly what effect? Does it unbind both?
>
> I'd like to see something much more explicit, as in:
> checkbox.UnBind(wx.EVT_CHECKBOX, frame.method1)
>
> ... and that would raise an exception if not previously bound.
>

Roger Binns wrote:
[...]

I then tried to make my code work with both 2.4 and 2.5 (ie whatever
anyone has installed). I had already done the wx namespace transition
months ago.

Unfortunately it required too many code changes like this:

   OLD: mdc.DrawBitmap(bmp, 10, 20)

   NEW: if wx.VERSION>=(2,5):
             mdc.DrawBitmap(bmp, (10, 20))
         else:
             mdc.DrawBitmap(bmp, 10, 20)

I started littering my code like that but eventually gave up since
there were too many spots scattered throughout the code.

If there was a 2.4.2.5 release that added the XY forms, then I could
quite happily make my code work with both releases.

Please try the attached module. Import it into your app and call MakeAllDCsCompatible(). If you are running on a wxPython version less than 2.5 it will add the XY forms of the DC methods to the DC classes, allowing you to use those methods on either version. It can also optionally replace the normal 2.4 methods with the 2.5 version that take point/size parameters.

I've only done minimal testing on this module so far so if there are problems please let me know, or send patches.

I would still need
the if statements for adding space in sizers, but that isn't done
in many places so I can live with it.

2.4 already supported the new version of adding spacers (with a wx.Size or a 2-element sequence instead of separate parameters) so that should already work on both versions.

compatdc.py (5.6 KB)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Paul McNett wrote:
[...]

One thing that baffles me is the following in the docstring:
Bind(self, event, handler, source=None, id=-1, id2=-1)
> Bind an event to an event handler.
>
> event One of the EVT_* objects that specifies the
> type of event to bind,
>
> handler A callable object to be invoked when the event
> is delivered to self. Pass None to disconnect an
> event handler.
>

Can't an event be bound to multiple handlers, as in:

checkbox.Bind(wx.EVT_CHECKBOX, frame.method1)
checkbox.Bind(wx.EVT_CHECKBOX, frame.method2)

At which point:
checkbox.Bind(wx.EVT_CHECKBOX, None)

has exactly what effect? Does it unbind both?

Just the first one it finds, I think.

I'd like to see something much more explicit, as in:
checkbox.UnBind(wx.EVT_CHECKBOX, frame.method1)

... and that would raise an exception if not previously bound.

That would probably be doable.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Brian Kelley wrote:

Paul McNett wrote:

> One thing that baffles me is the following in the docstring:
>
> Bind(self, event, handler, source=None, id=-1, id2=-1) | Bind
> an event to an event handler. | | event One of the EVT_*
> objects that specifies the | type of event to
> bind, | | handler A callable object to be invoked when the
> event | is delivered to self. Pass None to
> disconnect an | event handler. |
>
> Can't an event be bound to multiple handlers, as in:

I don't think so. This is a bit of a pain for me, I generally write
mixin-classes to add certain utilities to my objects, but if the
utility rebinds an event I have to remember to call the appropriate
functions.

I would rather have a chaining mechanism so the events are more like
observables and any interested object can catch the event. This is
easy to do, but still requires book keeping. I would prefer some
mechanism like "Bind" that could handle chaining of handlers.

If your handlers call evt.Skip then the next handler will be called.

Alternativly there is the evtmgr module in the lib, allowing events to be caught via a Publish/Subscribe pattern where there can easily be any number of subscribers to a specific event.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!