how to implement a queue-like container with sort function

Cameron Simpson cs at zip.com.au
Fri Nov 29 01:19:25 EST 2013


On 28Nov2013 18:04, iMath <redstone-cold at 163.com> wrote:
> All in all,I want to first fill the container, then sort it and process all the contents in it 

Automatically?

It sounds like an I/O buffer, in the sense that a block buffered
output stream does a write on buffer full.

Something like this:

  class Chunkifier:

    def __init__(self, size, process):
      self.size = size
      self.buffer = []

    def append(self, item):
      self.buffer.append(item)
      if len(self.buffer) >= self.size:
        process(sorted(self.buffer))
        self.buffer = []

    def extend(self, items):
      for item in items:
        self.append(item)

  def burp(items):
    for item in items:
      print item

  burper = Chunkifier(10, burp)
  burper.extend( (9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1) )

Totally untested, buyer beware, worth what you paid for it, etc...

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

It is necessary for technical reasons that these warheads be stored with
the top at the bottom and the bottom at the top. In order that there may
be no doubt as to which is the top and which is the bottom, for storage
purposes it will be seen that the bottom of each head has been labelled
with the word TOP.      - Instructions for storing British nuclear warheads



More information about the Python-list mailing list