Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/python27/lib64/python2....
File: _abcoll.py
# Copyright 2007 Google, Inc. All Rights Reserved.
[0] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[1] Fix | Delete
[2] Fix | Delete
"""Abstract Base Classes (ABCs) for collections, according to PEP 3119.
[3] Fix | Delete
[4] Fix | Delete
DON'T USE THIS MODULE DIRECTLY! The classes here should be imported
[5] Fix | Delete
via collections; they are defined here only to alleviate certain
[6] Fix | Delete
bootstrapping issues. Unit tests are in test_collections.
[7] Fix | Delete
"""
[8] Fix | Delete
[9] Fix | Delete
from abc import ABCMeta, abstractmethod
[10] Fix | Delete
import sys
[11] Fix | Delete
[12] Fix | Delete
__all__ = ["Hashable", "Iterable", "Iterator",
[13] Fix | Delete
"Sized", "Container", "Callable",
[14] Fix | Delete
"Set", "MutableSet",
[15] Fix | Delete
"Mapping", "MutableMapping",
[16] Fix | Delete
"MappingView", "KeysView", "ItemsView", "ValuesView",
[17] Fix | Delete
"Sequence", "MutableSequence",
[18] Fix | Delete
]
[19] Fix | Delete
[20] Fix | Delete
### ONE-TRICK PONIES ###
[21] Fix | Delete
[22] Fix | Delete
def _hasattr(C, attr):
[23] Fix | Delete
try:
[24] Fix | Delete
return any(attr in B.__dict__ for B in C.__mro__)
[25] Fix | Delete
except AttributeError:
[26] Fix | Delete
# Old-style class
[27] Fix | Delete
return hasattr(C, attr)
[28] Fix | Delete
[29] Fix | Delete
[30] Fix | Delete
class Hashable:
[31] Fix | Delete
__metaclass__ = ABCMeta
[32] Fix | Delete
[33] Fix | Delete
@abstractmethod
[34] Fix | Delete
def __hash__(self):
[35] Fix | Delete
return 0
[36] Fix | Delete
[37] Fix | Delete
@classmethod
[38] Fix | Delete
def __subclasshook__(cls, C):
[39] Fix | Delete
if cls is Hashable:
[40] Fix | Delete
try:
[41] Fix | Delete
for B in C.__mro__:
[42] Fix | Delete
if "__hash__" in B.__dict__:
[43] Fix | Delete
if B.__dict__["__hash__"]:
[44] Fix | Delete
return True
[45] Fix | Delete
break
[46] Fix | Delete
except AttributeError:
[47] Fix | Delete
# Old-style class
[48] Fix | Delete
if getattr(C, "__hash__", None):
[49] Fix | Delete
return True
[50] Fix | Delete
return NotImplemented
[51] Fix | Delete
[52] Fix | Delete
[53] Fix | Delete
class Iterable:
[54] Fix | Delete
__metaclass__ = ABCMeta
[55] Fix | Delete
[56] Fix | Delete
@abstractmethod
[57] Fix | Delete
def __iter__(self):
[58] Fix | Delete
while False:
[59] Fix | Delete
yield None
[60] Fix | Delete
[61] Fix | Delete
@classmethod
[62] Fix | Delete
def __subclasshook__(cls, C):
[63] Fix | Delete
if cls is Iterable:
[64] Fix | Delete
if _hasattr(C, "__iter__"):
[65] Fix | Delete
return True
[66] Fix | Delete
return NotImplemented
[67] Fix | Delete
[68] Fix | Delete
Iterable.register(str)
[69] Fix | Delete
[70] Fix | Delete
[71] Fix | Delete
class Iterator(Iterable):
[72] Fix | Delete
[73] Fix | Delete
@abstractmethod
[74] Fix | Delete
def next(self):
[75] Fix | Delete
'Return the next item from the iterator. When exhausted, raise StopIteration'
[76] Fix | Delete
raise StopIteration
[77] Fix | Delete
[78] Fix | Delete
def __iter__(self):
[79] Fix | Delete
return self
[80] Fix | Delete
[81] Fix | Delete
@classmethod
[82] Fix | Delete
def __subclasshook__(cls, C):
[83] Fix | Delete
if cls is Iterator:
[84] Fix | Delete
if _hasattr(C, "next") and _hasattr(C, "__iter__"):
[85] Fix | Delete
return True
[86] Fix | Delete
return NotImplemented
[87] Fix | Delete
[88] Fix | Delete
[89] Fix | Delete
class Sized:
[90] Fix | Delete
__metaclass__ = ABCMeta
[91] Fix | Delete
[92] Fix | Delete
@abstractmethod
[93] Fix | Delete
def __len__(self):
[94] Fix | Delete
return 0
[95] Fix | Delete
[96] Fix | Delete
@classmethod
[97] Fix | Delete
def __subclasshook__(cls, C):
[98] Fix | Delete
if cls is Sized:
[99] Fix | Delete
if _hasattr(C, "__len__"):
[100] Fix | Delete
return True
[101] Fix | Delete
return NotImplemented
[102] Fix | Delete
[103] Fix | Delete
[104] Fix | Delete
class Container:
[105] Fix | Delete
__metaclass__ = ABCMeta
[106] Fix | Delete
[107] Fix | Delete
@abstractmethod
[108] Fix | Delete
def __contains__(self, x):
[109] Fix | Delete
return False
[110] Fix | Delete
[111] Fix | Delete
@classmethod
[112] Fix | Delete
def __subclasshook__(cls, C):
[113] Fix | Delete
if cls is Container:
[114] Fix | Delete
if _hasattr(C, "__contains__"):
[115] Fix | Delete
return True
[116] Fix | Delete
return NotImplemented
[117] Fix | Delete
[118] Fix | Delete
[119] Fix | Delete
class Callable:
[120] Fix | Delete
__metaclass__ = ABCMeta
[121] Fix | Delete
[122] Fix | Delete
@abstractmethod
[123] Fix | Delete
def __call__(self, *args, **kwds):
[124] Fix | Delete
return False
[125] Fix | Delete
[126] Fix | Delete
@classmethod
[127] Fix | Delete
def __subclasshook__(cls, C):
[128] Fix | Delete
if cls is Callable:
[129] Fix | Delete
if _hasattr(C, "__call__"):
[130] Fix | Delete
return True
[131] Fix | Delete
return NotImplemented
[132] Fix | Delete
[133] Fix | Delete
[134] Fix | Delete
### SETS ###
[135] Fix | Delete
[136] Fix | Delete
[137] Fix | Delete
class Set(Sized, Iterable, Container):
[138] Fix | Delete
"""A set is a finite, iterable container.
[139] Fix | Delete
[140] Fix | Delete
This class provides concrete generic implementations of all
[141] Fix | Delete
methods except for __contains__, __iter__ and __len__.
[142] Fix | Delete
[143] Fix | Delete
To override the comparisons (presumably for speed, as the
[144] Fix | Delete
semantics are fixed), redefine __le__ and __ge__,
[145] Fix | Delete
then the other operations will automatically follow suit.
[146] Fix | Delete
"""
[147] Fix | Delete
[148] Fix | Delete
def __le__(self, other):
[149] Fix | Delete
if not isinstance(other, Set):
[150] Fix | Delete
return NotImplemented
[151] Fix | Delete
if len(self) > len(other):
[152] Fix | Delete
return False
[153] Fix | Delete
for elem in self:
[154] Fix | Delete
if elem not in other:
[155] Fix | Delete
return False
[156] Fix | Delete
return True
[157] Fix | Delete
[158] Fix | Delete
def __lt__(self, other):
[159] Fix | Delete
if not isinstance(other, Set):
[160] Fix | Delete
return NotImplemented
[161] Fix | Delete
return len(self) < len(other) and self.__le__(other)
[162] Fix | Delete
[163] Fix | Delete
def __gt__(self, other):
[164] Fix | Delete
if not isinstance(other, Set):
[165] Fix | Delete
return NotImplemented
[166] Fix | Delete
return len(self) > len(other) and self.__ge__(other)
[167] Fix | Delete
[168] Fix | Delete
def __ge__(self, other):
[169] Fix | Delete
if not isinstance(other, Set):
[170] Fix | Delete
return NotImplemented
[171] Fix | Delete
if len(self) < len(other):
[172] Fix | Delete
return False
[173] Fix | Delete
for elem in other:
[174] Fix | Delete
if elem not in self:
[175] Fix | Delete
return False
[176] Fix | Delete
return True
[177] Fix | Delete
[178] Fix | Delete
def __eq__(self, other):
[179] Fix | Delete
if not isinstance(other, Set):
[180] Fix | Delete
return NotImplemented
[181] Fix | Delete
return len(self) == len(other) and self.__le__(other)
[182] Fix | Delete
[183] Fix | Delete
def __ne__(self, other):
[184] Fix | Delete
return not (self == other)
[185] Fix | Delete
[186] Fix | Delete
@classmethod
[187] Fix | Delete
def _from_iterable(cls, it):
[188] Fix | Delete
'''Construct an instance of the class from any iterable input.
[189] Fix | Delete
[190] Fix | Delete
Must override this method if the class constructor signature
[191] Fix | Delete
does not accept an iterable for an input.
[192] Fix | Delete
'''
[193] Fix | Delete
return cls(it)
[194] Fix | Delete
[195] Fix | Delete
def __and__(self, other):
[196] Fix | Delete
if not isinstance(other, Iterable):
[197] Fix | Delete
return NotImplemented
[198] Fix | Delete
return self._from_iterable(value for value in other if value in self)
[199] Fix | Delete
[200] Fix | Delete
__rand__ = __and__
[201] Fix | Delete
[202] Fix | Delete
def isdisjoint(self, other):
[203] Fix | Delete
'Return True if two sets have a null intersection.'
[204] Fix | Delete
for value in other:
[205] Fix | Delete
if value in self:
[206] Fix | Delete
return False
[207] Fix | Delete
return True
[208] Fix | Delete
[209] Fix | Delete
def __or__(self, other):
[210] Fix | Delete
if not isinstance(other, Iterable):
[211] Fix | Delete
return NotImplemented
[212] Fix | Delete
chain = (e for s in (self, other) for e in s)
[213] Fix | Delete
return self._from_iterable(chain)
[214] Fix | Delete
[215] Fix | Delete
__ror__ = __or__
[216] Fix | Delete
[217] Fix | Delete
def __sub__(self, other):
[218] Fix | Delete
if not isinstance(other, Set):
[219] Fix | Delete
if not isinstance(other, Iterable):
[220] Fix | Delete
return NotImplemented
[221] Fix | Delete
other = self._from_iterable(other)
[222] Fix | Delete
return self._from_iterable(value for value in self
[223] Fix | Delete
if value not in other)
[224] Fix | Delete
[225] Fix | Delete
def __rsub__(self, other):
[226] Fix | Delete
if not isinstance(other, Set):
[227] Fix | Delete
if not isinstance(other, Iterable):
[228] Fix | Delete
return NotImplemented
[229] Fix | Delete
other = self._from_iterable(other)
[230] Fix | Delete
return self._from_iterable(value for value in other
[231] Fix | Delete
if value not in self)
[232] Fix | Delete
[233] Fix | Delete
def __xor__(self, other):
[234] Fix | Delete
if not isinstance(other, Set):
[235] Fix | Delete
if not isinstance(other, Iterable):
[236] Fix | Delete
return NotImplemented
[237] Fix | Delete
other = self._from_iterable(other)
[238] Fix | Delete
return (self - other) | (other - self)
[239] Fix | Delete
[240] Fix | Delete
__rxor__ = __xor__
[241] Fix | Delete
[242] Fix | Delete
# Sets are not hashable by default, but subclasses can change this
[243] Fix | Delete
__hash__ = None
[244] Fix | Delete
[245] Fix | Delete
def _hash(self):
[246] Fix | Delete
"""Compute the hash value of a set.
[247] Fix | Delete
[248] Fix | Delete
Note that we don't define __hash__: not all sets are hashable.
[249] Fix | Delete
But if you define a hashable set type, its __hash__ should
[250] Fix | Delete
call this function.
[251] Fix | Delete
[252] Fix | Delete
This must be compatible __eq__.
[253] Fix | Delete
[254] Fix | Delete
All sets ought to compare equal if they contain the same
[255] Fix | Delete
elements, regardless of how they are implemented, and
[256] Fix | Delete
regardless of the order of the elements; so there's not much
[257] Fix | Delete
freedom for __eq__ or __hash__. We match the algorithm used
[258] Fix | Delete
by the built-in frozenset type.
[259] Fix | Delete
"""
[260] Fix | Delete
MAX = sys.maxint
[261] Fix | Delete
MASK = 2 * MAX + 1
[262] Fix | Delete
n = len(self)
[263] Fix | Delete
h = 1927868237 * (n + 1)
[264] Fix | Delete
h &= MASK
[265] Fix | Delete
for x in self:
[266] Fix | Delete
hx = hash(x)
[267] Fix | Delete
h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
[268] Fix | Delete
h &= MASK
[269] Fix | Delete
h = h * 69069 + 907133923
[270] Fix | Delete
h &= MASK
[271] Fix | Delete
if h > MAX:
[272] Fix | Delete
h -= MASK + 1
[273] Fix | Delete
if h == -1:
[274] Fix | Delete
h = 590923713
[275] Fix | Delete
return h
[276] Fix | Delete
[277] Fix | Delete
Set.register(frozenset)
[278] Fix | Delete
[279] Fix | Delete
[280] Fix | Delete
class MutableSet(Set):
[281] Fix | Delete
"""A mutable set is a finite, iterable container.
[282] Fix | Delete
[283] Fix | Delete
This class provides concrete generic implementations of all
[284] Fix | Delete
methods except for __contains__, __iter__, __len__,
[285] Fix | Delete
add(), and discard().
[286] Fix | Delete
[287] Fix | Delete
To override the comparisons (presumably for speed, as the
[288] Fix | Delete
semantics are fixed), all you have to do is redefine __le__ and
[289] Fix | Delete
then the other operations will automatically follow suit.
[290] Fix | Delete
"""
[291] Fix | Delete
[292] Fix | Delete
@abstractmethod
[293] Fix | Delete
def add(self, value):
[294] Fix | Delete
"""Add an element."""
[295] Fix | Delete
raise NotImplementedError
[296] Fix | Delete
[297] Fix | Delete
@abstractmethod
[298] Fix | Delete
def discard(self, value):
[299] Fix | Delete
"""Remove an element. Do not raise an exception if absent."""
[300] Fix | Delete
raise NotImplementedError
[301] Fix | Delete
[302] Fix | Delete
def remove(self, value):
[303] Fix | Delete
"""Remove an element. If not a member, raise a KeyError."""
[304] Fix | Delete
if value not in self:
[305] Fix | Delete
raise KeyError(value)
[306] Fix | Delete
self.discard(value)
[307] Fix | Delete
[308] Fix | Delete
def pop(self):
[309] Fix | Delete
"""Return the popped value. Raise KeyError if empty."""
[310] Fix | Delete
it = iter(self)
[311] Fix | Delete
try:
[312] Fix | Delete
value = next(it)
[313] Fix | Delete
except StopIteration:
[314] Fix | Delete
raise KeyError
[315] Fix | Delete
self.discard(value)
[316] Fix | Delete
return value
[317] Fix | Delete
[318] Fix | Delete
def clear(self):
[319] Fix | Delete
"""This is slow (creates N new iterators!) but effective."""
[320] Fix | Delete
try:
[321] Fix | Delete
while True:
[322] Fix | Delete
self.pop()
[323] Fix | Delete
except KeyError:
[324] Fix | Delete
pass
[325] Fix | Delete
[326] Fix | Delete
def __ior__(self, it):
[327] Fix | Delete
for value in it:
[328] Fix | Delete
self.add(value)
[329] Fix | Delete
return self
[330] Fix | Delete
[331] Fix | Delete
def __iand__(self, it):
[332] Fix | Delete
for value in (self - it):
[333] Fix | Delete
self.discard(value)
[334] Fix | Delete
return self
[335] Fix | Delete
[336] Fix | Delete
def __ixor__(self, it):
[337] Fix | Delete
if it is self:
[338] Fix | Delete
self.clear()
[339] Fix | Delete
else:
[340] Fix | Delete
if not isinstance(it, Set):
[341] Fix | Delete
it = self._from_iterable(it)
[342] Fix | Delete
for value in it:
[343] Fix | Delete
if value in self:
[344] Fix | Delete
self.discard(value)
[345] Fix | Delete
else:
[346] Fix | Delete
self.add(value)
[347] Fix | Delete
return self
[348] Fix | Delete
[349] Fix | Delete
def __isub__(self, it):
[350] Fix | Delete
if it is self:
[351] Fix | Delete
self.clear()
[352] Fix | Delete
else:
[353] Fix | Delete
for value in it:
[354] Fix | Delete
self.discard(value)
[355] Fix | Delete
return self
[356] Fix | Delete
[357] Fix | Delete
MutableSet.register(set)
[358] Fix | Delete
[359] Fix | Delete
[360] Fix | Delete
### MAPPINGS ###
[361] Fix | Delete
[362] Fix | Delete
[363] Fix | Delete
class Mapping(Sized, Iterable, Container):
[364] Fix | Delete
[365] Fix | Delete
"""A Mapping is a generic container for associating key/value
[366] Fix | Delete
pairs.
[367] Fix | Delete
[368] Fix | Delete
This class provides concrete generic implementations of all
[369] Fix | Delete
methods except for __getitem__, __iter__, and __len__.
[370] Fix | Delete
[371] Fix | Delete
"""
[372] Fix | Delete
[373] Fix | Delete
@abstractmethod
[374] Fix | Delete
def __getitem__(self, key):
[375] Fix | Delete
raise KeyError
[376] Fix | Delete
[377] Fix | Delete
def get(self, key, default=None):
[378] Fix | Delete
'D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.'
[379] Fix | Delete
try:
[380] Fix | Delete
return self[key]
[381] Fix | Delete
except KeyError:
[382] Fix | Delete
return default
[383] Fix | Delete
[384] Fix | Delete
def __contains__(self, key):
[385] Fix | Delete
try:
[386] Fix | Delete
self[key]
[387] Fix | Delete
except KeyError:
[388] Fix | Delete
return False
[389] Fix | Delete
else:
[390] Fix | Delete
return True
[391] Fix | Delete
[392] Fix | Delete
def iterkeys(self):
[393] Fix | Delete
'D.iterkeys() -> an iterator over the keys of D'
[394] Fix | Delete
return iter(self)
[395] Fix | Delete
[396] Fix | Delete
def itervalues(self):
[397] Fix | Delete
'D.itervalues() -> an iterator over the values of D'
[398] Fix | Delete
for key in self:
[399] Fix | Delete
yield self[key]
[400] Fix | Delete
[401] Fix | Delete
def iteritems(self):
[402] Fix | Delete
'D.iteritems() -> an iterator over the (key, value) items of D'
[403] Fix | Delete
for key in self:
[404] Fix | Delete
yield (key, self[key])
[405] Fix | Delete
[406] Fix | Delete
def keys(self):
[407] Fix | Delete
"D.keys() -> list of D's keys"
[408] Fix | Delete
return list(self)
[409] Fix | Delete
[410] Fix | Delete
def items(self):
[411] Fix | Delete
"D.items() -> list of D's (key, value) pairs, as 2-tuples"
[412] Fix | Delete
return [(key, self[key]) for key in self]
[413] Fix | Delete
[414] Fix | Delete
def values(self):
[415] Fix | Delete
"D.values() -> list of D's values"
[416] Fix | Delete
return [self[key] for key in self]
[417] Fix | Delete
[418] Fix | Delete
# Mappings are not hashable by default, but subclasses can change this
[419] Fix | Delete
__hash__ = None
[420] Fix | Delete
[421] Fix | Delete
def __eq__(self, other):
[422] Fix | Delete
if not isinstance(other, Mapping):
[423] Fix | Delete
return NotImplemented
[424] Fix | Delete
return dict(self.items()) == dict(other.items())
[425] Fix | Delete
[426] Fix | Delete
def __ne__(self, other):
[427] Fix | Delete
return not (self == other)
[428] Fix | Delete
[429] Fix | Delete
class MappingView(Sized):
[430] Fix | Delete
[431] Fix | Delete
def __init__(self, mapping):
[432] Fix | Delete
self._mapping = mapping
[433] Fix | Delete
[434] Fix | Delete
def __len__(self):
[435] Fix | Delete
return len(self._mapping)
[436] Fix | Delete
[437] Fix | Delete
def __repr__(self):
[438] Fix | Delete
return '{0.__class__.__name__}({0._mapping!r})'.format(self)
[439] Fix | Delete
[440] Fix | Delete
[441] Fix | Delete
class KeysView(MappingView, Set):
[442] Fix | Delete
[443] Fix | Delete
@classmethod
[444] Fix | Delete
def _from_iterable(self, it):
[445] Fix | Delete
return set(it)
[446] Fix | Delete
[447] Fix | Delete
def __contains__(self, key):
[448] Fix | Delete
return key in self._mapping
[449] Fix | Delete
[450] Fix | Delete
def __iter__(self):
[451] Fix | Delete
for key in self._mapping:
[452] Fix | Delete
yield key
[453] Fix | Delete
[454] Fix | Delete
KeysView.register(type({}.viewkeys()))
[455] Fix | Delete
[456] Fix | Delete
class ItemsView(MappingView, Set):
[457] Fix | Delete
[458] Fix | Delete
@classmethod
[459] Fix | Delete
def _from_iterable(self, it):
[460] Fix | Delete
return set(it)
[461] Fix | Delete
[462] Fix | Delete
def __contains__(self, item):
[463] Fix | Delete
key, value = item
[464] Fix | Delete
try:
[465] Fix | Delete
v = self._mapping[key]
[466] Fix | Delete
except KeyError:
[467] Fix | Delete
return False
[468] Fix | Delete
else:
[469] Fix | Delete
return v == value
[470] Fix | Delete
[471] Fix | Delete
def __iter__(self):
[472] Fix | Delete
for key in self._mapping:
[473] Fix | Delete
yield (key, self._mapping[key])
[474] Fix | Delete
[475] Fix | Delete
ItemsView.register(type({}.viewitems()))
[476] Fix | Delete
[477] Fix | Delete
class ValuesView(MappingView):
[478] Fix | Delete
[479] Fix | Delete
def __contains__(self, value):
[480] Fix | Delete
for key in self._mapping:
[481] Fix | Delete
if value == self._mapping[key]:
[482] Fix | Delete
return True
[483] Fix | Delete
return False
[484] Fix | Delete
[485] Fix | Delete
def __iter__(self):
[486] Fix | Delete
for key in self._mapping:
[487] Fix | Delete
yield self._mapping[key]
[488] Fix | Delete
[489] Fix | Delete
ValuesView.register(type({}.viewvalues()))
[490] Fix | Delete
[491] Fix | Delete
class MutableMapping(Mapping):
[492] Fix | Delete
[493] Fix | Delete
"""A MutableMapping is a generic container for associating
[494] Fix | Delete
key/value pairs.
[495] Fix | Delete
[496] Fix | Delete
This class provides concrete generic implementations of all
[497] Fix | Delete
methods except for __getitem__, __setitem__, __delitem__,
[498] Fix | Delete
__iter__, and __len__.
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function