openmedialibrary_platform/Darwin/lib/python3.4/_collections_abc.py

735 lines
19 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00: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.
2014-09-30 16:15:32 +00:00
Unit tests are in test_collections.
2013-10-11 17:28:32 +00:00
"""
from abc import ABCMeta, abstractmethod
import sys
__all__ = ["Hashable", "Iterable", "Iterator",
"Sized", "Container", "Callable",
"Set", "MutableSet",
"Mapping", "MutableMapping",
"MappingView", "KeysView", "ItemsView", "ValuesView",
"Sequence", "MutableSequence",
2014-09-30 16:15:32 +00:00
"ByteString",
2013-10-11 17:28:32 +00:00
]
2014-09-30 16:15:32 +00:00
# This module has been renamed from collections.abc to _collections_abc to
# speed up interpreter startup. Some of the types such as MutableMapping are
# required early but collections module imports a lot of other modules.
# See issue #19218
__name__ = "collections.abc"
# Private list of types that we want to register with the various ABCs
# so that they will pass tests like:
# it = iter(somebytearray)
# assert isinstance(it, Iterable)
# Note: in other implementations, these types many not be distinct
# and they make have their own implementation specific types that
# are not included on this list.
bytes_iterator = type(iter(b''))
bytearray_iterator = type(iter(bytearray()))
#callable_iterator = ???
dict_keyiterator = type(iter({}.keys()))
dict_valueiterator = type(iter({}.values()))
dict_itemiterator = type(iter({}.items()))
list_iterator = type(iter([]))
list_reverseiterator = type(iter(reversed([])))
range_iterator = type(iter(range(0)))
set_iterator = type(iter(set()))
str_iterator = type(iter(""))
tuple_iterator = type(iter(()))
zip_iterator = type(iter(zip()))
## views ##
dict_keys = type({}.keys())
dict_values = type({}.values())
dict_items = type({}.items())
## misc ##
mappingproxy = type(type.__dict__)
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
### ONE-TRICK PONIES ###
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
class Hashable(metaclass=ABCMeta):
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
__slots__ = ()
2013-10-11 17:28:32 +00:00
@abstractmethod
def __hash__(self):
return 0
@classmethod
def __subclasshook__(cls, C):
if cls is Hashable:
2014-09-30 16:15:32 +00:00
for B in C.__mro__:
if "__hash__" in B.__dict__:
if B.__dict__["__hash__"]:
return True
break
2013-10-11 17:28:32 +00:00
return NotImplemented
2014-09-30 16:15:32 +00:00
class Iterable(metaclass=ABCMeta):
__slots__ = ()
2013-10-11 17:28:32 +00:00
@abstractmethod
def __iter__(self):
while False:
yield None
@classmethod
def __subclasshook__(cls, C):
if cls is Iterable:
2014-09-30 16:15:32 +00:00
if any("__iter__" in B.__dict__ for B in C.__mro__):
2013-10-11 17:28:32 +00:00
return True
return NotImplemented
class Iterator(Iterable):
2014-09-30 16:15:32 +00:00
__slots__ = ()
2013-10-11 17:28:32 +00:00
@abstractmethod
2014-09-30 16:15:32 +00:00
def __next__(self):
2013-10-11 17:28:32 +00:00
'Return the next item from the iterator. When exhausted, raise StopIteration'
raise StopIteration
def __iter__(self):
return self
@classmethod
def __subclasshook__(cls, C):
if cls is Iterator:
2014-09-30 16:15:32 +00:00
if (any("__next__" in B.__dict__ for B in C.__mro__) and
any("__iter__" in B.__dict__ for B in C.__mro__)):
2013-10-11 17:28:32 +00:00
return True
return NotImplemented
2014-09-30 16:15:32 +00:00
Iterator.register(bytes_iterator)
Iterator.register(bytearray_iterator)
#Iterator.register(callable_iterator)
Iterator.register(dict_keyiterator)
Iterator.register(dict_valueiterator)
Iterator.register(dict_itemiterator)
Iterator.register(list_iterator)
Iterator.register(list_reverseiterator)
Iterator.register(range_iterator)
Iterator.register(set_iterator)
Iterator.register(str_iterator)
Iterator.register(tuple_iterator)
Iterator.register(zip_iterator)
2013-10-11 17:28:32 +00:00
2014-09-30 16:15:32 +00:00
class Sized(metaclass=ABCMeta):
__slots__ = ()
2013-10-11 17:28:32 +00:00
@abstractmethod
def __len__(self):
return 0
@classmethod
def __subclasshook__(cls, C):
if cls is Sized:
2014-09-30 16:15:32 +00:00
if any("__len__" in B.__dict__ for B in C.__mro__):
2013-10-11 17:28:32 +00:00
return True
return NotImplemented
2014-09-30 16:15:32 +00:00
class Container(metaclass=ABCMeta):
__slots__ = ()
2013-10-11 17:28:32 +00:00
@abstractmethod
def __contains__(self, x):
return False
@classmethod
def __subclasshook__(cls, C):
if cls is Container:
2014-09-30 16:15:32 +00:00
if any("__contains__" in B.__dict__ for B in C.__mro__):
2013-10-11 17:28:32 +00:00
return True
return NotImplemented
2014-09-30 16:15:32 +00:00
class Callable(metaclass=ABCMeta):
__slots__ = ()
2013-10-11 17:28:32 +00:00
@abstractmethod
def __call__(self, *args, **kwds):
return False
@classmethod
def __subclasshook__(cls, C):
if cls is Callable:
2014-09-30 16:15:32 +00:00
if any("__call__" in B.__dict__ for B in C.__mro__):
2013-10-11 17:28:32 +00:00
return True
return NotImplemented
### SETS ###
class Set(Sized, Iterable, Container):
2014-09-30 16:15:32 +00:00
2013-10-11 17:28:32 +00: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
semantics are fixed), all you have to do is redefine __le__ and
then the other operations will automatically follow suit.
"""
2014-09-30 16:15:32 +00:00
__slots__ = ()
2013-10-11 17:28:32 +00:00
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)
def __gt__(self, other):
if not isinstance(other, Set):
return NotImplemented
2014-09-30 16:15:32 +00:00
return other.__lt__(self)
2013-10-11 17:28:32 +00:00
def __ge__(self, other):
if not isinstance(other, Set):
return NotImplemented
2014-09-30 16:15:32 +00:00
return other.__le__(self)
2013-10-11 17:28:32 +00:00
def __eq__(self, other):
if not isinstance(other, Set):
return NotImplemented
return len(self) == len(other) and self.__le__(other)
def __ne__(self, other):
return not (self == other)
@classmethod
def _from_iterable(cls, it):
'''Construct an instance of the class from any iterable input.
Must override this method if the class constructor signature
does not accept an iterable for an input.
'''
return cls(it)
def __and__(self, other):
if not isinstance(other, Iterable):
return NotImplemented
return self._from_iterable(value for value in other if value in self)
def isdisjoint(self, other):
'Return True if two sets have a null intersection.'
for value in other:
if value in self:
return False
return True
def __or__(self, other):
if not isinstance(other, Iterable):
return NotImplemented
chain = (e for s in (self, other) for e in s)
return self._from_iterable(chain)
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)
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)
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.
"""
2014-09-30 16:15:32 +00:00
MAX = sys.maxsize
2013-10-11 17:28:32 +00:00
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):
"""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.
"""
2014-09-30 16:15:32 +00:00
__slots__ = ()
2013-10-11 17:28:32 +00:00
@abstractmethod
def add(self, value):
"""Add an element."""
raise NotImplementedError
@abstractmethod
def discard(self, value):
"""Remove an element. Do not raise an exception if absent."""
raise NotImplementedError
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)
def pop(self):
"""Return the popped value. Raise KeyError if empty."""
it = iter(self)
try:
value = next(it)
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
def __iand__(self, it):
for value in (self - it):
self.discard(value)
return self
def __ixor__(self, it):
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)
return self
def __isub__(self, it):
if it is self:
self.clear()
else:
for value in it:
self.discard(value)
return self
MutableSet.register(set)
### MAPPINGS ###
class Mapping(Sized, Iterable, Container):
2014-09-30 16:15:32 +00:00
__slots__ = ()
2013-10-11 17:28:32 +00: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__.
"""
@abstractmethod
def __getitem__(self, key):
raise KeyError
def get(self, key, default=None):
'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.'
try:
return self[key]
except KeyError:
return default
def __contains__(self, key):
try:
self[key]
except KeyError:
return False
else:
return True
def keys(self):
2014-09-30 16:15:32 +00:00
"D.keys() -> a set-like object providing a view on D's keys"
return KeysView(self)
2013-10-11 17:28:32 +00:00
def items(self):
2014-09-30 16:15:32 +00:00
"D.items() -> a set-like object providing a view on D's items"
return ItemsView(self)
2013-10-11 17:28:32 +00:00
def values(self):
2014-09-30 16:15:32 +00:00
"D.values() -> an object providing a view on D's values"
return ValuesView(self)
2013-10-11 17:28:32 +00:00
def __eq__(self, other):
if not isinstance(other, Mapping):
return NotImplemented
return dict(self.items()) == dict(other.items())
def __ne__(self, other):
return not (self == other)
2014-09-30 16:15:32 +00:00
Mapping.register(mappingproxy)
2013-10-11 17:28:32 +00:00
class MappingView(Sized):
def __init__(self, mapping):
self._mapping = mapping
def __len__(self):
return len(self._mapping)
def __repr__(self):
return '{0.__class__.__name__}({0._mapping!r})'.format(self)
class KeysView(MappingView, Set):
@classmethod
def _from_iterable(self, it):
return set(it)
def __contains__(self, key):
return key in self._mapping
def __iter__(self):
2014-09-30 16:15:32 +00:00
yield from self._mapping
KeysView.register(dict_keys)
2013-10-11 17:28:32 +00:00
class ItemsView(MappingView, Set):
@classmethod
def _from_iterable(self, it):
return set(it)
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])
2014-09-30 16:15:32 +00:00
ItemsView.register(dict_items)
2013-10-11 17:28:32 +00:00
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]
2014-09-30 16:15:32 +00:00
ValuesView.register(dict_values)
2013-10-11 17:28:32 +00:00
class MutableMapping(Mapping):
2014-09-30 16:15:32 +00:00
__slots__ = ()
2013-10-11 17:28:32 +00: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__.
"""
@abstractmethod
def __setitem__(self, key, value):
raise KeyError
@abstractmethod
def __delitem__(self, key):
raise KeyError
__marker = object()
def pop(self, key, default=__marker):
'''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.
'''
try:
value = self[key]
except KeyError:
if default is self.__marker:
raise
return default
else:
del self[key]
return value
def popitem(self):
'''D.popitem() -> (k, v), remove and return some (key, value) pair
as a 2-tuple; but raise KeyError if D is empty.
'''
try:
key = next(iter(self))
except StopIteration:
raise KeyError
value = self[key]
del self[key]
return key, value
def clear(self):
'D.clear() -> None. Remove all items from D.'
try:
while True:
self.popitem()
except KeyError:
pass
def update(*args, **kwds):
''' 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
'''
if len(args) > 2:
raise TypeError("update() takes at most 2 positional "
"arguments ({} given)".format(len(args)))
elif not args:
raise TypeError("update() takes at least 1 argument (0 given)")
self = args[0]
other = args[1] if len(args) >= 2 else ()
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
for key, value in kwds.items():
self[key] = value
def setdefault(self, key, default=None):
'D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D'
try:
return self[key]
except KeyError:
self[key] = default
return default
MutableMapping.register(dict)
### SEQUENCES ###
class Sequence(Sized, Iterable, Container):
2014-09-30 16:15:32 +00:00
2013-10-11 17:28:32 +00:00
"""All the operations on a read-only sequence.
Concrete subclasses must override __new__ or __init__,
__getitem__, and __len__.
"""
2014-09-30 16:15:32 +00:00
__slots__ = ()
2013-10-11 17:28:32 +00:00
@abstractmethod
def __getitem__(self, index):
raise IndexError
def __iter__(self):
i = 0
try:
while True:
v = self[i]
yield v
i += 1
except IndexError:
return
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):
'''S.index(value) -> integer -- return first index of value.
Raises ValueError if the value is not present.
'''
for i, v in enumerate(self):
if v == value:
return i
raise ValueError
def count(self, value):
'S.count(value) -> integer -- return number of occurrences of value'
return sum(1 for v in self if v == value)
Sequence.register(tuple)
2014-09-30 16:15:32 +00:00
Sequence.register(str)
Sequence.register(range)
Sequence.register(memoryview)
class ByteString(Sequence):
"""This unifies bytes and bytearray.
XXX Should add all their methods.
"""
__slots__ = ()
ByteString.register(bytes)
ByteString.register(bytearray)
2013-10-11 17:28:32 +00:00
class MutableSequence(Sequence):
2014-09-30 16:15:32 +00:00
__slots__ = ()
"""All the operations on a read-write sequence.
2013-10-11 17:28:32 +00:00
Concrete subclasses must provide __new__ or __init__,
__getitem__, __setitem__, __delitem__, __len__, and insert().
"""
@abstractmethod
def __setitem__(self, index, value):
raise IndexError
@abstractmethod
def __delitem__(self, index):
raise IndexError
@abstractmethod
def insert(self, index, value):
2014-09-30 16:15:32 +00:00
'S.insert(index, value) -- insert value before index'
2013-10-11 17:28:32 +00:00
raise IndexError
def append(self, value):
2014-09-30 16:15:32 +00:00
'S.append(value) -- append value to the end of the sequence'
2013-10-11 17:28:32 +00:00
self.insert(len(self), value)
2014-09-30 16:15:32 +00:00
def clear(self):
'S.clear() -> None -- remove all items from S'
try:
while True:
self.pop()
except IndexError:
pass
2013-10-11 17:28:32 +00:00
def reverse(self):
'S.reverse() -- reverse *IN PLACE*'
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):
'S.extend(iterable) -- extend sequence by appending elements from the iterable'
for v in values:
self.append(v)
def pop(self, index=-1):
'''S.pop([index]) -> item -- remove and return item at index (default last).
Raise IndexError if list is empty or index is out of range.
'''
v = self[index]
del self[index]
return v
def remove(self, value):
'''S.remove(value) -- remove first occurrence of value.
Raise ValueError if the value is not present.
'''
del self[self.index(value)]
def __iadd__(self, values):
self.extend(values)
return self
MutableSequence.register(list)
2014-09-30 16:15:32 +00:00
MutableSequence.register(bytearray) # Multiply inheriting, see ByteString