Various additions and changes suggested by Raymond Hettinger

This commit is contained in:
Andrew M. Kuchling 2002-12-11 15:03:51 +00:00
parent da9f853b49
commit 449a87d791

View file

@ -833,6 +833,16 @@ creates a dictionary with keys taken from the supplied iterator
(Patches contributed by Raymond Hettinger.) (Patches contributed by Raymond Hettinger.)
The dict() constructor now also accepts keyword arguments to simplify
creating small dictionaries:
\begin{verbatim}
>>> dict(red=1, blue=2, green=3, black=4)
{'blue': 2, 'black': 4, 'green': 3, 'red': 1}
\end{verbatim}
(Contributed by Just van Rossum.)
\item The \keyword{assert} statement no longer checks the \code{__debug__} \item The \keyword{assert} statement no longer checks the \code{__debug__}
flag, so you can no longer disable assertions by assigning to \code{__debug__}. flag, so you can no longer disable assertions by assigning to \code{__debug__}.
Running Python with the \programopt{-O} switch will still generate Running Python with the \programopt{-O} switch will still generate
@ -1014,6 +1024,10 @@ and significantly reworked by Tim Peters.)
small speed increase, subject to your compiler's idiosyncrasies. small speed increase, subject to your compiler's idiosyncrasies.
(Removed by Michael Hudson.) (Removed by Michael Hudson.)
\item \function{xrange()} objects now have their own iterator, making
\code{for i in xrange(n)} slightly faster than
\code{for i in range(n)}. (Patch by Raymond Hettinger.)
\item A number of small rearrangements have been made in various \item A number of small rearrangements have been made in various
hotspots to improve performance, inlining a function here, removing hotspots to improve performance, inlining a function here, removing
some code there. (Implemented mostly by GvR, but lots of people have some code there. (Implemented mostly by GvR, but lots of people have
@ -1177,28 +1191,30 @@ will enable buffering.
\item The \function{sample(\var{population}, \var{k})} function was \item The \function{sample(\var{population}, \var{k})} function was
added to the \module{random} module. \var{population} is a sequence added to the \module{random} module. \var{population} is a sequence
containing the elements of a population, and \function{sample()} or \code{xrange} object containing the elements of a population, and \function{sample()}
chooses \var{k} elements from the population without replacing chosen chooses \var{k} elements from the population without replacing chosen
elements. \var{k} can be any value up to \code{len(\var{population})}. elements. \var{k} can be any value up to \code{len(\var{population})}.
For example: For example:
\begin{verbatim} \begin{verbatim}
>>> pop = range(6) ; pop >>> days = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'St', 'Sn']
[0, 1, 2, 3, 4, 5] >>> random.sample(days, 3) # Choose 3 elements
>>> random.sample(pop, 3) # Choose three elements ['St', 'Sn', 'Th']
[0, 4, 3] >>> random.sample(days, 7) # Choose 7 elements
>>> random.sample(pop, 6) # Choose all six elements ['Tu', 'Th', 'Mo', 'We', 'St', 'Fr', 'Sn']
[4, 5, 0, 3, 2, 1] >>> random.sample(days, 7) # Choose 7 again
>>> random.sample(pop, 6) # Choose six again ['We', 'Mo', 'Sn', 'Fr', 'Tu', 'St', 'Th']
[4, 2, 3, 0, 5, 1] >>> random.sample(days, 8) # Can't choose eight
>>> random.sample(pop, 7) # Can't choose more than six
Traceback (most recent call last): Traceback (most recent call last):
File "<stdin>", line 1, in ? File "<stdin>", line 1, in ?
File "random.py", line 396, in sample File "random.py", line 414, in sample
raise ValueError, "sample larger than population" raise ValueError, "sample larger than population"
ValueError: sample larger than population ValueError: sample larger than population
>>> >>> random.sample(xrange(1,10000,2), 10) # Choose ten odds under 10000
[3407, 3805, 1505, 7023, 2401, 2267, 9733, 3151, 8083, 9195]
\end{verbatim} \end{verbatim}
(Contributed by Raymond Hettinger.)
\item The \module{readline} module also gained a number of new \item The \module{readline} module also gained a number of new
functions: \function{get_history_item()}, functions: \function{get_history_item()},
@ -1270,6 +1286,58 @@ sometimes have odd bugs. Brett Cannon contributed a portable
implementation that's written in pure Python, which should behave implementation that's written in pure Python, which should behave
identically on all platforms. identically on all platforms.
\item The \module{UserDict) has a new \class{DictMixin} class which
defines all dictionary methods for classes that already have a minimum
mapping interface. This greatly simplifies writing classes that need
to be substitutable for dictionaries, such as the classes in
the \module{shelve} module.
Adding the mixin as a superclass provides the full dictionary
interface whenever the class defines \method{__getitem__},
\method{__setitem__}, \method{__delitem__), and \method{keys}.
For example:
\begin{verbatim}
>>> import UserDict
>>> class SeqDict(UserDict.DictMixin):
"""Dictionary lookalike implemented with lists."""
def __init__(self):
self.keylist = []
self.valuelist = []
def __getitem__(self, key):
try:
i = self.keylist.index(key)
except ValueError:
raise KeyError
return self.valuelist[i]
def __setitem__(self, key, value):
try:
i = self.keylist.index(key)
self.valuelist[i] = value
except ValueError:
self.keylist.append(key)
self.valuelist.append(value)
def __delitem__(self, key):
try:
i = self.keylist.index(key)
except ValueError:
raise KeyError
self.keylist.pop(i)
self.valuelist.pop(i)
def keys(self):
return list(self.keylist)
>>> s = SeqDict()
>>> dir(s) # See that other dictionary methods are implemented
['__cmp__', '__contains__', '__delitem__', '__doc__', '__getitem__',
'__init__', '__iter__', '__len__', '__module__', '__repr__',
'__setitem__', 'clear', 'get', 'has_key', 'items', 'iteritems',
'iterkeys', 'itervalues', 'keylist', 'keys', 'pop', 'popitem',
'setdefault', 'update', 'valuelist', 'values']
\end {verbatim}
(Contributed by Raymond Hettinger.)
\item The DOM implementation \item The DOM implementation
in \module{xml.dom.minidom} can now generate XML output in a in \module{xml.dom.minidom} can now generate XML output in a
particular encoding, by specifying an optional encoding argument to particular encoding, by specifying an optional encoding argument to
@ -1711,8 +1779,9 @@ name.
The author would like to thank the following people for offering The author would like to thank the following people for offering
suggestions, corrections and assistance with various drafts of this suggestions, corrections and assistance with various drafts of this
article: Simon Brunning, Michael Chermside, Scott David Daniels, article: Simon Brunning, Michael Chermside, Scott David Daniels,
Fred~L. Drake, Jr., Michael Hudson, Detlef Lannert, Martin von Fred~L. Drake, Jr., Raymond Hettinger, Michael Hudson, Detlef Lannert,
L\"owis, Andrew MacIntyre, Lalo Martins, Gustavo Niemeyer, Neal Martin von L\"owis, Andrew MacIntyre, Lalo Martins, Gustavo Niemeyer,
Norwitz, Chris Reedy, Vinay Sajip, Neil Schemenauer, Jason Tishler. Neal Norwitz, Chris Reedy, Vinay Sajip, Neil Schemenauer, Jason
Tishler.
\end{document} \end{document}