473,781 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Redirecting class method calls to owned objects

bartonc
6,596 Recognized Expert Expert
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 1041

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

Similar topics

5
1487
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 contained in, without having to be perpetually told it by being passed A when B methods are called. How can B store its A (or some handle to A) if A stores B?
14
9888
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? Flamewar........NOW! jk, I really do want other opinions. Thanks, Derek
5
2245
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 method. The structure is like so: frmMain (MDI, runs on app start) calls frmB (MDI child), which in turn calls frmC (MDI child), which in turn calls frmD (MDI child). frmMain and frmB remain open while frmC and frmD are shown.
11
6259
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 the app is concerned). The problem is, If someone makes a typo, they may get an unexpected error due accidentally calling the original attribute instead of the wrapped version. Does anyone have a simple solution for this?
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10143
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.