Python Forum

Full Version: Python, how to manage multiple data in list or dictionary with calculations and FIFO
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
what have you tried?
For the rotating FIFO, you could use a collections.deque instance. You could also very easily implement a fixed length rotating structure with a list and a pointer, for example
class Rotating:
    def __init__(self, items):
        self.data = list(items)
        self.head = 0
        
    def __iter__(self):
        for i in range(len(self)):
            yield self[i]
    
    def __getitem__(self, i):
        return self.data[(i + self.head) % len(self)]
    
    def __len__(self):
        return len(self.data)
    
    def append(self, item):
        self.data[self.head] = item
        self.head = (self.head + 1) % len(self)
        
    def __str__(self):
        return 'Rotating({})'.format(
            self.data[self.head:] + self.data[:self.head])
    
R = Rotating('abcdef')
print(R)
R.append('g')
print(R)
R.append('h')
print(R)
print(R[0], len(R), R[5])
print(R.data)
Output:
Rotating(['a', 'b', 'c', 'd', 'e', 'f']) Rotating(['b', 'c', 'd', 'e', 'f', 'g']) Rotating(['c', 'd', 'e', 'f', 'g', 'h']) c 6 h ['g', 'h', 'c', 'd', 'e', 'f']
(Dec-29-2021, 12:36 PM)Larz60+ Wrote: [ -> ]what have you tried?
nothing for now, because my experience in this new field for me...
(Dec-29-2021, 01:11 PM)Gribouillis Wrote: [ -> ]For the rotating FIFO, you could use a collections.deque instance. You could also very easily implement a fixed length rotating structure with a list and a pointer, for example
class Rotating:
    def __init__(self, items):
        self.data = list(items)
        self.head = 0
        
    def __iter__(self):
        for i in range(len(self)):
            yield self[i]
    
    def __getitem__(self, i):
        return self.data[(i + self.head) % len(self)]
    
    def __len__(self):
        return len(self.data)
    
    def append(self, item):
        self.data[self.head] = item
        self.head = (self.head + 1) % len(self)
        
    def __str__(self):
        return 'Rotating({})'.format(
            self.data[self.head:] + self.data[:self.head])
    
R = Rotating('abcdef')
print(R)
R.append('g')
print(R)
R.append('h')
print(R)
print(R[0], len(R), R[5])
print(R.data)
Output:
Rotating(['a', 'b', 'c', 'd', 'e', 'f']) Rotating(['b', 'c', 'd', 'e', 'f', 'g']) Rotating(['c', 'd', 'e', 'f', 'g', 'h']) c 6 h ['g', 'h', 'c', 'd', 'e', 'f']

That's amazing but it's intended for one column only, I need more columns and more slots (that basically are not specified in number at the start of the program).
Use rows as items, for example
r = Rotating((('xxx','aaa','bbb','ccc','ddd') for i in range(10)))
(Dec-30-2021, 02:47 PM)Gribouillis Wrote: [ -> ]Use rows as items, for example
r = Rotating((('xxx','aaa','bbb','ccc','ddd') for i in range(10)))

it's not so clear for me.
How can I access to (for example) SLOTn.ROW.COL ?
It seems to me like a 3D array, isn't it?
How to implement this?
Just make a list of Rotating instances
slots = []
for i in range(5):
     slots.append(Rotating((['xxx','aaa','bbb','ccc','ddd'] for i in range(10))))

value = slots[2][3][1]
But we'd like to see your Python code now. As @Larz60+ wrote above, this becomes interesting only if you try something by yourself.
(Dec-30-2021, 09:03 PM)Gribouillis Wrote: [ -> ]Just make a list of Rotating instances
slots = []
for i in range(5):
     slots.append(Rotating((['xxx','aaa','bbb','ccc','ddd'] for i in range(10))))

value = slots[2][3][1]
But we'd like to see your Python code now. As @Larz60+ wrote above, this becomes interesting only if you try something by yourself.

Sure,
let me try. I will come back with my code.
THanks