Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a text file circular buffer in python

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.

like image 975
s23k1hlucjs6a50u Avatar asked Jun 09 '11 13:06

s23k1hlucjs6a50u


People also ask

What is circular buffer in Python?

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.

How do you make a buffer in Python?

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.

How do you slice a file in Python?

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.

What is circular buffer diagram?

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.


1 Answers

Use collections.deque. It supports a maxlen parameter.

d = collections.deque(maxlen=10)
for line in f:
    d.append(line)
    # ...
like image 90
Sven Marnach Avatar answered Sep 24 '22 12:09

Sven Marnach