Design question.... parent/child class

Bengt Richter bokr at oz.net
Tue Sep 3 13:49:59 EDT 2002


On Tue, 3 Sep 2002 14:56:21 +0200, "Gumuz" <gumuz at looze.net> wrote:

>hi all,
>
>i have a SessionManager class, which contains a list of Session
>objects(instantiated from a Session class). I have a design problem.
>
>Each session object has a small 'queue' of messages for the specific
>session. How can a session send a message to another session without having
>some kind of parent-reference to the SessionManager object?
>
>i am a bit puzzled, or is this parent-reference not such a bad idea after
>all?
>

Seems like the Session class is in a good position to keep track of the
list of session objects instantiated from it. I.e., the __init__ method
could add sessions to a directory or list maintained as a class variable
(a variable associated with the class, not each instance, but accessible
from all instances (and via the class name unless you prevent it)).

If you gave names or id numbers to sessions when they were created, you could
then access one session from another by name. E.g., (I'm not recommending this
verbatim. It's just a quick example to give ideas. Maybe you don't need
a separate SessionManager class, depending on what you really want to do
and how you want to factor things.

 >>> class Session:
 ...     sessions = {}
 ...     def __init__(self, name, *etc):
 ...         if name in self.sessions:
 ...             raise KeyError,'Session name "%s" already used.' % name
 ...         self.name = name
 ...         self.sessions[name] = self
 ...         self.etc = etc
 ...         self.msg_queue=[]
 ...     def accept_msg(self, msg):
 ...         print 'I am "%s," and I accepted message "%s."' % (self.name, msg)
 ...     def send_msg(self, dest, msg):
 ...         print 'I am "%s," sending "%s" message "%s"' % (self.name, dest, msg)
 ...         self.sessions[dest].accept_msg(msg)
 ...
 >>> s1 = Session('one', 1,2,'stuff')
 >>> s2 = Session('two')
 >>> s2.send_msg('one','Hi from instance s2')
 I am "two," sending "one" message "Hi from instance s2"
 I am "one," and I accepted message "Hi from instance s2."
 >>> s3 = Session('two')
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 5, in __init__
 KeyError: Session name "two" already used.

Note that the s1 and s2 variables weren't really necessary to access the sessions,
since they were recorded by name in the session directory:

 >>> Session.sessions['one'].send_msg('two','Hi from session "one"')
 I am "one," sending "two" message "Hi from session "one""
 I am "two," and I accepted message "Hi from session "one"."

 >>> Session('three')
 <__main__.Session instance at 0x007D5B70>
 >>> Session.sessions['two'].send_msg('three','Hi from 2')
 I am "two," sending "three" message "Hi from 2"
 I am "three," and I accepted message "Hi from 2."

You could define class methods to implement various session management
functions, so you could write e.g. Session.status() and get back or print
a report with status for each session, etc., etc.

The instance data is also available, of course:

 >>> vars(s1)
 {'etc': (1, 2, 'stuff'), 'name': 'one', 'msg_queue': []}
 >>> vars(s2)
 {'etc': (), 'name': 'two', 'msg_queue': []}
 >>> vars(Session.sessions['three'])
 {'etc': (), 'name': 'three', 'msg_queue': []}

and thus as attributes, e.g.,

 >>> s1.etc
 (1, 2, 'stuff')
 >>> s2.etc
 ()
 >>> Session.sessions['one'].etc
 (1, 2, 'stuff')
 >>> Session.sessions['two'].etc
 ()

Regards,
Bengt Richter



More information about the Python-list mailing list