[Python-checkins] python/dist/src/Doc/whatsnew whatsnew23.tex,1.84,1.85

akuchling@users.sourceforge.net akuchling@users.sourceforge.net
Wed, 11 Dec 2002 07:03:58 -0800


Update of /cvsroot/python/python/dist/src/Doc/whatsnew
In directory sc8-pr-cvs1:/tmp/cvs-serv7669

Modified Files:
	whatsnew23.tex 
Log Message:
Various additions and changes suggested by Raymond Hettinger

Index: whatsnew23.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/whatsnew/whatsnew23.tex,v
retrieving revision 1.84
retrieving revision 1.85
diff -C2 -d -r1.84 -r1.85
*** whatsnew23.tex	10 Dec 2002 21:04:25 -0000	1.84
--- whatsnew23.tex	11 Dec 2002 15:03:51 -0000	1.85
***************
*** 834,837 ****
--- 834,847 ----
  (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__}
  flag, so you can no longer disable assertions by assigning to \code{__debug__}.
***************
*** 1015,1018 ****
--- 1025,1032 ----
  (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
  hotspots to improve performance, inlining a function here, removing
***************
*** 1178,1182 ****
  \item The \function{sample(\var{population}, \var{k})} function was
  added to the \module{random} module.  \var{population} is a sequence
! containing the elements of a population, and \function{sample()}
  chooses \var{k} elements from the population without replacing chosen
  elements.  \var{k} can be any value up to \code{len(\var{population})}.
--- 1192,1196 ----
  \item The \function{sample(\var{population}, \var{k})} function was
  added to the \module{random} module.  \var{population} is a sequence
! or \code{xrange} object containing the elements of a population, and \function{sample()}
  chooses \var{k} elements from the population without replacing chosen
  elements.  \var{k} can be any value up to \code{len(\var{population})}.
***************
*** 1184,1203 ****
  
  \begin{verbatim}
! >>> pop = range(6) ; pop
! [0, 1, 2, 3, 4, 5]
! >>> random.sample(pop, 3)          # Choose three elements
! [0, 4, 3]
! >>> random.sample(pop, 6)          # Choose all six elements
! [4, 5, 0, 3, 2, 1]
! >>> random.sample(pop, 6)          # Choose six again
! [4, 2, 3, 0, 5, 1]
! >>> random.sample(pop, 7)          # Can't choose more than six
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
!   File "random.py", line 396, in sample
!     raise ValueError, "sample larger than population"
  ValueError: sample larger than population
! >>>
  \end{verbatim}
  
  \item The \module{readline} module also gained a number of new
--- 1198,1219 ----
  
  \begin{verbatim}
! >>> days = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'St', 'Sn']
! >>> random.sample(days, 3)	# Choose 3 elements
! ['St', 'Sn', 'Th']
! >>> random.sample(days, 7)	# Choose 7 elements
! ['Tu', 'Th', 'Mo', 'We', 'St', 'Fr', 'Sn']
! >>> random.sample(days, 7)	# Choose 7 again
! ['We', 'Mo', 'Sn', 'Fr', 'Tu', 'St', 'Th']
! >>> random.sample(days, 8)	# Can't choose eight
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
!   File "random.py", line 414, in sample
!       raise 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}
+  
+ (Contributed by Raymond Hettinger.)
  
  \item The \module{readline} module also gained a number of new
***************
*** 1271,1274 ****
--- 1287,1342 ----
  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
  in \module{xml.dom.minidom} can now generate XML output in a
***************
*** 1712,1718 ****
  suggestions, corrections and assistance with various drafts of this
  article: Simon Brunning, Michael Chermside, Scott David Daniels,
! Fred~L. Drake, Jr., Michael Hudson, Detlef Lannert, Martin von
! L\"owis, Andrew MacIntyre, Lalo Martins, Gustavo Niemeyer, Neal
! Norwitz, Chris Reedy, Vinay Sajip, Neil Schemenauer, Jason Tishler.
  
  \end{document}
--- 1780,1787 ----
  suggestions, corrections and assistance with various drafts of this
  article: Simon Brunning, Michael Chermside, Scott David Daniels,
! Fred~L. Drake, Jr., Raymond Hettinger, Michael Hudson, Detlef Lannert,
! Martin von L\"owis, Andrew MacIntyre, Lalo Martins, Gustavo Niemeyer,
! Neal Norwitz, Chris Reedy, Vinay Sajip, Neil Schemenauer, Jason
! Tishler.
  
  \end{document}