Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:58:12+00:00 2026-06-01T20:58:12+00:00

I want to build a circular file buffer, in python, to hold file names

  • 0

I want to build a circular file buffer, in python, to hold file names (strings). The buffer should have the following properties.

  • The size of the buffer is the sum of the sizes of the files whose names are stored in the buffer. The buffer will have a maximum permitted size.
  • When a new file is added, if the buffer size is less than the maximum permitted size, that file name string is added. Else, the oldest modified file is pushed out and the new one is added. If the newly added file is older than all the files already present in the buffer, nothing takes place.

Is it possible to extend deque for such a purpose?

Or should I write it from scratch? Is there any design ideas that I can use for this purpose?

thanks

suresh

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:58:13+00:00Added an answer on June 1, 2026 at 8:58 pm

    OK, I believe that Raymond Hettinger’s interpretation of your question is correct, and your comment has clarified that you’re not concerned with the length of the queue, but rather with the sum of all the filesizes. That makes a lot more sense, and I’m glad I finally understand what you mean. With that in mind, here’s a simple implementation based on heapq that I believe satisfies all your stated requirements. Use it by putting (timestamp, filename, filesize) tuples on the queue, and note that when you get an item from the queue, it will be the oldest file (i.e. the file with the smallest timestamp.)

    import heapq
    
    class FilenameQueue(object):
        def __init__(self, times_sizes_names, maxsize):
            self.maxsize = maxsize
            self.size = sum(s for t, s, n in times_sizes_names)
            self.files = list(times_sizes_names)
            heapq.heapify(self.files)
            while self.size > self.maxsize:
                self.get()
        def __len__(self):
            return len(self.files)
        def put(self, time_size_name):
            self.size += time_size_name[1]
            if self.size < self.maxsize:
                heapq.heappush(self.files, time_size_name)
            else:
                time_size_name = heapq.heappushpop(self.files, time_size_name)
                self.size -= time_size_name[1]
        def get(self):
            time_size_name = heapq.heappop(self.files)
            self.size -= time_size_name[1]
            return time_size_name
    

    I added a __len__ method so that you can test the queue before getting from it. Here’s a usage example:

    >>> f = FilenameQueue(((22, 33, 'f1'), (44, 55, 'f2'), (33, 22, 'f3')), 150)
    >>> while f:
    ...     f.get()
    ... 
    (22, 33, 'f1')
    (33, 22, 'f3')
    (44, 55, 'f2')
    >>> f = FilenameQueue(((22, 33, 'f1'), (44, 55, 'f2'), (33, 22, 'f3')), 150)
    >>> f.put((55, 66, 'f4'))
    >>> while f:
    ...     f.get()
    ... 
    (33, 22, 'f3')
    (44, 55, 'f2')
    (55, 66, 'f4')
    

    See my edit history for a completely different solution involving Queue.PriorityQueue that is suboptimal. I forgot that maxsize enforces the limit by blocking, not by discarding elements. That’s not so useful!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build an executable to distribute to people without python installed on
I want to build two-dimentional array of strings where length of one dimention is
I want build a data structure to store limited undo buffer, take store 6
Want to build up a csv file, call up the default email app and
I have an existing EJB3/Hibernate (backend) and JSP (frontend) website. I want build an
I have crazy problem with xcode 4 when ever I want build and run
I want to build a lexer in C and I am following the dragon
i have a small java project i want to build using a makefile, the
I want to build a spring mvc project by maven, I got the following
i want build a photography app with effects . e.g. old images with brown

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.