Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3....
File: _collections_abc.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
Unit tests are in test_collections.
[5] Fix | Delete
"""
[6] Fix | Delete
[7] Fix | Delete
from abc import ABCMeta, abstractmethod
[8] Fix | Delete
import sys
[9] Fix | Delete
[10] Fix | Delete
__all__ = ["Awaitable", "Coroutine",
[11] Fix | Delete
"AsyncIterable", "AsyncIterator", "AsyncGenerator",
[12] Fix | Delete
"Hashable", "Iterable", "Iterator", "Generator", "Reversible",
[13] Fix | Delete
"Sized", "Container", "Callable", "Collection",
[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
"ByteString",
[19] Fix | Delete
]
[20] Fix | Delete
[21] Fix | Delete
# This module has been renamed from collections.abc to _collections_abc to
[22] Fix | Delete
# speed up interpreter startup. Some of the types such as MutableMapping are
[23] Fix | Delete
# required early but collections module imports a lot of other modules.
[24] Fix | Delete
# See issue #19218
[25] Fix | Delete
__name__ = "collections.abc"
[26] Fix | Delete
[27] Fix | Delete
# Private list of types that we want to register with the various ABCs
[28] Fix | Delete
# so that they will pass tests like:
[29] Fix | Delete
# it = iter(somebytearray)
[30] Fix | Delete
# assert isinstance(it, Iterable)
[31] Fix | Delete
# Note: in other implementations, these types might not be distinct
[32] Fix | Delete
# and they may have their own implementation specific types that
[33] Fix | Delete
# are not included on this list.
[34] Fix | Delete
bytes_iterator = type(iter(b''))
[35] Fix | Delete
bytearray_iterator = type(iter(bytearray()))
[36] Fix | Delete
#callable_iterator = ???
[37] Fix | Delete
dict_keyiterator = type(iter({}.keys()))
[38] Fix | Delete
dict_valueiterator = type(iter({}.values()))
[39] Fix | Delete
dict_itemiterator = type(iter({}.items()))
[40] Fix | Delete
list_iterator = type(iter([]))
[41] Fix | Delete
list_reverseiterator = type(iter(reversed([])))
[42] Fix | Delete
range_iterator = type(iter(range(0)))
[43] Fix | Delete
longrange_iterator = type(iter(range(1 << 1000)))
[44] Fix | Delete
set_iterator = type(iter(set()))
[45] Fix | Delete
str_iterator = type(iter(""))
[46] Fix | Delete
tuple_iterator = type(iter(()))
[47] Fix | Delete
zip_iterator = type(iter(zip()))
[48] Fix | Delete
## views ##
[49] Fix | Delete
dict_keys = type({}.keys())
[50] Fix | Delete
dict_values = type({}.values())
[51] Fix | Delete
dict_items = type({}.items())
[52] Fix | Delete
## misc ##
[53] Fix | Delete
mappingproxy = type(type.__dict__)
[54] Fix | Delete
generator = type((lambda: (yield))())
[55] Fix | Delete
## coroutine ##
[56] Fix | Delete
async def _coro(): pass
[57] Fix | Delete
_coro = _coro()
[58] Fix | Delete
coroutine = type(_coro)
[59] Fix | Delete
_coro.close() # Prevent ResourceWarning
[60] Fix | Delete
del _coro
[61] Fix | Delete
## asynchronous generator ##
[62] Fix | Delete
async def _ag(): yield
[63] Fix | Delete
_ag = _ag()
[64] Fix | Delete
async_generator = type(_ag)
[65] Fix | Delete
del _ag
[66] Fix | Delete
[67] Fix | Delete
[68] Fix | Delete
### ONE-TRICK PONIES ###
[69] Fix | Delete
[70] Fix | Delete
def _check_methods(C, *methods):
[71] Fix | Delete
mro = C.__mro__
[72] Fix | Delete
for method in methods:
[73] Fix | Delete
for B in mro:
[74] Fix | Delete
if method in B.__dict__:
[75] Fix | Delete
if B.__dict__[method] is None:
[76] Fix | Delete
return NotImplemented
[77] Fix | Delete
break
[78] Fix | Delete
else:
[79] Fix | Delete
return NotImplemented
[80] Fix | Delete
return True
[81] Fix | Delete
[82] Fix | Delete
class Hashable(metaclass=ABCMeta):
[83] Fix | Delete
[84] Fix | Delete
__slots__ = ()
[85] Fix | Delete
[86] Fix | Delete
@abstractmethod
[87] Fix | Delete
def __hash__(self):
[88] Fix | Delete
return 0
[89] Fix | Delete
[90] Fix | Delete
@classmethod
[91] Fix | Delete
def __subclasshook__(cls, C):
[92] Fix | Delete
if cls is Hashable:
[93] Fix | Delete
return _check_methods(C, "__hash__")
[94] Fix | Delete
return NotImplemented
[95] Fix | Delete
[96] Fix | Delete
[97] Fix | Delete
class Awaitable(metaclass=ABCMeta):
[98] Fix | Delete
[99] Fix | Delete
__slots__ = ()
[100] Fix | Delete
[101] Fix | Delete
@abstractmethod
[102] Fix | Delete
def __await__(self):
[103] Fix | Delete
yield
[104] Fix | Delete
[105] Fix | Delete
@classmethod
[106] Fix | Delete
def __subclasshook__(cls, C):
[107] Fix | Delete
if cls is Awaitable:
[108] Fix | Delete
return _check_methods(C, "__await__")
[109] Fix | Delete
return NotImplemented
[110] Fix | Delete
[111] Fix | Delete
[112] Fix | Delete
class Coroutine(Awaitable):
[113] Fix | Delete
[114] Fix | Delete
__slots__ = ()
[115] Fix | Delete
[116] Fix | Delete
@abstractmethod
[117] Fix | Delete
def send(self, value):
[118] Fix | Delete
"""Send a value into the coroutine.
[119] Fix | Delete
Return next yielded value or raise StopIteration.
[120] Fix | Delete
"""
[121] Fix | Delete
raise StopIteration
[122] Fix | Delete
[123] Fix | Delete
@abstractmethod
[124] Fix | Delete
def throw(self, typ, val=None, tb=None):
[125] Fix | Delete
"""Raise an exception in the coroutine.
[126] Fix | Delete
Return next yielded value or raise StopIteration.
[127] Fix | Delete
"""
[128] Fix | Delete
if val is None:
[129] Fix | Delete
if tb is None:
[130] Fix | Delete
raise typ
[131] Fix | Delete
val = typ()
[132] Fix | Delete
if tb is not None:
[133] Fix | Delete
val = val.with_traceback(tb)
[134] Fix | Delete
raise val
[135] Fix | Delete
[136] Fix | Delete
def close(self):
[137] Fix | Delete
"""Raise GeneratorExit inside coroutine.
[138] Fix | Delete
"""
[139] Fix | Delete
try:
[140] Fix | Delete
self.throw(GeneratorExit)
[141] Fix | Delete
except (GeneratorExit, StopIteration):
[142] Fix | Delete
pass
[143] Fix | Delete
else:
[144] Fix | Delete
raise RuntimeError("coroutine ignored GeneratorExit")
[145] Fix | Delete
[146] Fix | Delete
@classmethod
[147] Fix | Delete
def __subclasshook__(cls, C):
[148] Fix | Delete
if cls is Coroutine:
[149] Fix | Delete
return _check_methods(C, '__await__', 'send', 'throw', 'close')
[150] Fix | Delete
return NotImplemented
[151] Fix | Delete
[152] Fix | Delete
[153] Fix | Delete
Coroutine.register(coroutine)
[154] Fix | Delete
[155] Fix | Delete
[156] Fix | Delete
class AsyncIterable(metaclass=ABCMeta):
[157] Fix | Delete
[158] Fix | Delete
__slots__ = ()
[159] Fix | Delete
[160] Fix | Delete
@abstractmethod
[161] Fix | Delete
def __aiter__(self):
[162] Fix | Delete
return AsyncIterator()
[163] Fix | Delete
[164] Fix | Delete
@classmethod
[165] Fix | Delete
def __subclasshook__(cls, C):
[166] Fix | Delete
if cls is AsyncIterable:
[167] Fix | Delete
return _check_methods(C, "__aiter__")
[168] Fix | Delete
return NotImplemented
[169] Fix | Delete
[170] Fix | Delete
[171] Fix | Delete
class AsyncIterator(AsyncIterable):
[172] Fix | Delete
[173] Fix | Delete
__slots__ = ()
[174] Fix | Delete
[175] Fix | Delete
@abstractmethod
[176] Fix | Delete
async def __anext__(self):
[177] Fix | Delete
"""Return the next item or raise StopAsyncIteration when exhausted."""
[178] Fix | Delete
raise StopAsyncIteration
[179] Fix | Delete
[180] Fix | Delete
def __aiter__(self):
[181] Fix | Delete
return self
[182] Fix | Delete
[183] Fix | Delete
@classmethod
[184] Fix | Delete
def __subclasshook__(cls, C):
[185] Fix | Delete
if cls is AsyncIterator:
[186] Fix | Delete
return _check_methods(C, "__anext__", "__aiter__")
[187] Fix | Delete
return NotImplemented
[188] Fix | Delete
[189] Fix | Delete
[190] Fix | Delete
class AsyncGenerator(AsyncIterator):
[191] Fix | Delete
[192] Fix | Delete
__slots__ = ()
[193] Fix | Delete
[194] Fix | Delete
async def __anext__(self):
[195] Fix | Delete
"""Return the next item from the asynchronous generator.
[196] Fix | Delete
When exhausted, raise StopAsyncIteration.
[197] Fix | Delete
"""
[198] Fix | Delete
return await self.asend(None)
[199] Fix | Delete
[200] Fix | Delete
@abstractmethod
[201] Fix | Delete
async def asend(self, value):
[202] Fix | Delete
"""Send a value into the asynchronous generator.
[203] Fix | Delete
Return next yielded value or raise StopAsyncIteration.
[204] Fix | Delete
"""
[205] Fix | Delete
raise StopAsyncIteration
[206] Fix | Delete
[207] Fix | Delete
@abstractmethod
[208] Fix | Delete
async def athrow(self, typ, val=None, tb=None):
[209] Fix | Delete
"""Raise an exception in the asynchronous generator.
[210] Fix | Delete
Return next yielded value or raise StopAsyncIteration.
[211] Fix | Delete
"""
[212] Fix | Delete
if val is None:
[213] Fix | Delete
if tb is None:
[214] Fix | Delete
raise typ
[215] Fix | Delete
val = typ()
[216] Fix | Delete
if tb is not None:
[217] Fix | Delete
val = val.with_traceback(tb)
[218] Fix | Delete
raise val
[219] Fix | Delete
[220] Fix | Delete
async def aclose(self):
[221] Fix | Delete
"""Raise GeneratorExit inside coroutine.
[222] Fix | Delete
"""
[223] Fix | Delete
try:
[224] Fix | Delete
await self.athrow(GeneratorExit)
[225] Fix | Delete
except (GeneratorExit, StopAsyncIteration):
[226] Fix | Delete
pass
[227] Fix | Delete
else:
[228] Fix | Delete
raise RuntimeError("asynchronous generator ignored GeneratorExit")
[229] Fix | Delete
[230] Fix | Delete
@classmethod
[231] Fix | Delete
def __subclasshook__(cls, C):
[232] Fix | Delete
if cls is AsyncGenerator:
[233] Fix | Delete
return _check_methods(C, '__aiter__', '__anext__',
[234] Fix | Delete
'asend', 'athrow', 'aclose')
[235] Fix | Delete
return NotImplemented
[236] Fix | Delete
[237] Fix | Delete
[238] Fix | Delete
AsyncGenerator.register(async_generator)
[239] Fix | Delete
[240] Fix | Delete
[241] Fix | Delete
class Iterable(metaclass=ABCMeta):
[242] Fix | Delete
[243] Fix | Delete
__slots__ = ()
[244] Fix | Delete
[245] Fix | Delete
@abstractmethod
[246] Fix | Delete
def __iter__(self):
[247] Fix | Delete
while False:
[248] Fix | Delete
yield None
[249] Fix | Delete
[250] Fix | Delete
@classmethod
[251] Fix | Delete
def __subclasshook__(cls, C):
[252] Fix | Delete
if cls is Iterable:
[253] Fix | Delete
return _check_methods(C, "__iter__")
[254] Fix | Delete
return NotImplemented
[255] Fix | Delete
[256] Fix | Delete
[257] Fix | Delete
class Iterator(Iterable):
[258] Fix | Delete
[259] Fix | Delete
__slots__ = ()
[260] Fix | Delete
[261] Fix | Delete
@abstractmethod
[262] Fix | Delete
def __next__(self):
[263] Fix | Delete
'Return the next item from the iterator. When exhausted, raise StopIteration'
[264] Fix | Delete
raise StopIteration
[265] Fix | Delete
[266] Fix | Delete
def __iter__(self):
[267] Fix | Delete
return self
[268] Fix | Delete
[269] Fix | Delete
@classmethod
[270] Fix | Delete
def __subclasshook__(cls, C):
[271] Fix | Delete
if cls is Iterator:
[272] Fix | Delete
return _check_methods(C, '__iter__', '__next__')
[273] Fix | Delete
return NotImplemented
[274] Fix | Delete
[275] Fix | Delete
Iterator.register(bytes_iterator)
[276] Fix | Delete
Iterator.register(bytearray_iterator)
[277] Fix | Delete
#Iterator.register(callable_iterator)
[278] Fix | Delete
Iterator.register(dict_keyiterator)
[279] Fix | Delete
Iterator.register(dict_valueiterator)
[280] Fix | Delete
Iterator.register(dict_itemiterator)
[281] Fix | Delete
Iterator.register(list_iterator)
[282] Fix | Delete
Iterator.register(list_reverseiterator)
[283] Fix | Delete
Iterator.register(range_iterator)
[284] Fix | Delete
Iterator.register(longrange_iterator)
[285] Fix | Delete
Iterator.register(set_iterator)
[286] Fix | Delete
Iterator.register(str_iterator)
[287] Fix | Delete
Iterator.register(tuple_iterator)
[288] Fix | Delete
Iterator.register(zip_iterator)
[289] Fix | Delete
[290] Fix | Delete
[291] Fix | Delete
class Reversible(Iterable):
[292] Fix | Delete
[293] Fix | Delete
__slots__ = ()
[294] Fix | Delete
[295] Fix | Delete
@abstractmethod
[296] Fix | Delete
def __reversed__(self):
[297] Fix | Delete
while False:
[298] Fix | Delete
yield None
[299] Fix | Delete
[300] Fix | Delete
@classmethod
[301] Fix | Delete
def __subclasshook__(cls, C):
[302] Fix | Delete
if cls is Reversible:
[303] Fix | Delete
return _check_methods(C, "__reversed__", "__iter__")
[304] Fix | Delete
return NotImplemented
[305] Fix | Delete
[306] Fix | Delete
[307] Fix | Delete
class Generator(Iterator):
[308] Fix | Delete
[309] Fix | Delete
__slots__ = ()
[310] Fix | Delete
[311] Fix | Delete
def __next__(self):
[312] Fix | Delete
"""Return the next item from the generator.
[313] Fix | Delete
When exhausted, raise StopIteration.
[314] Fix | Delete
"""
[315] Fix | Delete
return self.send(None)
[316] Fix | Delete
[317] Fix | Delete
@abstractmethod
[318] Fix | Delete
def send(self, value):
[319] Fix | Delete
"""Send a value into the generator.
[320] Fix | Delete
Return next yielded value or raise StopIteration.
[321] Fix | Delete
"""
[322] Fix | Delete
raise StopIteration
[323] Fix | Delete
[324] Fix | Delete
@abstractmethod
[325] Fix | Delete
def throw(self, typ, val=None, tb=None):
[326] Fix | Delete
"""Raise an exception in the generator.
[327] Fix | Delete
Return next yielded value or raise StopIteration.
[328] Fix | Delete
"""
[329] Fix | Delete
if val is None:
[330] Fix | Delete
if tb is None:
[331] Fix | Delete
raise typ
[332] Fix | Delete
val = typ()
[333] Fix | Delete
if tb is not None:
[334] Fix | Delete
val = val.with_traceback(tb)
[335] Fix | Delete
raise val
[336] Fix | Delete
[337] Fix | Delete
def close(self):
[338] Fix | Delete
"""Raise GeneratorExit inside generator.
[339] Fix | Delete
"""
[340] Fix | Delete
try:
[341] Fix | Delete
self.throw(GeneratorExit)
[342] Fix | Delete
except (GeneratorExit, StopIteration):
[343] Fix | Delete
pass
[344] Fix | Delete
else:
[345] Fix | Delete
raise RuntimeError("generator ignored GeneratorExit")
[346] Fix | Delete
[347] Fix | Delete
@classmethod
[348] Fix | Delete
def __subclasshook__(cls, C):
[349] Fix | Delete
if cls is Generator:
[350] Fix | Delete
return _check_methods(C, '__iter__', '__next__',
[351] Fix | Delete
'send', 'throw', 'close')
[352] Fix | Delete
return NotImplemented
[353] Fix | Delete
[354] Fix | Delete
Generator.register(generator)
[355] Fix | Delete
[356] Fix | Delete
[357] Fix | Delete
class Sized(metaclass=ABCMeta):
[358] Fix | Delete
[359] Fix | Delete
__slots__ = ()
[360] Fix | Delete
[361] Fix | Delete
@abstractmethod
[362] Fix | Delete
def __len__(self):
[363] Fix | Delete
return 0
[364] Fix | Delete
[365] Fix | Delete
@classmethod
[366] Fix | Delete
def __subclasshook__(cls, C):
[367] Fix | Delete
if cls is Sized:
[368] Fix | Delete
return _check_methods(C, "__len__")
[369] Fix | Delete
return NotImplemented
[370] Fix | Delete
[371] Fix | Delete
[372] Fix | Delete
class Container(metaclass=ABCMeta):
[373] Fix | Delete
[374] Fix | Delete
__slots__ = ()
[375] Fix | Delete
[376] Fix | Delete
@abstractmethod
[377] Fix | Delete
def __contains__(self, x):
[378] Fix | Delete
return False
[379] Fix | Delete
[380] Fix | Delete
@classmethod
[381] Fix | Delete
def __subclasshook__(cls, C):
[382] Fix | Delete
if cls is Container:
[383] Fix | Delete
return _check_methods(C, "__contains__")
[384] Fix | Delete
return NotImplemented
[385] Fix | Delete
[386] Fix | Delete
class Collection(Sized, Iterable, Container):
[387] Fix | Delete
[388] Fix | Delete
__slots__ = ()
[389] Fix | Delete
[390] Fix | Delete
@classmethod
[391] Fix | Delete
def __subclasshook__(cls, C):
[392] Fix | Delete
if cls is Collection:
[393] Fix | Delete
return _check_methods(C, "__len__", "__iter__", "__contains__")
[394] Fix | Delete
return NotImplemented
[395] Fix | Delete
[396] Fix | Delete
class Callable(metaclass=ABCMeta):
[397] Fix | Delete
[398] Fix | Delete
__slots__ = ()
[399] Fix | Delete
[400] Fix | Delete
@abstractmethod
[401] Fix | Delete
def __call__(self, *args, **kwds):
[402] Fix | Delete
return False
[403] Fix | Delete
[404] Fix | Delete
@classmethod
[405] Fix | Delete
def __subclasshook__(cls, C):
[406] Fix | Delete
if cls is Callable:
[407] Fix | Delete
return _check_methods(C, "__call__")
[408] Fix | Delete
return NotImplemented
[409] Fix | Delete
[410] Fix | Delete
[411] Fix | Delete
### SETS ###
[412] Fix | Delete
[413] Fix | Delete
[414] Fix | Delete
class Set(Collection):
[415] Fix | Delete
[416] Fix | Delete
"""A set is a finite, iterable container.
[417] Fix | Delete
[418] Fix | Delete
This class provides concrete generic implementations of all
[419] Fix | Delete
methods except for __contains__, __iter__ and __len__.
[420] Fix | Delete
[421] Fix | Delete
To override the comparisons (presumably for speed, as the
[422] Fix | Delete
semantics are fixed), redefine __le__ and __ge__,
[423] Fix | Delete
then the other operations will automatically follow suit.
[424] Fix | Delete
"""
[425] Fix | Delete
[426] Fix | Delete
__slots__ = ()
[427] Fix | Delete
[428] Fix | Delete
def __le__(self, other):
[429] Fix | Delete
if not isinstance(other, Set):
[430] Fix | Delete
return NotImplemented
[431] Fix | Delete
if len(self) > len(other):
[432] Fix | Delete
return False
[433] Fix | Delete
for elem in self:
[434] Fix | Delete
if elem not in other:
[435] Fix | Delete
return False
[436] Fix | Delete
return True
[437] Fix | Delete
[438] Fix | Delete
def __lt__(self, other):
[439] Fix | Delete
if not isinstance(other, Set):
[440] Fix | Delete
return NotImplemented
[441] Fix | Delete
return len(self) < len(other) and self.__le__(other)
[442] Fix | Delete
[443] Fix | Delete
def __gt__(self, other):
[444] Fix | Delete
if not isinstance(other, Set):
[445] Fix | Delete
return NotImplemented
[446] Fix | Delete
return len(self) > len(other) and self.__ge__(other)
[447] Fix | Delete
[448] Fix | Delete
def __ge__(self, other):
[449] Fix | Delete
if not isinstance(other, Set):
[450] Fix | Delete
return NotImplemented
[451] Fix | Delete
if len(self) < len(other):
[452] Fix | Delete
return False
[453] Fix | Delete
for elem in other:
[454] Fix | Delete
if elem not in self:
[455] Fix | Delete
return False
[456] Fix | Delete
return True
[457] Fix | Delete
[458] Fix | Delete
def __eq__(self, other):
[459] Fix | Delete
if not isinstance(other, Set):
[460] Fix | Delete
return NotImplemented
[461] Fix | Delete
return len(self) == len(other) and self.__le__(other)
[462] Fix | Delete
[463] Fix | Delete
@classmethod
[464] Fix | Delete
def _from_iterable(cls, it):
[465] Fix | Delete
'''Construct an instance of the class from any iterable input.
[466] Fix | Delete
[467] Fix | Delete
Must override this method if the class constructor signature
[468] Fix | Delete
does not accept an iterable for an input.
[469] Fix | Delete
'''
[470] Fix | Delete
return cls(it)
[471] Fix | Delete
[472] Fix | Delete
def __and__(self, other):
[473] Fix | Delete
if not isinstance(other, Iterable):
[474] Fix | Delete
return NotImplemented
[475] Fix | Delete
return self._from_iterable(value for value in other if value in self)
[476] Fix | Delete
[477] Fix | Delete
__rand__ = __and__
[478] Fix | Delete
[479] Fix | Delete
def isdisjoint(self, other):
[480] Fix | Delete
'Return True if two sets have a null intersection.'
[481] Fix | Delete
for value in other:
[482] Fix | Delete
if value in self:
[483] Fix | Delete
return False
[484] Fix | Delete
return True
[485] Fix | Delete
[486] Fix | Delete
def __or__(self, other):
[487] Fix | Delete
if not isinstance(other, Iterable):
[488] Fix | Delete
return NotImplemented
[489] Fix | Delete
chain = (e for s in (self, other) for e in s)
[490] Fix | Delete
return self._from_iterable(chain)
[491] Fix | Delete
[492] Fix | Delete
__ror__ = __or__
[493] Fix | Delete
[494] Fix | Delete
def __sub__(self, other):
[495] Fix | Delete
if not isinstance(other, Set):
[496] Fix | Delete
if not isinstance(other, Iterable):
[497] Fix | Delete
return NotImplemented
[498] Fix | Delete
other = self._from_iterable(other)
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function