@@ -138,14 +138,21 @@ def __init__(self, *args, **kwargs):
138138
139139 # Cache function pointers to SortedList methods.
140140
141- self ._list_add = self ._list .add
142- self ._list_bisect_left = self ._list .bisect_left
143- self ._list_bisect_right = self ._list .bisect_right
144- self ._list_clear = self ._list .clear
145- self ._list_index = self ._list .index
146- self ._list_pop = self ._list .pop
147- self ._list_remove = self ._list .remove
148- self ._list_update = self ._list .update
141+ _list = self ._list
142+ self ._list_add = _list .add
143+ self .bisect_left = _list .bisect_left
144+ self .bisect = _list .bisect_right
145+ self .bisect_right = _list .bisect_right
146+ self ._list_clear = _list .clear
147+ self .index = _list .index
148+ self ._list_pop = _list .pop
149+ self ._list_remove = _list .remove
150+ self ._list_update = _list .update
151+
152+ if self ._key is not None :
153+ self .bisect_key_left = _list .bisect_key_left
154+ self .bisect_key_right = _list .bisect_key_right
155+ self .bisect_key = _list .bisect_key
149156
150157 self .iloc = _IlocWrapper (self )
151158
@@ -182,7 +189,7 @@ def __setitem__(self, key, value):
182189
183190 def copy (self ):
184191 """Return a shallow copy of the sorted dictionary."""
185- return SortedDict (self ._key , self ._load , self .iteritems ())
192+ return self . __class__ (self ._key , self ._load , self .iteritems ())
186193
187194 __copy__ = copy
188195
@@ -191,55 +198,55 @@ def fromkeys(cls, seq, value=None):
191198 """
192199 Create a new dictionary with keys from *seq* and values set to *value*.
193200 """
194- that = cls ((key , value ) for key in seq )
195- return that
201+ return cls ((key , value ) for key in seq )
196202
197- def items (self ):
198- """
199- In Python 2, returns a list of the dictionary's items (``(key, value)``
200- pairs).
201-
202- In Python 3, returns a new ItemsView of the dictionary's items. In
203- addition to the methods provided by the built-in `view` the ItemsView is
204- indexable (e.g., ``d.items()[5]``).
205- """
206- if hexversion < 0x03000000 :
203+ if hexversion < 0x03000000 :
204+ def items (self ):
205+ """
206+ Return a list of the dictionary's items (``(key, value)`` pairs).
207+ """
207208 return list (self .iteritems ())
208- else :
209+ else :
210+ def items (self ):
211+ """
212+ Return a new ItemsView of the dictionary's items. In addition to
213+ the methods provided by the built-in `view` the ItemsView is
214+ indexable (e.g. ``d.items()[5]``).
215+ """
209216 return ItemsView (self )
210217
211218 def iteritems (self ):
212219 """Return an iterable over the items (``(key, value)`` pairs)."""
213220 return iter ((key , self [key ]) for key in self ._list )
214221
215- def keys (self ):
216- """
217- In Python 2, return a SortedSet of the dictionary's keys.
218-
219- In Python 3, return a new KeysView of the dictionary's keys. In
220- addition to the methods provided by the built-in `view` the KeysView is
221- indexable (e.g., ``d.keys()[5]``).
222- """
223- if hexversion < 0x03000000 :
222+ if hexversion < 0x03000000 :
223+ def keys (self ):
224+ """Return a SortedSet of the dictionary's keys."""
224225 return SortedSet (self ._list , key = self ._key , load = self ._load )
225- else :
226+ else :
227+ def keys (self ):
228+ """
229+ Return a new KeysView of the dictionary's keys. In addition to the
230+ methods provided by the built-in `view` the KeysView is indexable
231+ (e.g. ``d.keys()[5]``).
232+ """
226233 return KeysView (self )
227234
228235 def iterkeys (self ):
229236 """Return an iterable over the keys of the dictionary."""
230237 return iter (self ._list )
231238
232- def values (self ):
233- """
234- In Python 2, return a list of the dictionary's values.
235-
236- In Python 3, return a new :class:`ValuesView` of the dictionary's
237- values. In addition to the methods provided by the built-in `view`
238- the ValuesView is indexable (e.g., ``d.values()[5]``).
239- """
240- if hexversion < 0x03000000 :
239+ if hexversion < 0x03000000 :
240+ def values (self ):
241+ """Return a list of the dictionary's values."""
241242 return list (self .itervalues ())
242- else :
243+ else :
244+ def values (self ):
245+ """
246+ Return a new :class:`ValuesView` of the dictionary's values.
247+ In addition to the methods provided by the built-in `view` the
248+ ValuesView is indexable (e.g., ``d.values()[5]``).
249+ """
243250 return ValuesView (self )
244251
245252 def itervalues (self ):
@@ -261,18 +268,19 @@ def pop(self, key, default=_NotGiven):
261268 else :
262269 return default
263270
264- def popitem (self ):
271+ def popitem (self , last = True ):
265272 """
266- Remove and return the ``(key, value)`` pair with the greatest *key*
267- from the dictionary.
273+ Remove and return a ``(key, value)`` pair from the dictionary. If
274+ last=True (default) then remove the *greatest* `key` from the
275+ diciontary. Else, remove the *least* key from the dictionary.
268276
269277 If the dictionary is empty, calling `popitem` raises a
270278 KeyError`.
271279 """
272280 if not len (self ):
273281 raise KeyError ('popitem(): dictionary is empty' )
274282
275- key = self ._list_pop ()
283+ key = self ._list_pop (- 1 if last else 0 )
276284 value = self ._pop (key )
277285
278286 return (key , value )
@@ -318,36 +326,6 @@ def update(self, *args, **kwargs):
318326 for key in pairs :
319327 self [key ] = pairs [key ]
320328
321- def index (self , key , start = None , stop = None ):
322- """
323- Return the smallest *k* such that `d.iloc[k] == key` and `i <= k < j`.
324- Raises `ValueError` if *key* is not present. *stop* defaults to the end
325- of the set. *start* defaults to the beginning. Negative indexes are
326- supported, as for slice indices.
327- """
328- return self ._list_index (key , start , stop )
329-
330- def bisect_left (self , key ):
331- """
332- Similar to the ``bisect`` module in the standard library, this returns
333- an appropriate index to insert *key* in SortedDict. If *key* is
334- already present in SortedDict, the insertion point will be before (to
335- the left of) any existing entries.
336- """
337- return self ._list_bisect_left (key )
338-
339- def bisect (self , key ):
340- """Same as bisect_right."""
341- return self ._list_bisect_right (key )
342-
343- def bisect_right (self , key ):
344- """
345- Same as `bisect_left`, but if *key* is already present in SortedDict,
346- the insertion point will be after (to the right of) any existing
347- entries.
348- """
349- return self ._list_bisect_right (key )
350-
351329 @not26
352330 def viewkeys (self ):
353331 """
@@ -378,11 +356,20 @@ def viewitems(self):
378356 """
379357 return ItemsView (self )
380358
359+ def __reduce__ (self ):
360+ return (self .__class__ , (self ._key , self ._load , list (self .iteritems ())))
361+
381362 @recursive_repr
382363 def __repr__ (self ):
364+ temp = '{0}({1}, {2}, {{{3}}})'
383365 items = ', ' .join ('{0}: {1}' .format (repr (key ), repr (self [key ]))
384366 for key in self ._list )
385- return '{0}({{{1}}})' .format (self .__class__ .__name__ , items )
367+ return temp .format (
368+ self .__class__ .__name__ ,
369+ repr (self ._key ),
370+ repr (self ._load ),
371+ items
372+ )
386373
387374 def _check (self ):
388375 self ._list ._check ()
@@ -397,14 +384,19 @@ class KeysView(AbstractKeysView, Set, Sequence):
397384
398385 The KeysView class implements the Set and Sequence Abstract Base Classes.
399386 """
400- def __init__ ( self , sorted_dict ) :
401- """
402- Initialize a KeysView from a SortedDict container as *sorted_dict*.
403- """
404- self . _list = sorted_dict . _list
405- if hexversion < 0x03000000 :
387+ if hexversion < 0x03000000 :
388+ def __init__ ( self , sorted_dict ):
389+ """
390+ Initialize a KeysView from a SortedDict container as *sorted_dict*.
391+ """
392+ self . _list = sorted_dict . _list
406393 self ._view = sorted_dict ._dict .viewkeys ()
407- else :
394+ else :
395+ def __init__ (self , sorted_dict ):
396+ """
397+ Initialize a KeysView from a SortedDict container as *sorted_dict*.
398+ """
399+ self ._list = sorted_dict ._list
408400 self ._view = sorted_dict ._dict .keys ()
409401 def __len__ (self ):
410402 """Return the number of entries in the dictionary."""
@@ -477,11 +469,13 @@ def __sub__(self, that):
477469 def __xor__ (self , that ):
478470 """Return a SortedSet of the symmetric difference of self and *that*."""
479471 return SortedSet (self ._view ^ that )
480- def isdisjoint ( self , that ) :
481- """Return True if and only if * that* is disjoint with self."""
482- if hexversion < 0x03000000 :
472+ if hexversion < 0x03000000 :
473+ def isdisjoint ( self , that ):
474+ """Return True if and only if *that* is disjoint with self."""
483475 return not any (key in self ._list for key in that )
484- else :
476+ else :
477+ def isdisjoint (self , that ):
478+ """Return True if and only if *that* is disjoint with self."""
485479 return self ._view .isdisjoint (that )
486480 @recursive_repr
487481 def __repr__ (self ):
@@ -495,15 +489,23 @@ class ValuesView(AbstractValuesView, Sequence):
495489
496490 The ValuesView class implements the Sequence Abstract Base Class.
497491 """
498- def __init__ (self , sorted_dict ):
499- """
500- Initialize a ValuesView from a SortedDict container as *sorted_dict*.
501- """
502- self ._dict = sorted_dict
503- self ._list = sorted_dict ._list
504- if hexversion < 0x03000000 :
492+ if hexversion < 0x03000000 :
493+ def __init__ (self , sorted_dict ):
494+ """
495+ Initialize a ValuesView from a SortedDict container as
496+ *sorted_dict*.
497+ """
498+ self ._dict = sorted_dict
499+ self ._list = sorted_dict ._list
505500 self ._view = sorted_dict ._dict .viewvalues ()
506- else :
501+ else :
502+ def __init__ (self , sorted_dict ):
503+ """
504+ Initialize a ValuesView from a SortedDict container as
505+ *sorted_dict*.
506+ """
507+ self ._dict = sorted_dict
508+ self ._list = sorted_dict ._list
507509 self ._view = sorted_dict ._dict .values ()
508510 def __len__ (self ):
509511 """Return the number of entries in the dictionary."""
@@ -556,12 +558,13 @@ def index(self, value):
556558 return idx
557559 else :
558560 raise ValueError ('{0} is not in dict' .format (repr (value )))
559- def count (self , value ):
560- """Return the number of occurrences of *value* in self."""
561- _dict = self ._dict
562- if hexversion < 0x03000000 :
563- return sum (1 for val in _dict .itervalues () if val == value )
564- else :
561+ if hexversion < 0x03000000 :
562+ def count (self , value ):
563+ """Return the number of occurrences of *value* in self."""
564+ return sum (1 for val in self ._dict .itervalues () if val == value )
565+ else :
566+ def count (self , value ):
567+ """Return the number of occurrences of *value* in self."""
565568 return sum (1 for val in _dict .values () if val == value )
566569 def __lt__ (self , that ):
567570 raise TypeError
@@ -593,15 +596,23 @@ class ItemsView(AbstractItemsView, Set, Sequence):
593596 However, the set-like operations (``&``, ``|``, ``-``, ``^``) will only
594597 operate correctly if all of the dictionary's values are hashable.
595598 """
596- def __init__ (self , sorted_dict ):
597- """
598- Initialize an ItemsView from a SortedDict container as *sorted_dict*.
599- """
600- self ._dict = sorted_dict
601- self ._list = sorted_dict ._list
602- if hexversion < 0x03000000 :
599+ if hexversion < 0x03000000 :
600+ def __init__ (self , sorted_dict ):
601+ """
602+ Initialize an ItemsView from a SortedDict container as
603+ *sorted_dict*.
604+ """
605+ self ._dict = sorted_dict
606+ self ._list = sorted_dict ._list
603607 self ._view = sorted_dict ._dict .viewitems ()
604- else :
608+ else :
609+ def __init__ (self , sorted_dict ):
610+ """
611+ Initialize an ItemsView from a SortedDict container as
612+ *sorted_dict*.
613+ """
614+ self ._dict = sorted_dict
615+ self ._list = sorted_dict ._list
605616 self ._view = sorted_dict ._dict .items ()
606617 def __len__ (self ):
607618 """Return the number of entries in the dictionary."""
@@ -687,15 +698,17 @@ def __sub__(self, that):
687698 def __xor__ (self , that ):
688699 """Return a SortedSet of the symmetric difference of self and *that*."""
689700 return SortedSet (self ._view ^ that )
690- def isdisjoint ( self , that ) :
691- """Return True if and only if * that* is disjoint with self."""
692- if hexversion < 0x03000000 :
701+ if hexversion < 0x03000000 :
702+ def isdisjoint ( self , that ):
703+ """Return True if and only if *that* is disjoint with self."""
693704 _dict = self ._dict
694705 for key , value in that :
695706 if key in _dict and _dict [key ] == value :
696707 return False
697708 return True
698- else :
709+ else :
710+ def isdisjoint (self , that ):
711+ """Return True if and only if *that* is disjoint with self."""
699712 return self ._view .isdisjoint (that )
700713 @recursive_repr
701714 def __repr__ (self ):
0 commit comments