2

I'm processing data from a serial port in python. The first byte indicates the start of a message and then the second byte indicates what type of message it is. Depending on that second byte we read in the message differently (to account for different types of messages, some are only data others are string and so on).

I now had the following structure. I have a general Message class that contains basic functions for every type of message and then derived classes that represent the different types of Messages (for example DataMessage or StringMessage). These have there own specific read and print function.

In my read_value_from_serial I read in all the byte. Right now I use the following code (which is bad) to determine if a message will be a DataMessage or a StringMessage (there are around 6 different type of messages but I simplify a bit).

msg_type = serial_port.read(size=1).encode("hex").upper()
msg_string = StringMessage()
msg_data = StringData()

processread = {"01" : msg_string.read, "02" : msg_data.read}
result = processread[msg_type]()

Now I want to simplify/improve this type of code. I've read about killing the switch but I don't like it that I have to create objects that I won't use in the end. Any suggestions for improving this specific problem?

Thanks

DanFritz
  • 401
  • 8
  • 20
  • Could you provide a reference or link to the idea of "killing the switch"? A range of alternatives to switch statements that might be useful to you can be found in [Replacements for switch statement in python?](http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python?rq=1). – Simon Sep 10 '13 at 20:43
  • Hello, I actually used that link to implement my code. The problem I'm having is that I don't like the solution and there should be another more Python way of handeling my problem. I think my title was a bit misleading because I'm actually looking for alternatives of my current solution that are less ugly and don't create objects for nothing. – DanFritz Sep 10 '13 at 21:02

1 Answers1

2

This is very close to what you have and I see nothing wrong with it.

class Message(object):
    def print(self):
        pass

class StringMessage(Message):
    def __init__(self, port):
        self.message = 'get a string from port'

def MessageFactory(port):
    readers = {'01': StringMessage, … }
    msg_type = serial_port.read(size=1).encode("hex").upper()
    return readers[msg_type](port)

You say "I don't like it that I have to create objects that I won't use in the end". How is it that you aren't using the objects? If I have a StringMessage msg, then

msg.print()

is using an object exactly how it is supposed to be used. Did it bother you that your one instance of msg_string only existed to call msg_string.read()? My example code makes a new Message instance for every message read; that's what objects are for. That's actually how Object Oriented Programming works.

msw
  • 42,753
  • 9
  • 87
  • 112
  • I didn't like the useless creation of msg_data and msg_string when for example the msg type is msg_object. – DanFritz Sep 10 '13 at 21:30
  • You'll need to give more detail about your type structure for any better answer to be given. – msw Sep 10 '13 at 21:41