472,958 Members | 1,804 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,958 software developers and data experts.

Passing messages between classes

bartonc
6,596 Expert 4TB
I'm working on a scheme which reduces the interface between classed to a single method and a list messages that the co-operative class knows. I invite all comments and/or critiques/improvements: We could even turn this into an article!
Expand|Select|Wrap|Line Numbers
  1. class AnyClass(object):
  2.     """Demonstrate massage passing:
  3.         This is the subordinate class."""
  4.     def Funct1(self, arg1):
  5.         print "AnyClass.Funct1 got", arg1
  6.         self.Send("MESSAGE3", arg1, string1='hello', string2='world')
  7.  
  8.     def Funct2(self, arg2, arg3):
  9.         print arg2, arg3
  10.  
  11.     def Funct3(self, count, **kwargs):
  12.         """Must document the names of the kwargs here:
  13.            Funct3(count, string1, string2)"""
  14.         print count, kwargs['string1'], kwargs['string2']
  15.  
  16.  
  17.     # Of cource, the sender and the reciver would typically have different massages
  18.     DispatchDict = {'MESSAGE1':Funct1, 'MESSAGE2':Funct2, 'MESSAGE3':Funct3}
  19.  
  20.     def Receive(self, *args, **kwargs):
  21.         self.DispatchDict[args[0]](self, *args[1:], **kwargs)
  22.  
  23.     def Connect(self, callback):
  24.         self.Send = callback
  25.         return self.Receive
  26.  
  27.  
  28. class AnyOtherClass(object):
  29.     """Demonstrate massage passing:
  30.         This is the superior class."""
  31.     def __init__(self):
  32.         self.Worker = AnyClass()
  33.         self.Send = self.Worker.Connect(self.Receive)
  34.         self.Send('MESSAGE1', 5)
  35.  
  36.     def Funct1(self, arg1):
  37.         print arg1
  38.  
  39.     def Funct2(self, arg2, arg3):
  40.         print arg2, arg3
  41.  
  42.     def Funct3(self, count, **kwargs):
  43.         """Must document the names of the kwargs here:
  44.            Funct3(count, string1, string2)"""
  45.         print count, kwargs['string1'], kwargs['string2']
  46.  
  47.  
  48.     # Of cource, the sender and the reciver would typically have different massages
  49.     DispatchDict = {'MESSAGE1':Funct1, 'MESSAGE2':Funct2, 'MESSAGE3':Funct3}
  50.  
  51.     def Receive(self, *args, **kwargs):
  52.         self.DispatchDict[args[0]](self, *args[1:], **kwargs)
  53.  
  54. test = AnyOtherClass()
  55.  
Nov 3 '07 #1
0 1391

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

Similar topics

5
by: cold | last post by:
I'm trying to write an helper class that allows me to serialize objects to a socket (or a NetworkStream). I know there are plenty of .NET classes that help me to serialize object to an array of...
2
by: Gvs | last post by:
Hi, I'm currently trying to pass messages into a queue. This all works fine, however, i'm trying to my program to stop sending messages to the queue when it reaches an upper threshold. At...
3
by: Schorschi | last post by:
Can someone please point out why I am getting an 87 error? I am sure it is obvious, but I am new to C# and seem to be having a lot of stress understanding managed versus unmanaged code when API...
5
by: Adam | last post by:
Hi, How do i listen for windows messages in c# on compact.net? I have a window containing an instantiation of the HTML viewer control, which is a child of the main form. As the compact...
11
by: Arsen Vladimirskiy | last post by:
Hello, If I have a few simple classes to represent Entities such as Customers and Orders. What is the proper way to pass information to the Data Access Layer? 1) Pass the actual ENTITY to...
3
by: Curious | last post by:
Hi, I need some technique to communicate by passing messages with different threads. For my problem shared memory is not applicable. Can someone give me some help of what there is available...
10
by: Stan | last post by:
There are two ways to pass structured data to a web service: xml === <Order OrderId="123" OrderAmount="234" /> or class =====
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
0
by: AAaron123 | last post by:
On a Form I have a RichTextBox. I do the call for EM_SETEVENTMASK with ENM_REQUESTRESIZE The user control's WndProc writes to the consol to keep track of any EN_REQUESTRESIZE messages. As...
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...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
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...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.