Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60759

Re: how to implement a queue-like container with sort function

Date 2013-11-29 17:19 +1100
From Cameron Simpson <cs@zip.com.au>
Subject Re: how to implement a queue-like container with sort function
References <cf8a9111-2202-4519-b6aa-48744b5ba198@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3393.1385705979.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 28Nov2013 18:04, iMath <redstone-cold@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@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to implement a queue-like container  with sort function iMath <redstone-cold@163.com> - 2013-11-28 17:54 -0800
  Re: how to implement a queue-like container with sort function Chris Angelico <rosuav@gmail.com> - 2013-11-29 13:03 +1100
    Re: how to implement a queue-like container with sort function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-29 03:32 +0000
  Re: how to implement a queue-like container  with sort function iMath <redstone-cold@163.com> - 2013-11-28 18:04 -0800
    Re: how to implement a queue-like container with sort function Chris Angelico <rosuav@gmail.com> - 2013-11-29 13:06 +1100
      Re: how to implement a queue-like container with sort function iMath <redstone-cold@163.com> - 2013-11-28 18:32 -0800
    Re: how to implement a queue-like container  with sort function Cameron Simpson <cs@zip.com.au> - 2013-11-29 17:19 +1100
  Re: how to implement a queue-like container  with sort function MRAB <python@mrabarnett.plus.com> - 2013-11-29 02:23 +0000
  Re: how to implement a queue-like container  with sort function Terry Reedy <tjreedy@udel.edu> - 2013-11-28 21:31 -0500
  Re: how to implement a queue-like container  with sort function Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-11-29 17:53 +1300
  Re: how to implement a queue-like container  with sort function iMath <redstone-cold@163.com> - 2013-11-29 04:33 -0800
    Re: how to implement a queue-like container  with sort function Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-29 14:57 +0000
      Re: how to implement a queue-like container  with sort function iMath <redstone-cold@163.com> - 2013-12-02 03:41 -0800
        Re: how to implement a queue-like container  with sort function Ned Batchelder <ned@nedbatchelder.com> - 2013-12-02 06:58 -0500
        Re: how to implement a queue-like container with sort function Chris Angelico <rosuav@gmail.com> - 2013-12-02 23:04 +1100
        Re: how to implement a queue-like container with sort function Ned Batchelder <ned@nedbatchelder.com> - 2013-12-02 07:26 -0500

csiph-web