OOP Pattern in Python

Simon Wittber (Maptek) Simon.Wittber at perth.maptek.com.au
Wed May 21 23:16:30 EDT 2003


I am writing a server program in Python.

I have a 'server' class designed to maintain a list of connections
(Server). 
I have a 'connection' class designed to maintain a single connection
(Connection).

I sub class Connection to maintain a different type of Connection
(ANSIConnection). I sub class Server to maintain a different set of
connections (ANSIServer).
The original Server class is passed the new Connection class as a
parameter to its __init__ method, so it knows what sort of connections
to maintain. See the below incomplete code snippets for more
information.

Is this a good way of programming? Or am I digging myself into a hole or
doing something silly? If this is actually an acceptable
methodology/pattern, what would you call it?



----Asyn.py-----
class Server (asyncore.dispatcher):
    def __init__ (self, port, ConnectionClass):
        asyncore.dispatcher.__init__ (self)
        self.ConnectionClass = ConnectionClass
    
    def handle_accept (self):
        self.channels.append(self.ConnectionClass(self, self.accept()))
       
    def poll(self):

class Connection(asynchat.async_chat):
	def __init__ (self, server, (conn, addr)):
		asynchat.async_chat.__init__ (self, conn)

EOF

----trs.py-----
Import Asyn

class ANSIConnection(Asyn.Connection):
	def __init__ (self, server, (conn, addr)):
		Asyn.Connection.__init__ (self, server, (conn, addr))
	
	def push(self, text):
		Asyn.Connection.push(self, text)
	
class ANSIServer(Asyn.Server):
    def __init__(self):
        Asyn.Server.__init__(self, 8888, ANSIConnection)

EOF





More information about the Python-list mailing list