2007-11-21 20:55:51 -04:00
|
|
|
# Copyright 2007 Google, Inc. All Rights Reserved.
|
|
|
|
# Licensed to PSF under a Contributor Agreement.
|
|
|
|
|
|
|
|
"""Abstract Base Classes (ABCs) for collections, according to PEP 3119.
|
|
|
|
|
|
|
|
DON'T USE THIS MODULE DIRECTLY! The classes here should be imported
|
|
|
|
via collections; they are defined here only to alleviate certain
|
|
|
|
bootstrapping issues. Unit tests are in test_collections.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from abc import ABCMeta, abstractmethod
|
2008-06-23 00:29:28 -03:00
|
|
|
import sys
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
__all__ = ["Hashable", "Iterable", "Iterator",
|
|
|
|
"Sized", "Container", "Callable",
|
|
|
|
"Set", "MutableSet",
|
|
|
|
"Mapping", "MutableMapping",
|
|
|
|
"MappingView", "KeysView", "ItemsView", "ValuesView",
|
|
|
|
"Sequence", "MutableSequence",
|
|
|
|
]
|
|
|
|
|
|
|
|
### ONE-TRICK PONIES ###
|
|
|
|
|
2010-03-08 11:20:28 -04:00
|
|
|
def _hasattr(C, attr):
|
|
|
|
try:
|
|
|
|
return any(attr in B.__dict__ for B in C.__mro__)
|
|
|
|
except AttributeError:
|
|
|
|
# Old-style class
|
|
|
|
return hasattr(C, attr)
|
|
|
|
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
class Hashable:
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __hash__(self):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __subclasshook__(cls, C):
|
|
|
|
if cls is Hashable:
|
2010-03-08 11:20:28 -04:00
|
|
|
try:
|
|
|
|
for B in C.__mro__:
|
|
|
|
if "__hash__" in B.__dict__:
|
|
|
|
if B.__dict__["__hash__"]:
|
|
|
|
return True
|
|
|
|
break
|
|
|
|
except AttributeError:
|
|
|
|
# Old-style class
|
|
|
|
if getattr(C, "__hash__", None):
|
|
|
|
return True
|
2007-11-21 20:55:51 -04:00
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
|
|
|
|
class Iterable:
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __iter__(self):
|
|
|
|
while False:
|
|
|
|
yield None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __subclasshook__(cls, C):
|
|
|
|
if cls is Iterable:
|
2010-03-08 11:20:28 -04:00
|
|
|
if _hasattr(C, "__iter__"):
|
2007-11-21 20:55:51 -04:00
|
|
|
return True
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
Iterable.register(str)
|
|
|
|
|
|
|
|
|
2008-02-08 21:18:42 -04:00
|
|
|
class Iterator(Iterable):
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
@abstractmethod
|
2009-01-28 19:02:26 -04:00
|
|
|
def next(self):
|
2013-03-24 18:54:25 -03:00
|
|
|
'Return the next item from the iterator. When exhausted, raise StopIteration'
|
2007-11-21 20:55:51 -04:00
|
|
|
raise StopIteration
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __subclasshook__(cls, C):
|
|
|
|
if cls is Iterator:
|
2010-11-29 21:18:17 -04:00
|
|
|
if _hasattr(C, "next") and _hasattr(C, "__iter__"):
|
2007-11-21 20:55:51 -04:00
|
|
|
return True
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
|
|
|
|
class Sized:
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __len__(self):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __subclasshook__(cls, C):
|
|
|
|
if cls is Sized:
|
2010-03-08 11:20:28 -04:00
|
|
|
if _hasattr(C, "__len__"):
|
2007-11-21 20:55:51 -04:00
|
|
|
return True
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
|
|
|
|
class Container:
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __contains__(self, x):
|
|
|
|
return False
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __subclasshook__(cls, C):
|
|
|
|
if cls is Container:
|
2010-03-08 11:20:28 -04:00
|
|
|
if _hasattr(C, "__contains__"):
|
2007-11-21 20:55:51 -04:00
|
|
|
return True
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
|
|
|
|
class Callable:
|
|
|
|
__metaclass__ = ABCMeta
|
|
|
|
|
|
|
|
@abstractmethod
|
2008-03-03 18:19:58 -04:00
|
|
|
def __call__(self, *args, **kwds):
|
2007-11-21 20:55:51 -04:00
|
|
|
return False
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __subclasshook__(cls, C):
|
|
|
|
if cls is Callable:
|
2010-03-08 11:20:28 -04:00
|
|
|
if _hasattr(C, "__call__"):
|
2007-11-21 20:55:51 -04:00
|
|
|
return True
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
|
|
|
|
### SETS ###
|
|
|
|
|
|
|
|
|
2008-02-08 21:18:42 -04:00
|
|
|
class Set(Sized, Iterable, Container):
|
2007-11-21 20:55:51 -04:00
|
|
|
"""A set is a finite, iterable container.
|
|
|
|
|
|
|
|
This class provides concrete generic implementations of all
|
|
|
|
methods except for __contains__, __iter__ and __len__.
|
|
|
|
|
|
|
|
To override the comparisons (presumably for speed, as the
|
2014-07-02 20:30:52 -03:00
|
|
|
semantics are fixed), redefine __le__ and __ge__,
|
2007-11-21 20:55:51 -04:00
|
|
|
then the other operations will automatically follow suit.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __le__(self, other):
|
|
|
|
if not isinstance(other, Set):
|
|
|
|
return NotImplemented
|
|
|
|
if len(self) > len(other):
|
|
|
|
return False
|
|
|
|
for elem in self:
|
|
|
|
if elem not in other:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
if not isinstance(other, Set):
|
|
|
|
return NotImplemented
|
|
|
|
return len(self) < len(other) and self.__le__(other)
|
|
|
|
|
2008-02-08 19:34:21 -04:00
|
|
|
def __gt__(self, other):
|
|
|
|
if not isinstance(other, Set):
|
|
|
|
return NotImplemented
|
2014-05-26 02:13:41 -03:00
|
|
|
return len(self) > len(other) and self.__ge__(other)
|
2008-02-08 19:34:21 -04:00
|
|
|
|
|
|
|
def __ge__(self, other):
|
|
|
|
if not isinstance(other, Set):
|
|
|
|
return NotImplemented
|
2014-05-26 02:13:41 -03:00
|
|
|
if len(self) < len(other):
|
|
|
|
return False
|
|
|
|
for elem in other:
|
|
|
|
if elem not in self:
|
|
|
|
return False
|
|
|
|
return True
|
2008-02-08 19:34:21 -04:00
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
def __eq__(self, other):
|
|
|
|
if not isinstance(other, Set):
|
|
|
|
return NotImplemented
|
|
|
|
return len(self) == len(other) and self.__le__(other)
|
|
|
|
|
2008-02-08 19:34:21 -04:00
|
|
|
def __ne__(self, other):
|
|
|
|
return not (self == other)
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
@classmethod
|
|
|
|
def _from_iterable(cls, it):
|
2008-02-06 23:10:33 -04:00
|
|
|
'''Construct an instance of the class from any iterable input.
|
|
|
|
|
|
|
|
Must override this method if the class constructor signature
|
2008-02-08 23:34:52 -04:00
|
|
|
does not accept an iterable for an input.
|
2008-02-06 23:10:33 -04:00
|
|
|
'''
|
2008-02-08 23:34:52 -04:00
|
|
|
return cls(it)
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def __and__(self, other):
|
|
|
|
if not isinstance(other, Iterable):
|
|
|
|
return NotImplemented
|
|
|
|
return self._from_iterable(value for value in other if value in self)
|
|
|
|
|
2014-05-26 02:13:41 -03:00
|
|
|
__rand__ = __and__
|
|
|
|
|
2008-01-29 20:01:07 -04:00
|
|
|
def isdisjoint(self, other):
|
2013-03-24 18:54:25 -03:00
|
|
|
'Return True if two sets have a null intersection.'
|
2008-01-29 20:01:07 -04:00
|
|
|
for value in other:
|
|
|
|
if value in self:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
def __or__(self, other):
|
|
|
|
if not isinstance(other, Iterable):
|
|
|
|
return NotImplemented
|
2008-03-03 18:04:55 -04:00
|
|
|
chain = (e for s in (self, other) for e in s)
|
|
|
|
return self._from_iterable(chain)
|
2007-11-21 20:55:51 -04:00
|
|
|
|
2014-05-26 02:13:41 -03:00
|
|
|
__ror__ = __or__
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
def __sub__(self, other):
|
|
|
|
if not isinstance(other, Set):
|
|
|
|
if not isinstance(other, Iterable):
|
|
|
|
return NotImplemented
|
|
|
|
other = self._from_iterable(other)
|
|
|
|
return self._from_iterable(value for value in self
|
|
|
|
if value not in other)
|
|
|
|
|
2014-05-26 02:13:41 -03:00
|
|
|
def __rsub__(self, other):
|
|
|
|
if not isinstance(other, Set):
|
|
|
|
if not isinstance(other, Iterable):
|
|
|
|
return NotImplemented
|
|
|
|
other = self._from_iterable(other)
|
|
|
|
return self._from_iterable(value for value in other
|
|
|
|
if value not in self)
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
def __xor__(self, other):
|
|
|
|
if not isinstance(other, Set):
|
|
|
|
if not isinstance(other, Iterable):
|
|
|
|
return NotImplemented
|
|
|
|
other = self._from_iterable(other)
|
|
|
|
return (self - other) | (other - self)
|
|
|
|
|
2014-05-26 02:13:41 -03:00
|
|
|
__rxor__ = __xor__
|
|
|
|
|
2008-08-11 12:45:58 -03:00
|
|
|
# Sets are not hashable by default, but subclasses can change this
|
|
|
|
__hash__ = None
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
def _hash(self):
|
|
|
|
"""Compute the hash value of a set.
|
|
|
|
|
|
|
|
Note that we don't define __hash__: not all sets are hashable.
|
|
|
|
But if you define a hashable set type, its __hash__ should
|
|
|
|
call this function.
|
|
|
|
|
|
|
|
This must be compatible __eq__.
|
|
|
|
|
|
|
|
All sets ought to compare equal if they contain the same
|
|
|
|
elements, regardless of how they are implemented, and
|
|
|
|
regardless of the order of the elements; so there's not much
|
|
|
|
freedom for __eq__ or __hash__. We match the algorithm used
|
|
|
|
by the built-in frozenset type.
|
|
|
|
"""
|
|
|
|
MAX = sys.maxint
|
|
|
|
MASK = 2 * MAX + 1
|
|
|
|
n = len(self)
|
|
|
|
h = 1927868237 * (n + 1)
|
|
|
|
h &= MASK
|
|
|
|
for x in self:
|
|
|
|
hx = hash(x)
|
|
|
|
h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
|
|
|
|
h &= MASK
|
|
|
|
h = h * 69069 + 907133923
|
|
|
|
h &= MASK
|
|
|
|
if h > MAX:
|
|
|
|
h -= MASK + 1
|
|
|
|
if h == -1:
|
|
|
|
h = 590923713
|
|
|
|
return h
|
|
|
|
|
|
|
|
Set.register(frozenset)
|
|
|
|
|
|
|
|
|
|
|
|
class MutableSet(Set):
|
2013-03-24 18:54:25 -03:00
|
|
|
"""A mutable set is a finite, iterable container.
|
|
|
|
|
|
|
|
This class provides concrete generic implementations of all
|
|
|
|
methods except for __contains__, __iter__, __len__,
|
|
|
|
add(), and discard().
|
|
|
|
|
|
|
|
To override the comparisons (presumably for speed, as the
|
|
|
|
semantics are fixed), all you have to do is redefine __le__ and
|
|
|
|
then the other operations will automatically follow suit.
|
|
|
|
"""
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def add(self, value):
|
2009-01-13 05:08:32 -04:00
|
|
|
"""Add an element."""
|
2007-11-21 20:55:51 -04:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def discard(self, value):
|
2009-01-13 05:08:32 -04:00
|
|
|
"""Remove an element. Do not raise an exception if absent."""
|
2007-11-21 20:55:51 -04:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2008-01-29 20:08:31 -04:00
|
|
|
def remove(self, value):
|
|
|
|
"""Remove an element. If not a member, raise a KeyError."""
|
|
|
|
if value not in self:
|
|
|
|
raise KeyError(value)
|
|
|
|
self.discard(value)
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
def pop(self):
|
|
|
|
"""Return the popped value. Raise KeyError if empty."""
|
|
|
|
it = iter(self)
|
|
|
|
try:
|
2009-01-28 19:02:26 -04:00
|
|
|
value = next(it)
|
2007-11-21 20:55:51 -04:00
|
|
|
except StopIteration:
|
|
|
|
raise KeyError
|
|
|
|
self.discard(value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
"""This is slow (creates N new iterators!) but effective."""
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
self.pop()
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __ior__(self, it):
|
|
|
|
for value in it:
|
|
|
|
self.add(value)
|
|
|
|
return self
|
|
|
|
|
2009-04-01 15:50:56 -03:00
|
|
|
def __iand__(self, it):
|
|
|
|
for value in (self - it):
|
|
|
|
self.discard(value)
|
2007-11-21 20:55:51 -04:00
|
|
|
return self
|
|
|
|
|
|
|
|
def __ixor__(self, it):
|
2010-08-24 18:09:30 -03:00
|
|
|
if it is self:
|
|
|
|
self.clear()
|
|
|
|
else:
|
|
|
|
if not isinstance(it, Set):
|
|
|
|
it = self._from_iterable(it)
|
|
|
|
for value in it:
|
|
|
|
if value in self:
|
|
|
|
self.discard(value)
|
|
|
|
else:
|
|
|
|
self.add(value)
|
2008-01-30 21:42:11 -04:00
|
|
|
return self
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def __isub__(self, it):
|
2010-08-24 18:09:30 -03:00
|
|
|
if it is self:
|
|
|
|
self.clear()
|
|
|
|
else:
|
|
|
|
for value in it:
|
|
|
|
self.discard(value)
|
2007-11-21 20:55:51 -04:00
|
|
|
return self
|
|
|
|
|
|
|
|
MutableSet.register(set)
|
|
|
|
|
|
|
|
|
|
|
|
### MAPPINGS ###
|
|
|
|
|
|
|
|
|
2008-02-08 21:18:42 -04:00
|
|
|
class Mapping(Sized, Iterable, Container):
|
2007-11-21 20:55:51 -04:00
|
|
|
|
2013-03-24 18:54:25 -03:00
|
|
|
"""A Mapping is a generic container for associating key/value
|
|
|
|
pairs.
|
|
|
|
|
|
|
|
This class provides concrete generic implementations of all
|
|
|
|
methods except for __getitem__, __iter__, and __len__.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
@abstractmethod
|
|
|
|
def __getitem__(self, key):
|
|
|
|
raise KeyError
|
|
|
|
|
|
|
|
def get(self, key, default=None):
|
2013-03-24 18:54:25 -03:00
|
|
|
'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.'
|
2007-11-21 20:55:51 -04:00
|
|
|
try:
|
|
|
|
return self[key]
|
|
|
|
except KeyError:
|
|
|
|
return default
|
|
|
|
|
|
|
|
def __contains__(self, key):
|
|
|
|
try:
|
|
|
|
self[key]
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2008-06-07 14:03:28 -03:00
|
|
|
def iterkeys(self):
|
2013-03-24 18:54:25 -03:00
|
|
|
'D.iterkeys() -> an iterator over the keys of D'
|
2008-06-07 14:03:28 -03:00
|
|
|
return iter(self)
|
|
|
|
|
|
|
|
def itervalues(self):
|
2013-03-24 18:54:25 -03:00
|
|
|
'D.itervalues() -> an iterator over the values of D'
|
2008-06-07 14:03:28 -03:00
|
|
|
for key in self:
|
|
|
|
yield self[key]
|
|
|
|
|
|
|
|
def iteritems(self):
|
2013-03-24 18:54:25 -03:00
|
|
|
'D.iteritems() -> an iterator over the (key, value) items of D'
|
2008-06-07 14:03:28 -03:00
|
|
|
for key in self:
|
|
|
|
yield (key, self[key])
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
def keys(self):
|
2013-03-24 18:54:25 -03:00
|
|
|
"D.keys() -> list of D's keys"
|
2008-06-07 14:03:28 -03:00
|
|
|
return list(self)
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def items(self):
|
2013-03-24 18:54:25 -03:00
|
|
|
"D.items() -> list of D's (key, value) pairs, as 2-tuples"
|
2008-06-07 14:03:28 -03:00
|
|
|
return [(key, self[key]) for key in self]
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def values(self):
|
2013-03-24 18:54:25 -03:00
|
|
|
"D.values() -> list of D's values"
|
2008-06-07 14:03:28 -03:00
|
|
|
return [self[key] for key in self]
|
2007-11-21 20:55:51 -04:00
|
|
|
|
2008-08-11 12:45:58 -03:00
|
|
|
# Mappings are not hashable by default, but subclasses can change this
|
|
|
|
__hash__ = None
|
|
|
|
|
2008-02-05 21:49:00 -04:00
|
|
|
def __eq__(self, other):
|
2010-05-21 17:51:45 -03:00
|
|
|
if not isinstance(other, Mapping):
|
|
|
|
return NotImplemented
|
|
|
|
return dict(self.items()) == dict(other.items())
|
2008-02-05 21:49:00 -04:00
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not (self == other)
|
2007-11-21 20:55:51 -04:00
|
|
|
|
2008-02-08 21:18:42 -04:00
|
|
|
class MappingView(Sized):
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def __init__(self, mapping):
|
|
|
|
self._mapping = mapping
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._mapping)
|
|
|
|
|
2009-02-27 04:09:47 -04:00
|
|
|
def __repr__(self):
|
|
|
|
return '{0.__class__.__name__}({0._mapping!r})'.format(self)
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
class KeysView(MappingView, Set):
|
|
|
|
|
2010-08-22 05:01:58 -03:00
|
|
|
@classmethod
|
|
|
|
def _from_iterable(self, it):
|
|
|
|
return set(it)
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
def __contains__(self, key):
|
|
|
|
return key in self._mapping
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for key in self._mapping:
|
|
|
|
yield key
|
|
|
|
|
|
|
|
|
|
|
|
class ItemsView(MappingView, Set):
|
|
|
|
|
2010-08-22 05:01:58 -03:00
|
|
|
@classmethod
|
|
|
|
def _from_iterable(self, it):
|
|
|
|
return set(it)
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
def __contains__(self, item):
|
|
|
|
key, value = item
|
|
|
|
try:
|
|
|
|
v = self._mapping[key]
|
|
|
|
except KeyError:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return v == value
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for key in self._mapping:
|
|
|
|
yield (key, self._mapping[key])
|
|
|
|
|
|
|
|
|
|
|
|
class ValuesView(MappingView):
|
|
|
|
|
|
|
|
def __contains__(self, value):
|
|
|
|
for key in self._mapping:
|
|
|
|
if value == self._mapping[key]:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for key in self._mapping:
|
|
|
|
yield self._mapping[key]
|
|
|
|
|
|
|
|
|
|
|
|
class MutableMapping(Mapping):
|
|
|
|
|
2013-03-24 18:54:25 -03:00
|
|
|
"""A MutableMapping is a generic container for associating
|
|
|
|
key/value pairs.
|
|
|
|
|
|
|
|
This class provides concrete generic implementations of all
|
|
|
|
methods except for __getitem__, __setitem__, __delitem__,
|
|
|
|
__iter__, and __len__.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
@abstractmethod
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
raise KeyError
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __delitem__(self, key):
|
|
|
|
raise KeyError
|
|
|
|
|
|
|
|
__marker = object()
|
|
|
|
|
|
|
|
def pop(self, key, default=__marker):
|
2013-03-24 18:54:25 -03:00
|
|
|
'''D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
|
|
|
|
If key is not found, d is returned if given, otherwise KeyError is raised.
|
|
|
|
'''
|
2007-11-21 20:55:51 -04:00
|
|
|
try:
|
|
|
|
value = self[key]
|
|
|
|
except KeyError:
|
|
|
|
if default is self.__marker:
|
|
|
|
raise
|
|
|
|
return default
|
|
|
|
else:
|
|
|
|
del self[key]
|
|
|
|
return value
|
|
|
|
|
|
|
|
def popitem(self):
|
2013-03-24 18:54:25 -03:00
|
|
|
'''D.popitem() -> (k, v), remove and return some (key, value) pair
|
|
|
|
as a 2-tuple; but raise KeyError if D is empty.
|
|
|
|
'''
|
2007-11-21 20:55:51 -04:00
|
|
|
try:
|
|
|
|
key = next(iter(self))
|
|
|
|
except StopIteration:
|
|
|
|
raise KeyError
|
|
|
|
value = self[key]
|
|
|
|
del self[key]
|
|
|
|
return key, value
|
|
|
|
|
|
|
|
def clear(self):
|
2013-03-24 18:54:25 -03:00
|
|
|
'D.clear() -> None. Remove all items from D.'
|
2007-11-21 20:55:51 -04:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
self.popitem()
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2010-07-11 16:17:28 -03:00
|
|
|
def update(*args, **kwds):
|
2013-03-24 18:54:25 -03:00
|
|
|
''' D.update([E, ]**F) -> None. Update D from mapping/iterable E and F.
|
|
|
|
If E present and has a .keys() method, does: for k in E: D[k] = E[k]
|
|
|
|
If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
|
|
|
|
In either case, this is followed by: for k, v in F.items(): D[k] = v
|
|
|
|
'''
|
2014-11-27 13:02:56 -04:00
|
|
|
if not args:
|
|
|
|
raise TypeError("descriptor 'update' of 'MutableMapping' object "
|
|
|
|
"needs an argument")
|
2010-07-11 16:17:28 -03:00
|
|
|
self = args[0]
|
2014-11-27 13:02:56 -04:00
|
|
|
args = args[1:]
|
|
|
|
if len(args) > 1:
|
|
|
|
raise TypeError('update expected at most 1 arguments, got %d' %
|
|
|
|
len(args))
|
|
|
|
if args:
|
|
|
|
other = args[0]
|
|
|
|
if isinstance(other, Mapping):
|
|
|
|
for key in other:
|
|
|
|
self[key] = other[key]
|
|
|
|
elif hasattr(other, "keys"):
|
|
|
|
for key in other.keys():
|
|
|
|
self[key] = other[key]
|
|
|
|
else:
|
|
|
|
for key, value in other:
|
|
|
|
self[key] = value
|
2007-11-21 20:55:51 -04:00
|
|
|
for key, value in kwds.items():
|
|
|
|
self[key] = value
|
|
|
|
|
2008-02-05 21:49:00 -04:00
|
|
|
def setdefault(self, key, default=None):
|
2013-03-24 18:54:25 -03:00
|
|
|
'D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D'
|
2008-02-05 21:49:00 -04:00
|
|
|
try:
|
|
|
|
return self[key]
|
|
|
|
except KeyError:
|
|
|
|
self[key] = default
|
|
|
|
return default
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
MutableMapping.register(dict)
|
|
|
|
|
|
|
|
|
|
|
|
### SEQUENCES ###
|
|
|
|
|
|
|
|
|
2008-02-08 21:18:42 -04:00
|
|
|
class Sequence(Sized, Iterable, Container):
|
2007-11-21 20:55:51 -04:00
|
|
|
"""All the operations on a read-only sequence.
|
|
|
|
|
|
|
|
Concrete subclasses must override __new__ or __init__,
|
|
|
|
__getitem__, and __len__.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __getitem__(self, index):
|
|
|
|
raise IndexError
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
i = 0
|
2008-02-08 19:02:27 -04:00
|
|
|
try:
|
|
|
|
while True:
|
2007-11-21 20:55:51 -04:00
|
|
|
v = self[i]
|
2008-02-08 19:02:27 -04:00
|
|
|
yield v
|
|
|
|
i += 1
|
|
|
|
except IndexError:
|
|
|
|
return
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
def __contains__(self, value):
|
|
|
|
for v in self:
|
|
|
|
if v == value:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __reversed__(self):
|
|
|
|
for i in reversed(range(len(self))):
|
|
|
|
yield self[i]
|
|
|
|
|
|
|
|
def index(self, value):
|
2013-03-24 18:54:25 -03:00
|
|
|
'''S.index(value) -> integer -- return first index of value.
|
|
|
|
Raises ValueError if the value is not present.
|
|
|
|
'''
|
2007-11-21 20:55:51 -04:00
|
|
|
for i, v in enumerate(self):
|
|
|
|
if v == value:
|
|
|
|
return i
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
def count(self, value):
|
2013-03-24 18:54:25 -03:00
|
|
|
'S.count(value) -> integer -- return number of occurrences of value'
|
2007-11-21 20:55:51 -04:00
|
|
|
return sum(1 for v in self if v == value)
|
|
|
|
|
|
|
|
Sequence.register(tuple)
|
|
|
|
Sequence.register(basestring)
|
|
|
|
Sequence.register(buffer)
|
2009-02-24 08:23:23 -04:00
|
|
|
Sequence.register(xrange)
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
class MutableSequence(Sequence):
|
|
|
|
|
2013-03-24 18:54:25 -03:00
|
|
|
"""All the operations on a read-only sequence.
|
|
|
|
|
|
|
|
Concrete subclasses must provide __new__ or __init__,
|
|
|
|
__getitem__, __setitem__, __delitem__, __len__, and insert().
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2007-11-21 20:55:51 -04:00
|
|
|
@abstractmethod
|
|
|
|
def __setitem__(self, index, value):
|
|
|
|
raise IndexError
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def __delitem__(self, index):
|
|
|
|
raise IndexError
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def insert(self, index, value):
|
2013-03-24 18:54:25 -03:00
|
|
|
'S.insert(index, object) -- insert object before index'
|
2007-11-21 20:55:51 -04:00
|
|
|
raise IndexError
|
|
|
|
|
|
|
|
def append(self, value):
|
2013-03-24 18:54:25 -03:00
|
|
|
'S.append(object) -- append object to the end of the sequence'
|
2007-11-21 20:55:51 -04:00
|
|
|
self.insert(len(self), value)
|
|
|
|
|
|
|
|
def reverse(self):
|
2013-03-24 18:54:25 -03:00
|
|
|
'S.reverse() -- reverse *IN PLACE*'
|
2007-11-21 20:55:51 -04:00
|
|
|
n = len(self)
|
|
|
|
for i in range(n//2):
|
|
|
|
self[i], self[n-i-1] = self[n-i-1], self[i]
|
|
|
|
|
|
|
|
def extend(self, values):
|
2013-03-24 18:54:25 -03:00
|
|
|
'S.extend(iterable) -- extend sequence by appending elements from the iterable'
|
2007-11-21 20:55:51 -04:00
|
|
|
for v in values:
|
|
|
|
self.append(v)
|
|
|
|
|
|
|
|
def pop(self, index=-1):
|
2013-03-24 18:54:25 -03:00
|
|
|
'''S.pop([index]) -> item -- remove and return item at index (default last).
|
|
|
|
Raise IndexError if list is empty or index is out of range.
|
|
|
|
'''
|
2007-11-21 20:55:51 -04:00
|
|
|
v = self[index]
|
|
|
|
del self[index]
|
|
|
|
return v
|
|
|
|
|
|
|
|
def remove(self, value):
|
2013-03-24 18:54:25 -03:00
|
|
|
'''S.remove(value) -- remove first occurrence of value.
|
|
|
|
Raise ValueError if the value is not present.
|
|
|
|
'''
|
2007-11-21 20:55:51 -04:00
|
|
|
del self[self.index(value)]
|
|
|
|
|
|
|
|
def __iadd__(self, values):
|
|
|
|
self.extend(values)
|
2009-05-18 12:51:59 -03:00
|
|
|
return self
|
2007-11-21 20:55:51 -04:00
|
|
|
|
|
|
|
MutableSequence.register(list)
|