472,950 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,950 software developers and data experts.

Redirecting class method calls to owned objects

bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. #-----------------------------------------------------------------------------
  2. # Name:        ObjectRedirect.py
  3. # Purpose:     Demonstrate class methode redirection with new-style classes
  4. #
  5. # Author:      Barton
  6. #
  7. # Created:     2007/09/20
  8. # RCS-ID:      $Id: ObjectRedirect.py $
  9. # Copyright:   (c) 2007
  10. # Licence:     free, free, free
  11. #-----------------------------------------------------------------------------
  12.  
  13.  
  14. """In GUI programming, it is almost always the case that a simple widget needs
  15.    a parent window to be displayed in. No problem... Create the Frame() with a
  16.    sizer (usually) and add (say) a multi-line text widget. And there you have your
  17.    text display. Now, in the main application, create an instance of your new Frame
  18.    and call myFrame.Show() to get it on the screen. But, what about writing text to
  19.    that widget that is owned by the Frame??? You could:
  20.    self.myFrame.textObj.WriteText("the text to write")  # but that many dots can be worrisome
  21.    or
  22.    textObj = self.myFrame.GetTextObj()     # but there's that extra line
  23.    textObj.WriteText("the text to write")  # and you need to define GetTextObj()
  24.  
  25.    I've been toying with a trick that seems to work quite well on wxPython widgets that all
  26.    decend from a python object (new-style class objects). In the following snippet, imagine that these
  27.    objects are all several layers deep in the object hierarchy and decent from the same roots"""
  28.  
  29.  
  30. class SimulatedTextWidget(object):
  31.     """bla-bla_bla"""
  32.     def __init__(self, parent, *args, **kwargs):
  33.         self.parent = parent
  34.  
  35.     def WriteText(self, text):
  36.         print text
  37.  
  38.  
  39. class SimulatedFrame(object):
  40.     """bla-bla_bla"""
  41.     def __init__(self, parent, *args, **kwargs):
  42.         self.parent = parent
  43.         self.simTextObj = SimulatedTextWidget(self)
  44.  
  45.     def __getattribute__(self, name):
  46.         """Redirect method calls: Try this object first. If it's not a method/attribute
  47.            of this object, try the redirected object."""
  48.         try:
  49.             return object.__getattribute__(self, name)
  50.         except AttributeError:
  51.             return object.__getattribute__(self.simTextObj, name)
  52.  
  53.  
  54.  
  55. if __name__ == "__main__":
  56.     frame = SimulatedFrame(None)
  57.     frame.WriteText("Hello world!")
Sep 21 '07 #1
0 1014

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Joseph Turian | last post by:
Okay. So I have a class A, which contains a vector of B. Any B item is owned by a single, unique A. Indeed, when we create A, A creates its B items. B would like to know which A it is...
14
by: Derek Basch | last post by:
This one has always bugged me. Is it better to just slap a "self" in front of any variable that will be used by more than one class method or should I pass around variable between the methods? ...
5
by: Earl | last post by:
I need to call a method on an owned child form, and am wondering if the best way of doing this is to capture the Closing event of the form that passes control back to the form where I have the...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.