Generic communications module with a little help from classes...

dberlin at gmail.com dberlin at gmail.com
Thu Jun 30 14:37:18 EDT 2005


Hi,
What I want to do is create a module that offers a generic set of
functions (send, recieve, etc...) and reffers the request to the
correct module (rs-232, tcp/ip, etc..).
I want all this to be unseen by the script that calls this module.
I want the script to specify the communication type, and then be able
to use the functions. In the future, when the whole script will work on
a different type of communication all that will be changed is the type
of communication property.
How do I do this?

I thought of a few possibilities:
1. all communication modules will have the same function names and same
number of params (problematic), and then it will look like this:

class module1:
   def send(self):
      # send one way

class module2:
   def send(self):
      # send the other
if X:
   selected = module1
else:
   selected = module2

class main(selected):
   pass

m = main()
m.send()

2. the other option is creating a "wrapper" class for each module. this
class takes the generic params of the function "send" and organizes the
params and name so it will be compatible with the comm module:

class a:
   def send(self, what):
      module1.sendCMD(what)

class b:
   def send(self, what):
      module2.SEND(what)

if X:
   selected = a
else:
   selected = b

class main(selected):
   pass

m = main()
m.send()

#############
which way is better? any better ideas?

thanks.




More information about the Python-list mailing list