Skip to main content leehalls.net

Shortening the Code

The module i wrote for decoding the sensor data stream bugged me it was too repetitive with multiple if statements ie;

python code snippet start

if "ABT" in llapMsg:
    # front room temp
    try:
        aABTEMP = float(llapMsg[7:12])
        print("AB temp level = %.2f - detected at %s " %
              (aABTEMP, time.strftime("%H:%M:%S")))
    except:
        print(
            "Cannot decode - msg detected: %s - detected at %s " %
            (llapMsg, time.strftime("%H:%M:%S")))
if "ACT" in llapMsg:
    # external temp
    try:
        aACTEMP = float(llapMsg[7:12])
        print("AC temp level = %.2f - detected at %s " %
              (aACTEMP, time.strftime("%H:%M:%S")))
    except:
        print(
            "Cannot decode - msg detected: %s - detected at %s " %
            (llapMsg, time.strftime("%H:%M:%S")))
if "ADT" in llapMsg:
    # kitchen temp
    try:
        aADTEMP = float(llapMsg[7:12])
        print("AD temp level = %.2f - detected at %s " %

python code snippet end

and so on

So over the last few days i decided to try again and it looks reasonable in tests i just need to try it out in the main program. Instead of multiple if statements i now use a simple dictionary and pythons text handling to go from a module with 140 lines to one with 8 without additional comments.

A dictionary is a collection of many values a bit like a list however it can hold different data types. So you have an index and the value although in python terms the index is a key and combined you have a key-value-pair.

I find it’s often useful to view several explanations of a subject for dictionaries i suggest you not only visit the link above but try here; Guru99 this one gives a few more examples such as comparing dictionaries, sorting and the examples are in both python 2 & 3.

python code snippet start

sensor_dictionary = {'msg': "this is the value", 'key1': 2}

python code snippet end

you can see from the simple example above we have two pairs;

key value
msg this is the value
key1 2

Using this concept i no longer needed to maintain a list eg sensor[0] which unless i have a look up table makes the code harder to read each time i go back to it as i have to try and remember which sensor is 1,2,3 etc but with the dictionary i can simply use the key as the identifier and when a new value comes along update the correct key.

python code snippet start

def decdict(incoming):
    # sample: aAETMPA12.45
    if incoming.get('msg') != '':
        # get sensor id from llapmsg
        id = incoming.get("msg")
        id = id[1:3]
        # get value from llapmsg
        val = incoming.get("msg")
        val = val[7:12]
        # assign value to correct sensor in dictionary
        incoming[id] = val

    return(incoming)

python code snippet end

Thats it, 8 lines of code replace multiple if statements and handle the temperature decode, now i need to deal with the battery messages and also correct an annoying new problem - the canvas gauges display the correct value in numeric terms but the angular needle can be wildly out ie value is 10 needle shows 2?? go figure?? I’ve not changed anything in the display code.