Catching wx events
Hello all, I am new to this group (and new to Python) and was hoping someone would be able to help me with something, it is not so much a problem it is more of a general interest query about something i have a solution too but am not sure it is the correct one. I have a class that contains a string ID and a name, and a list containing a few objects of this type, i need to loop through this list and create a button for each object (with the name as the label) i have done this with the following code - for chan in self._channellist: channelbutton = wx.Button(self, id=-1, label=chan.getName()) channelbutton.Bind(wx.EVT_BUTTON,self._channelChanged) My question is this - in the _channelChanged method, how do i know which button has been pressed when i enter the channel changed method, and so how do i retrieve the appropriate object depending on which button has been pressed? I have potentially solved this problem using the UserData property in a sizer, i have added all the buttons to a sizer for display purposes and so SizerItem objects have been created, i can then set the original object to the UserData object by putting the following line of code in the loop sizeritem = self.topsizer.Add(channelbutton,0,wx.ALIGN_RIGHT, userData=chan) This way i can retrieve the item in the _channelChanged method with the following - def _channelChanged(self, event): eventobj = event.GetEventObject() chan = self.topsizer.GetItem(eventobj).GetUserData() This works fine but by looking at the API it would appear the UserData property is not really designed for this use ("userData - Allows an extra object to be attached to the sizer item, for use in derived classes when sizing information is more complex than the proportion and flag will allow for"). Another option would be to derive my own Button class and include the object in there. Any advice on the best way to solve this problem would be appreciated. Many thanks Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: Catching wx events
Chris Mellon wrote: > On 18 Jan 2007 06:12:17 -0800, Cruelemort <[EMAIL PROTECTED]> wrote: > > Hello all, > > > > I am new to this group (and new to Python) and was hoping someone would > > be able to help me with something, it is not so much a problem it is > > more of a general interest query about something i have a solution too > > but am not sure it is the correct one. > > > > I have a class that contains a string ID and a name, and a list > > containing a few objects of this type, i need to loop through this list > > and create a button for each object (with the name as the label) i have > > done this with the following code - > > > > for chan in self._channellist: > > channelbutton = wx.Button(self, id=-1, > > label=chan.getName()) > > > > channelbutton.Bind(wx.EVT_BUTTON,self._channelChanged) > > > > My question is this - in the _channelChanged method, how do i know > > which button has been pressed when i enter the channel changed method, > > and so how do i retrieve the appropriate object depending on which > > button has been pressed? > > > > I have potentially solved this problem using the UserData property in a > > sizer, i have added all the buttons to a sizer for display purposes and > > so SizerItem objects have been created, i can then set the original > > object to the UserData object by putting the following line of code in > > the loop > > > > sizeritem = > > self.topsizer.Add(channelbutton,0,wx.ALIGN_RIGHT, userData=chan) > > > > This way i can retrieve the item in the _channelChanged method with the > > following - > > > > def _channelChanged(self, event): > > eventobj = event.GetEventObject() > > chan = self.topsizer.GetItem(eventobj).GetUserData() > > > > > > This works fine but by looking at the API it would appear the UserData > > property is not really designed for this use ("userData - Allows an > > extra object to be attached to the sizer item, for use in derived > > classes when sizing information is more complex than the proportion and > > flag will allow for"). > > > > Another option would be to derive my own Button class and include the > > object in there. > > > > Any advice on the best way to solve this problem would be appreciated. > > > > Exactly how I would do it depends on the rest of the application. I > would probably derive my own button - never be afraid to subclass. > > You could also generate the buttons IDs up front, and maintain a > mapping between the IDs and the channels, like so: > > self.mapper = {} > for channel in self.channels: > id = wx.NewId() > self.mapper[id] = channel > channelbutton = wx.Button(self, id=id, label=channel.getName()) > > > def channelChanged(self, event): > channel = self.mapper[event.Id] > > > You could also use closures (lambdas or via a factory function) to > bind the channel at the time you create the button: > > for channel in self.channels: > channelbutton = wx.Button(self, label=channel.getName()) > self.Bind(wx.EVT_BUTTON, lambda event: > self.channelChanged(channel), source=channelbutton) > > def channelChanged(self, channel): > print "Channel changed to ", channel.getName() Two good ideas, i used the mapping system, works and seems like a slightly more elegant way of doing things. Many thanks! Ian -- http://mail.python.org/mailman/listinfo/python-list
LDAP/LDIF Parsing
All, I am hoping someone would be able to help me with a problem. I have an LDAP server running on a linux box, this LDAP server contains a telephone list in various groupings, the ldif file of which is - dn: dc=example,dc=com objectClass: top objectClass: dcObject objectClass: organization dc: example o: Example Organisation dn: ou=groupa,dc=example,dc=com ou: groupa objectClass: top objectClass: organizationalUnit description: Group A dn: cn=johnsmith,ou=groupa,dc=example,dc=com cn: johnsmith objectClass: top objectClass: person sn: Smith telephoneNumber: 112 dn: cn=davesteel,ou=groupa,dc=example,dc=com cn: davesteel objectClass: top objectClass: person sn: Steel telephoneNumber: 113 dn: ou=groupb,dc=example,dc=com ou: groupb objectClass: top objectClass: organizationalUnit description: Group B dn: cn=williamdavis,ou=groupb,dc=example,dc=com cn: williamdavis objectClass: top objectClass: person sn: Davis telephoneNumber: 122 dn: cn=jamesjarvis,ou=groupb,dc=example,dc=com cn: jamesjarvis objectClass: top objectClass: person sn: Jarvis telephoneNumber: 123 I am creating a python client program that will display the telephone list in the same directory structure as is on the LDAP server (i.e. it starts with buttons of all the groups, when you click on a group it comes up with buttons of all the numbers or groups available, and you can continually drill down). I was wondering the best way to do this? I have installed and used the python-ldap libraries and these allow me to access and search the server, but the searches always return a horrible nesting of lists, tuples and dictionaries, below is an example of returning just one record - ('dc=example,dc=com', {'objectClass': ['top', 'dcObject', 'organization'], 'dc': ['example'], 'o': ['Example Organisation']}) Basically i think i need to parse the search results to create objects and build the python buttons around this, but i was hoping someone would be able to point me in the correct direction of how to do this? Is there a parser available? (there is an ldif library available but it is not obvious how this works, i cannot see much documentation, and it seems to be deprecated...). Many thanks. Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: LDAP/LDIF Parsing
On Feb 1, 11:08 pm, "aspineux" <[EMAIL PROTECTED]> wrote: > The tree hierarchy is defined by the DN of each object, the types of > the object is specified by its objectClass. > Just collect all items (or do it dynamically by tunning the scope and > the base of your search request) > > On 1 fév, 18:22, "Cruelemort" <[EMAIL PROTECTED]> wrote: > > > > > All, > > > I am hoping someone would be able to help me with a problem. I have an > > LDAP server running on a linux box, this LDAP server contains a > > telephone list in various groupings, the ldif file of which is - > > > dn: dc=example,dc=com > > objectClass: top > > objectClass: dcObject > > objectClass: organization > > dc: example > > o: Example Organisation > > > dn: ou=groupa,dc=example,dc=com > > ou: groupa > > objectClass: top > > objectClass: organizationalUnit > > description: Group A > > > dn: cn=johnsmith,ou=groupa,dc=example,dc=com > > cn: johnsmith > > objectClass: top > > objectClass: person > > sn: Smith > > telephoneNumber: 112 > > > dn: cn=davesteel,ou=groupa,dc=example,dc=com > > cn: davesteel > > objectClass: top > > objectClass: person > > sn: Steel > > telephoneNumber: 113 > > > dn: ou=groupb,dc=example,dc=com > > ou: groupb > > objectClass: top > > objectClass: organizationalUnit > > description: Group B > > > dn: cn=williamdavis,ou=groupb,dc=example,dc=com > > cn: williamdavis > > objectClass: top > > objectClass: person > > sn: Davis > > telephoneNumber: 122 > > > dn: cn=jamesjarvis,ou=groupb,dc=example,dc=com > > cn: jamesjarvis > > objectClass: top > > objectClass: person > > sn: Jarvis > > telephoneNumber: 123 > > > I am creating a python client program that will display the telephone > > list in the same directory structure as is on the LDAP server (i.e. it > > starts with buttons of all the groups, when you click on a group it > > comes up with buttons of all the numbers or groups available, and you > > can continually drill down). > > > I was wondering the best way to do this? I have installed and used the > > python-ldap libraries and these allow me to access and search the > > server, but the searches always return a horrible nesting of lists, > > tuples and dictionaries, below is an example of returning just one > > record - > > > ('dc=example,dc=com', {'objectClass': ['top', 'dcObject', > > 'organization'], 'dc': ['example'], 'o': ['Example Organisation']}) > > > Basically i think i need to parse the search results to create objects > > and build the python buttons around this, but i was hoping someone > > would be able to point me in the correct direction of how to do this? > > Is there a parser available? (there is an ldif library available but > > it is not obvious how this works, i cannot see much documentation, and > > it seems to be deprecated...). > > > Many thanks. > > > Ian- Hide quoted text - > > - Show quoted text - Thanks for the replies all - it was a higher level wrapper like Bruno mentioned that i was looking for (with objects and attributes based on each objectClass), but the code posted above will work fine. Many thanks all. Ian -- http://mail.python.org/mailman/listinfo/python-list