Python Forum

Full Version: lcd script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey everyone,

I'm working on a function that helps phrase the values for a 16x2 char lcd. every 5 seconds the screen changes to a different 'page'.
at the moment it does what I need, but I feel there is a cleaner way to do what I want


Here is the script:

def __value(self, data, Session, screenNum=0, line=1):
        now = display.datetime.now()
        r = None

#phrase line 1                
        if line == 1:
#phrase line 1 screen 2
            if screenNum == 1:
                r = "\x07\x03"+self.__PiTemp()
#phrase line 1 screen 1, or default if none valid screen number is given
            if r == None: # screen 0 / default
                r =  "Time: %s" %now.strftime("%H:%M:%S") #screen 0
            screenMax = 1

        else: #line 2
#phrase line 2 screen 2
            if screenNum == 1:
                #r =  self.__co2Levels(resp['/api/equipment/9'])
                r =  self.__co2Levels('22')
#phrase line 2 screen 3
            if screenNum == 2:
                r =  self.__equipmentStatus1()
#phrase line 2 screen 4
            if screenNum == 3:
                r =  self.__equipmentStatus2()
#phrase line 2 screen 1, or default if none valid screen number is given
            if r == None: # screen 0 / default
                r =  "H20\x03"+self.__tankTemp(data)
            screenMax = 3

        return [r, (screenMax < screenNum)]
above you can see it takes the line and page number (represented by screenNum, line respectively), and returns the phrased script along with a boolean value showing if the page number exists. Which is great.

However, if I want to add more pages to a screen, I have to add another 'if' statement, and readjust the screenMax variables.

I would like to find a more elegant way, that is able to calculate the "screenMax" on its own, and doesn't need to add an additional 'if' statement to the code.



Anyone have any ideas?
This is what I came up with.

Hopefully someone can use this to help themselves out

I use replace and getattr. I put the name of the functions I need into square brackets

    def __value(self, Session, screenNum=0, line=1):

        if line == 1:
            lineVal = ("Time: [time]","\x07\x03[PiTemp]")
        else:
            lineVal = ("H20\x03[tankTemp]", "Co2: [co2Levels]","[equipmentStatus1]","[equipmentStatus2]")

        funcs=('[time]','[PiTemp]','[tankTemp]','[co2Levels]','[equipmentStatus1]','[equipmentStatus2]')
        try:
            r = lineVal[screenNum]
        except Exception:
            r = lineVal[0]

        for i in funcs:
            if i in r:
                func = getattr(self, i[1:-1])
                r = r.replace(i, func(self))

        return [r, ((len(lineVal)-1) < screenNum)]
A different approach (but I may like JarredAwsome's better, haven't decided yet)
Everything after # ........... is for testing only and can be removed after trying as standalone

please note, I also removed name mangling (underscores), add back if needed

class Dummy:
    def dvalue(self, data, Session, screenNum=0, line=1):
        rscreen = {
            1: self.rfunc1,
            2: self.rfunc2,
            3: self.rfunc3,
            None: self.rfuncnone
        }

        # following was commented out as display not defined
        # now = display.datetime.now()
        r = None
        r = rscreen[screenNum](data, Session, screenNum, line)

    def rfunc1(self, data, Session, screenNum, line):
        if line == 1:
            return f"\x07\x03{self.PiTemp()}"
        else:
            return self.co2Levels('22')

    def rfunc2(self, data, Session, screenNum, line):
        return self.equipmentStatus1()
    
    def rfunc3(self, data, Session, screenNum, line):
        return self.equipmentStatus2()
    
    def rfuncnone(self, data, Session, screenNum, line):
        return "H20\x03"+self.tankTemp(data)

# ...........
# Dummy sub functions:
# for testing only remove after assuring all is working well:
    def PiTemp(self):
        return "SomeValue"

    def co2Levels(self, value):
        print(f"executing co2Levels value: {value}")
    
    def equipmentStatus1(self):
        print(f"executing equipmentStatus1")
    
    def equipmentStatus2(self):
        print(f"executing equipmentStatus2")

    def tankTemp(self, data):
        print(f"executing tankTemp data: {data}")

def testit():
    dum = Dummy()

    data = [1, 2, 3, 4]
    Session = None # this was not used

    for line in range(2):
        for screenNum in range(1, 4):
            print(f"\nTest for:\n  data: {data}\n   Session: {Session}" \
                f"\n   screenNum: {screenNum}\n   line: {line}")
            dum.dvalue(data, Session, screenNum, line)


if __name__ == '__main__':
    testit()