Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 756b3f3

Browse files
committed
* Move collections.deque() in from the sandbox
* Add unittests, newsitem, and whatsnew * Apply to Queue.py mutex.py threading.py pydoc.py and shlex.py * Docs are forthcoming
1 parent 141d4e5 commit 756b3f3

15 files changed

Lines changed: 982 additions & 56 deletions

File tree

Doc/lib/libbisect.tex

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,5 @@ \subsection{Examples}
8080
'C'
8181
>>> map(grade, [33, 99, 77, 44, 12, 88])
8282
['E', 'A', 'B', 'D', 'F', 'A']
83-
\end{verbatim}
84-
85-
The bisect module can be used with the Queue module to implement a priority
86-
queue (example courtesy of Fredrik Lundh): \index{Priority Queue}
87-
88-
\begin{verbatim}
89-
import Queue, bisect
90-
91-
class PriorityQueue(Queue.Queue):
92-
def _put(self, item):
93-
bisect.insort(self.queue, item)
9483
95-
# usage
96-
queue = PriorityQueue(0)
97-
queue.put((2, "second"))
98-
queue.put((1, "first"))
99-
queue.put((3, "third"))
100-
priority, value = queue.get()
10184
\end{verbatim}

Doc/lib/libqueue.tex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ \section{\module{Queue} ---
1212
semantics. It depends on the availability of thread support in
1313
Python.
1414

15-
\begin{seealso}
16-
\seemodule{bisect}{PriorityQueue example using the Queue class}
17-
\end{seealso}
18-
1915
The \module{Queue} module defines the following class and exception:
2016

2117

Doc/whatsnew/whatsnew24.tex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,31 @@ \section{New, Improved, and Deprecated Modules}
322322
\item Korean: cp949, euc-kr, johab, iso-2022-kr
323323
\end{itemize}
324324

325+
\item There is a new \module{collections} module which currently offers
326+
just one new datatype, \class{deque}, which offers high-performance,
327+
thread-safe, memory friendly appends and pops on either side of the
328+
deque resulting in efficient stacks and queues:
329+
330+
\begin{verbatim}
331+
>>> from collections import deque
332+
>>> d = deque('ghi') # make a new deque with three items
333+
>>> d.append('j') # add a new entry to the right side
334+
>>> d.appendleft('f') # add a new entry to the left side
335+
>>> d # show the representation of the deque
336+
deque(['f', 'g', 'h', 'i', 'j'])
337+
>>> d.pop() # return and remove the rightmost item
338+
'j'
339+
>>> d.popleft() # return and remove the leftmost item
340+
'f'
341+
>>> list(d) # list the contents of the deque
342+
['g', 'h', 'i']
343+
>>> 'h' in d # search the deque
344+
True
345+
\end{verbatim}
346+
347+
Several modules now take advantage of \class{collections.deque()} for
348+
improved performance: \module{Queue}, \module{mutex}, \module{shlex}
349+
\module{threading}, and \module{pydoc}.
325350

326351
\item The \module{heapq} module has been converted to C. The resulting
327352
ten-fold improvement in speed makes the module suitable for handling

Lib/Queue.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""A multi-producer, multi-consumer queue."""
22

33
from time import time as _time, sleep as _sleep
4+
from collections import deque
45

56
__all__ = ['Empty', 'Full', 'Queue']
67

@@ -184,7 +185,7 @@ def get_nowait(self):
184185
# Initialize the queue representation
185186
def _init(self, maxsize):
186187
self.maxsize = maxsize
187-
self.queue = []
188+
self.queue = deque()
188189

189190
def _qsize(self):
190191
return len(self.queue)
@@ -203,4 +204,4 @@ def _put(self, item):
203204

204205
# Get an item from the queue
205206
def _get(self):
206-
return self.queue.pop(0)
207+
return self.queue.popleft()

Lib/mutex.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
for lock, where a function is called once the lock is aquired.
1313
"""
1414

15+
from collections import deque
16+
1517
class mutex:
1618
def __init__(self):
1719
"""Create a new mutex -- initially unlocked."""
1820
self.locked = 0
19-
self.queue = []
21+
self.queue = deque()
2022

2123
def test(self):
2224
"""Test the locked bit of the mutex."""
@@ -44,7 +46,7 @@ def unlock(self):
4446
"""Unlock a mutex. If the queue is not empty, call the next
4547
function with its argument."""
4648
if self.queue:
47-
function, argument = self.queue.pop(0)
49+
function, argument = self.queue.popleft()
4850
function(argument)
4951
else:
5052
self.locked = 0

Lib/pydoc.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class or function within a module or module in a package. If the
5555
import sys, imp, os, re, types, inspect, __builtin__
5656
from repr import Repr
5757
from string import expandtabs, find, join, lower, split, strip, rfind, rstrip
58+
from collections import deque
5859

5960
# --------------------------------------------------------- common routines
6061

@@ -685,7 +686,7 @@ def maybe(self):
685686
hr = HorizontalRule()
686687

687688
# List the mro, if non-trivial.
688-
mro = list(inspect.getmro(object))
689+
mro = deque(inspect.getmro(object))
689690
if len(mro) > 2:
690691
hr.maybe()
691692
push('<dl><dt>Method resolution order:</dt>\n')
@@ -763,7 +764,7 @@ def spilldata(msg, attrs, predicate):
763764

764765
while attrs:
765766
if mro:
766-
thisclass = mro.pop(0)
767+
thisclass = mro.popleft()
767768
else:
768769
thisclass = attrs[0][2]
769770
attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)
@@ -1083,7 +1084,7 @@ def makename(c, m=object.__module__):
10831084
push = contents.append
10841085

10851086
# List the mro, if non-trivial.
1086-
mro = list(inspect.getmro(object))
1087+
mro = deque(inspect.getmro(object))
10871088
if len(mro) > 2:
10881089
push("Method resolution order:")
10891090
for base in mro:
@@ -1152,7 +1153,7 @@ def spilldata(msg, attrs, predicate):
11521153
inspect.classify_class_attrs(object))
11531154
while attrs:
11541155
if mro:
1155-
thisclass = mro.pop(0)
1156+
thisclass = mro.popleft()
11561157
else:
11571158
thisclass = attrs[0][2]
11581159
attrs, inherited = _split_list(attrs, lambda t: t[2] is thisclass)

Lib/shlex.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import os.path
1111
import sys
12+
from collections import deque
1213

1314
try:
1415
from cStringIO import StringIO
@@ -45,11 +46,11 @@ def __init__(self, instream=None, infile=None, posix=False):
4546
self.escape = '\\'
4647
self.escapedquotes = '"'
4748
self.state = ' '
48-
self.pushback = []
49+
self.pushback = deque()
4950
self.lineno = 1
5051
self.debug = 0
5152
self.token = ''
52-
self.filestack = []
53+
self.filestack = deque()
5354
self.source = None
5455
if self.debug:
5556
print 'shlex: reading from %s, line %d' \
@@ -59,13 +60,13 @@ def push_token(self, tok):
5960
"Push a token onto the stack popped by the get_token method"
6061
if self.debug >= 1:
6162
print "shlex: pushing token " + `tok`
62-
self.pushback.insert(0, tok)
63+
self.pushback.appendleft(tok)
6364

6465
def push_source(self, newstream, newfile=None):
6566
"Push an input source onto the lexer's input source stack."
6667
if isinstance(newstream, basestring):
6768
newstream = StringIO(newstream)
68-
self.filestack.insert(0, (self.infile, self.instream, self.lineno))
69+
self.filestack.appendleft((self.infile, self.instream, self.lineno))
6970
self.infile = newfile
7071
self.instream = newstream
7172
self.lineno = 1
@@ -78,8 +79,7 @@ def push_source(self, newstream, newfile=None):
7879
def pop_source(self):
7980
"Pop the input source stack."
8081
self.instream.close()
81-
(self.infile, self.instream, self.lineno) = self.filestack[0]
82-
self.filestack = self.filestack[1:]
82+
(self.infile, self.instream, self.lineno) = self.filestack.popleft()
8383
if self.debug:
8484
print 'shlex: popping to %s, line %d' \
8585
% (self.instream, self.lineno)
@@ -88,7 +88,7 @@ def pop_source(self):
8888
def get_token(self):
8989
"Get a token from the input stream (or from stack if it's nonempty)"
9090
if self.pushback:
91-
tok = self.pushback.pop(0)
91+
tok = self.pushback.popleft()
9292
if self.debug >= 1:
9393
print "shlex: popping token " + `tok`
9494
return tok
@@ -226,7 +226,7 @@ def read_token(self):
226226
or self.whitespace_split:
227227
self.token = self.token + nextchar
228228
else:
229-
self.pushback.insert(0, nextchar)
229+
self.pushback.appendleft(nextchar)
230230
if self.debug >= 2:
231231
print "shlex: I see punctuation in word state"
232232
self.state = ' '

Lib/test/test_bisect.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,6 @@ def test_backcompatibility(self):
170170
>>> map(grade, [33, 99, 77, 44, 12, 88])
171171
['E', 'A', 'B', 'D', 'F', 'A']
172172
173-
The bisect module can be used with the Queue module to implement
174-
a priority queue (example courtesy of Fredrik Lundh):
175-
176-
>>> import Queue, bisect
177-
>>> class PriorityQueue(Queue.Queue):
178-
... def _put(self, item):
179-
... bisect.insort(self.queue, item)
180-
...
181-
>>> queue = PriorityQueue(0)
182-
>>> queue.put((2, "second"))
183-
>>> queue.put((1, "first"))
184-
>>> queue.put((3, "third"))
185-
>>> queue.get()
186-
(1, 'first')
187-
>>> queue.get()
188-
(2, 'second')
189-
190173
"""
191174

192175
#------------------------------------------------------------------------------

0 commit comments

Comments
 (0)