[issue35118] Add peek() or first() method in queue
New submission from Windson Yang <wiwindson@gmail.com>: I found other languages like Java and C++ have the method to access the first value in Queue like first() and peek(). Since we use deque_ to create Queue now, it's easy to implement in python using the index. Otherwise, we can add this to the document? I also found some discussion_ here. .. _deque: https://github.com/python/cpython/blob/master/Lib/queue.py#L205 .. _discussion https://mail.python.org/pipermail/python-list/2010-March/569930.html ---------- assignee: docs@python components: Documentation messages: 328963 nosy: Windson Yang, docs@python priority: normal severity: normal status: open title: Add peek() or first() method in queue type: enhancement versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- assignee: docs@python -> rhettinger nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: For deque, we have d[0]. Is anything more needed? Even lists don't require a peek. For Queue, I'm not sure I've ever seen any use case for peek. What do you have in mind? * https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html#peek() ---------- versions: -Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
Windson Yang <wiwindson@gmail.com> added the comment: For deque, we can add peek() function to deque or just make it clear in the document that we can use deque[0] to access the first element(I can only find index method in the document) For Queue, I found Java and C++ has the function like first() or peek(), I'm not sure should we also implement a method like this. * http://www.cplusplus.com/reference/queue/queue/ ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: In the absence of good use cases, I'll decline the API expansion. Feel free to post a PR to have the deque documentation to mention d[0] indexing more prominently. Am not really sure that is needed though, we don't have to point out the same thing for lists and I haven't encountered any misunderstandings on the topic. This would be just a minor usage note. ---------- priority: normal -> low _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
Change by Windson Yang <wiwindson@gmail.com>: ---------- keywords: +patch pull_requests: +9576 stage: -> patch review _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: New changeset 98b85354153883b0a080f678f213729cd0764fee by Raymond Hettinger (Windson yang) in branch 'master': bpo-35118: Improve docs regarding indexing (GH-10265) https://github.com/python/cpython/commit/98b85354153883b0a080f678f213729cd07... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
Alexey Volkov <alexey.volkov@ark-kun.com> added the comment:
For Queue, I'm not sure I've ever seen any use case for peek. What do you have in mind?
I want to examine the first (oldest) element in queue and remove it if it's too old. The queue should not be modified unless the oldest element is too old. ---------- nosy: +Ark-kun _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:
I want to examine the first (oldest) element in queue and remove it if it's too old.
Why not just dismiss older queue entries during a normal get() operation? Or just use a plain deque with access guarded by a lock. FWIW, the standard library queue module doesn't have a straight-forward way to implement a peek() method. The module guarantees that the underlying data structure is only accessed with _init, _qsize, _get, and _put. That would be difficult to do atomically with a Queue object. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
Alexey Volkov <alexey.volkov@ark-kun.com> added the comment:
Why not just dismiss older queue entries during a normal get() operation? Or just use a plain deque with access guarded by a lock.
You do not know whether the item needs to be removed until you see it. And to see it you need to get it. And if you get it, you cannot push it back to the start of the queue.
FWIW, the standard library queue module doesn't have a straight-forward way to implement a peek() method.
I currently use `q.deque[0]`
Or just use a plain deque with access guarded by a lock.
I can. But technically the same applies to almost any queue usage. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:
FWIW, the standard library queue module doesn't have a straight-forward way to implement a peek() method.
I currently use `q.deque[0]`
The Queue class is only allowed to call _init, _qsize, _put, and _get. It is not allowed to directly touch the underlying data structure. Otherwise, subclasses relying on the abstraction would fail. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35118> _______________________________________
participants (3)
-
Alexey Volkov
-
Raymond Hettinger
-
Windson Yang