I need a python script implementing a circular buffer for rows in a text file limited to N rows like this:
row 1 -> pop
row 2
row 3
|
|
push -> row N
What's the best solution?
EDIT: This script should create and maintain the text file which only contains the latest N lines. Then it should pop the first line pushed in. Like a fifo buffer.
A ring buffer is a buffer with a fixed size. When it fills up, adding another element overwrites the oldest one that was still being kept. It's particularly useful for the storage of log information and history. There is no direct support in Python for this kind of structure, but it's easy to construct one.
In Python, you don't have to care about memory management. You don't need to reserve "buffers", just assign the variables you want, and use them. If you assign too many (for your RAM), you might run into MemoryError s, indicating you don't have enough memory. You could use a Queue as some kind of buffer, though, e.g.
Introducing the split() method. The fastest way to split text in Python is with the split() method. This is a built-in method that is useful for separating a string into its individual parts. The split() method will return a list of the elements in a string.
A ring buffer, or circular buffer, is a data structure used to emulate a wrap-around in memory. Ring buffers are contiguous blocks of memory that act as if they are circular, with no beginning or end, instead of 1D.
Use collections.deque
. It supports a maxlen
parameter.
d = collections.deque(maxlen=10)
for line in f:
d.append(line)
# ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With