Home > Software design >  Python, how to manage multiple data in list or dictionary with calculations and FIFO functionality
Python, how to manage multiple data in list or dictionary with calculations and FIFO functionality

Time:01-02

I need to manage multiple data organized like as following:

SLOT1

timestamp value1 value2 value3 value4
xxxxxxxxx aaaaaa bbbbbb cccccc dddddd
......... ...... ...... ...... ......
......... ...... ...... ...... ......
......... ...... ...... ...... ......
......... ...... ...... ...... ......
xxxxxxxxx qqqqqq rrrrrr ssssss tttttt

SLOT2

timestamp value1 value2 value3 value4
xxxxxxxxx aaaaaa bbbbbb cccccc dddddd
......... ...... ...... ...... ......
......... ...... ...... ...... ......
......... ...... ...... ...... ......
......... ...... ...... ...... ......
xxxxxxxxx qqqqqq rrrrrr ssssss tttttt

......

SLOT 'n'

timestamp value1 value2 value3 value4
xxxxxxxxx aaaaaa bbbbbb cccccc dddddd
......... ...... ...... ...... ......
......... ...... ...... ...... ......
......... ...... ...... ...... ......
......... ...... ...... ...... ......
xxxxxxxxx qqqqqq rrrrrr ssssss tttttt

The data structure should not be a Pandas Dataframe but preferibly a native structure without the use of external libraries.

Each single Slot should have a fixed length and new coming data (let say a new row) should be treated with the FIFO logic. so the last coming data sould be appended at the end and the first one should be extracted mainteining fixed the total length of the data structure.

Last but not the least, I need to add an extra column to each slot to perform a kind of computation like

"columnadded" = ("value1" "value2") / ("value3" "value4")

thank you for all will help me on this

CodePudding user response:

except for the last part of the question, you can use the following class to create a slot object in which you can store data the way you described. You can create as many slots as you want and store them in an array to achieve what you want.

class Slot:
    maxrowlen = 5 #the maximum number of row you want in one slot
    coredict = {"TimeStamp":[],"Value1":[],"Value2":[],"Value3":[],"Value4":[]}

    def addrow(self,timestamp, value1, value2, value3, value4):
        if len(self.coredict["TimeStamp"])<self.maxrowlen:
            self.coredict["TimeStamp"].append(timestamp)
            self.coredict["Value1"].append(value1)
            self.coredict["Value2"].append(value2)
            self.coredict["Value3"].append(value3)
            self.coredict["Value4"].append(value4)
            print("Data successfully added to slot")
        else:
            for i in range(len(self.coredict["TimeStamp"])-1):
                self.coredict["TimeStamp"][i]=self.coredict["TimeStamp"][i 1]
                self.coredict["Value1"][i]=self.coredict["Value1"][i 1]
                self.coredict["Value2"][i]=self.coredict["Value2"][i 1]
                self.coredict["Value3"][i]=self.coredict["Value3"][i 1]
                self.coredict["Value4"][i]=self.coredict["Value4"][i 1]
            
            index = self.maxrowlen-1
            self.coredict["TimeStamp"][index] = timestamp
            self.coredict["Value1"][index]=value1
            self.coredict["Value2"][index]=value2
            self.coredict["Value3"][index]=value3
            self.coredict["Value4"][index]=value4

    def getfirst(self):
        index = 0
        for value in self.coredict.values():
            print(" | "   str(value[index])   " | ", end="")
        print("")
    
    def printall(self):
        for i in range(len(self.coredict["TimeStamp"])):
            index = i
            for value in self.coredict.values():
                print(" | "   str(value[index])   " | ", end="")
            print("")

there are three methods in Slot class:
1. addrow() - to add a row at the end of the slot (according to FIFO)
2. getfirst() - to print the data of a the first row added (according to FIFO)
3. printall() - to print all the data in the slot


This class can store the data in the way you need it.

  • Related