[Python-ideas] Queue suggestions
Gregory P. Smith
greg at krypto.org
Fri May 23 08:56:58 CEST 2008
Do you have a compelling use case for Queue.rotate?
On Thu, May 22, 2008 at 10:23 PM, George Sakkis <george.sakkis at gmail.com> wrote:
> I'd like to propose a signature simplification and a new method for
> the Queue.Queue class:
>
> 1) Drop the optional `block` argument from put() and get(), since all
> the meaningful combinations of (block, timeout) are equivalent to
> passing block = (timeout is None or timeout>0). IOW, instead of
> passing block=False pass timeout=0 (or negative). Obviously this is to
> be considered for 3.x only.
>
> 2) Add a new `rotate` method as the atomic equivalent of
> put_nowait(get_nowait()). Currently I use the following subclass but
> it would be nice to have it in the base Queue:
>
> class RQueue(Queue):
>
> def rotate(self, n=1):
> '''Rotate this queue n steps to the left (if it is not empty).
>
> Rotating one step is equivalent to an atomic
> q.put_nowait(q.get_nowait()).
> '''
> if n < 0:
> raise ValueError('n must be non-negative')
> self.mutex.acquire()
> try:
> if not self._empty():
> self._rotate(n)
> finally:
> self.mutex.release()
>
> def __init__(self, maxsize=0):
> Queue.__init__(self, maxsize)
> if hasattr(self.queue, 'rotate'): # deque has rotate() since v2.5
> def _rotate(n):
> # negative n for left rotate
> self.queue.rotate(-n)
> else:
> def _rotate(n):
> put,get = self._put, self._get
> for i in xrange(n):
> put(get())
> self._rotate = _rotate
>
>
> What do you think ?
>
> George
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
More information about the Python-ideas
mailing list