Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: UserDict.py
"""A more or less complete user-defined wrapper around dictionary objects."""
[0] Fix | Delete
[1] Fix | Delete
class UserDict:
[2] Fix | Delete
def __init__(*args, **kwargs):
[3] Fix | Delete
if not args:
[4] Fix | Delete
raise TypeError("descriptor '__init__' of 'UserDict' object "
[5] Fix | Delete
"needs an argument")
[6] Fix | Delete
self = args[0]
[7] Fix | Delete
args = args[1:]
[8] Fix | Delete
if len(args) > 1:
[9] Fix | Delete
raise TypeError('expected at most 1 arguments, got %d' % len(args))
[10] Fix | Delete
if args:
[11] Fix | Delete
dict = args[0]
[12] Fix | Delete
elif 'dict' in kwargs:
[13] Fix | Delete
dict = kwargs.pop('dict')
[14] Fix | Delete
import warnings
[15] Fix | Delete
warnings.warn("Passing 'dict' as keyword argument is "
[16] Fix | Delete
"deprecated", PendingDeprecationWarning,
[17] Fix | Delete
stacklevel=2)
[18] Fix | Delete
else:
[19] Fix | Delete
dict = None
[20] Fix | Delete
self.data = {}
[21] Fix | Delete
if dict is not None:
[22] Fix | Delete
self.update(dict)
[23] Fix | Delete
if len(kwargs):
[24] Fix | Delete
self.update(kwargs)
[25] Fix | Delete
def __repr__(self): return repr(self.data)
[26] Fix | Delete
def __cmp__(self, dict):
[27] Fix | Delete
if isinstance(dict, UserDict):
[28] Fix | Delete
return cmp(self.data, dict.data)
[29] Fix | Delete
else:
[30] Fix | Delete
return cmp(self.data, dict)
[31] Fix | Delete
__hash__ = None # Avoid Py3k warning
[32] Fix | Delete
def __len__(self): return len(self.data)
[33] Fix | Delete
def __getitem__(self, key):
[34] Fix | Delete
if key in self.data:
[35] Fix | Delete
return self.data[key]
[36] Fix | Delete
if hasattr(self.__class__, "__missing__"):
[37] Fix | Delete
return self.__class__.__missing__(self, key)
[38] Fix | Delete
raise KeyError(key)
[39] Fix | Delete
def __setitem__(self, key, item): self.data[key] = item
[40] Fix | Delete
def __delitem__(self, key): del self.data[key]
[41] Fix | Delete
def clear(self): self.data.clear()
[42] Fix | Delete
def copy(self):
[43] Fix | Delete
if self.__class__ is UserDict:
[44] Fix | Delete
return UserDict(self.data.copy())
[45] Fix | Delete
import copy
[46] Fix | Delete
data = self.data
[47] Fix | Delete
try:
[48] Fix | Delete
self.data = {}
[49] Fix | Delete
c = copy.copy(self)
[50] Fix | Delete
finally:
[51] Fix | Delete
self.data = data
[52] Fix | Delete
c.update(self)
[53] Fix | Delete
return c
[54] Fix | Delete
def keys(self): return self.data.keys()
[55] Fix | Delete
def items(self): return self.data.items()
[56] Fix | Delete
def iteritems(self): return self.data.iteritems()
[57] Fix | Delete
def iterkeys(self): return self.data.iterkeys()
[58] Fix | Delete
def itervalues(self): return self.data.itervalues()
[59] Fix | Delete
def values(self): return self.data.values()
[60] Fix | Delete
def has_key(self, key): return key in self.data
[61] Fix | Delete
def update(*args, **kwargs):
[62] Fix | Delete
if not args:
[63] Fix | Delete
raise TypeError("descriptor 'update' of 'UserDict' object "
[64] Fix | Delete
"needs an argument")
[65] Fix | Delete
self = args[0]
[66] Fix | Delete
args = args[1:]
[67] Fix | Delete
if len(args) > 1:
[68] Fix | Delete
raise TypeError('expected at most 1 arguments, got %d' % len(args))
[69] Fix | Delete
if args:
[70] Fix | Delete
dict = args[0]
[71] Fix | Delete
elif 'dict' in kwargs:
[72] Fix | Delete
dict = kwargs.pop('dict')
[73] Fix | Delete
import warnings
[74] Fix | Delete
warnings.warn("Passing 'dict' as keyword argument is deprecated",
[75] Fix | Delete
PendingDeprecationWarning, stacklevel=2)
[76] Fix | Delete
else:
[77] Fix | Delete
dict = None
[78] Fix | Delete
if dict is None:
[79] Fix | Delete
pass
[80] Fix | Delete
elif isinstance(dict, UserDict):
[81] Fix | Delete
self.data.update(dict.data)
[82] Fix | Delete
elif isinstance(dict, type({})) or not hasattr(dict, 'items'):
[83] Fix | Delete
self.data.update(dict)
[84] Fix | Delete
else:
[85] Fix | Delete
for k, v in dict.items():
[86] Fix | Delete
self[k] = v
[87] Fix | Delete
if len(kwargs):
[88] Fix | Delete
self.data.update(kwargs)
[89] Fix | Delete
def get(self, key, failobj=None):
[90] Fix | Delete
if key not in self:
[91] Fix | Delete
return failobj
[92] Fix | Delete
return self[key]
[93] Fix | Delete
def setdefault(self, key, failobj=None):
[94] Fix | Delete
if key not in self:
[95] Fix | Delete
self[key] = failobj
[96] Fix | Delete
return self[key]
[97] Fix | Delete
def pop(self, key, *args):
[98] Fix | Delete
return self.data.pop(key, *args)
[99] Fix | Delete
def popitem(self):
[100] Fix | Delete
return self.data.popitem()
[101] Fix | Delete
def __contains__(self, key):
[102] Fix | Delete
return key in self.data
[103] Fix | Delete
@classmethod
[104] Fix | Delete
def fromkeys(cls, iterable, value=None):
[105] Fix | Delete
d = cls()
[106] Fix | Delete
for key in iterable:
[107] Fix | Delete
d[key] = value
[108] Fix | Delete
return d
[109] Fix | Delete
[110] Fix | Delete
class IterableUserDict(UserDict):
[111] Fix | Delete
def __iter__(self):
[112] Fix | Delete
return iter(self.data)
[113] Fix | Delete
[114] Fix | Delete
import _abcoll
[115] Fix | Delete
_abcoll.MutableMapping.register(IterableUserDict)
[116] Fix | Delete
[117] Fix | Delete
[118] Fix | Delete
class DictMixin:
[119] Fix | Delete
# Mixin defining all dictionary methods for classes that already have
[120] Fix | Delete
# a minimum dictionary interface including getitem, setitem, delitem,
[121] Fix | Delete
# and keys. Without knowledge of the subclass constructor, the mixin
[122] Fix | Delete
# does not define __init__() or copy(). In addition to the four base
[123] Fix | Delete
# methods, progressively more efficiency comes with defining
[124] Fix | Delete
# __contains__(), __iter__(), and iteritems().
[125] Fix | Delete
[126] Fix | Delete
# second level definitions support higher levels
[127] Fix | Delete
def __iter__(self):
[128] Fix | Delete
for k in self.keys():
[129] Fix | Delete
yield k
[130] Fix | Delete
def has_key(self, key):
[131] Fix | Delete
try:
[132] Fix | Delete
self[key]
[133] Fix | Delete
except KeyError:
[134] Fix | Delete
return False
[135] Fix | Delete
return True
[136] Fix | Delete
def __contains__(self, key):
[137] Fix | Delete
return self.has_key(key)
[138] Fix | Delete
[139] Fix | Delete
# third level takes advantage of second level definitions
[140] Fix | Delete
def iteritems(self):
[141] Fix | Delete
for k in self:
[142] Fix | Delete
yield (k, self[k])
[143] Fix | Delete
def iterkeys(self):
[144] Fix | Delete
return self.__iter__()
[145] Fix | Delete
[146] Fix | Delete
# fourth level uses definitions from lower levels
[147] Fix | Delete
def itervalues(self):
[148] Fix | Delete
for _, v in self.iteritems():
[149] Fix | Delete
yield v
[150] Fix | Delete
def values(self):
[151] Fix | Delete
return [v for _, v in self.iteritems()]
[152] Fix | Delete
def items(self):
[153] Fix | Delete
return list(self.iteritems())
[154] Fix | Delete
def clear(self):
[155] Fix | Delete
for key in self.keys():
[156] Fix | Delete
del self[key]
[157] Fix | Delete
def setdefault(self, key, default=None):
[158] Fix | Delete
try:
[159] Fix | Delete
return self[key]
[160] Fix | Delete
except KeyError:
[161] Fix | Delete
self[key] = default
[162] Fix | Delete
return default
[163] Fix | Delete
def pop(self, key, *args):
[164] Fix | Delete
if len(args) > 1:
[165] Fix | Delete
raise TypeError, "pop expected at most 2 arguments, got "\
[166] Fix | Delete
+ repr(1 + len(args))
[167] Fix | Delete
try:
[168] Fix | Delete
value = self[key]
[169] Fix | Delete
except KeyError:
[170] Fix | Delete
if args:
[171] Fix | Delete
return args[0]
[172] Fix | Delete
raise
[173] Fix | Delete
del self[key]
[174] Fix | Delete
return value
[175] Fix | Delete
def popitem(self):
[176] Fix | Delete
try:
[177] Fix | Delete
k, v = self.iteritems().next()
[178] Fix | Delete
except StopIteration:
[179] Fix | Delete
raise KeyError, 'container is empty'
[180] Fix | Delete
del self[k]
[181] Fix | Delete
return (k, v)
[182] Fix | Delete
def update(self, other=None, **kwargs):
[183] Fix | Delete
# Make progressively weaker assumptions about "other"
[184] Fix | Delete
if other is None:
[185] Fix | Delete
pass
[186] Fix | Delete
elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups
[187] Fix | Delete
for k, v in other.iteritems():
[188] Fix | Delete
self[k] = v
[189] Fix | Delete
elif hasattr(other, 'keys'):
[190] Fix | Delete
for k in other.keys():
[191] Fix | Delete
self[k] = other[k]
[192] Fix | Delete
else:
[193] Fix | Delete
for k, v in other:
[194] Fix | Delete
self[k] = v
[195] Fix | Delete
if kwargs:
[196] Fix | Delete
self.update(kwargs)
[197] Fix | Delete
def get(self, key, default=None):
[198] Fix | Delete
try:
[199] Fix | Delete
return self[key]
[200] Fix | Delete
except KeyError:
[201] Fix | Delete
return default
[202] Fix | Delete
def __repr__(self):
[203] Fix | Delete
return repr(dict(self.iteritems()))
[204] Fix | Delete
def __cmp__(self, other):
[205] Fix | Delete
if other is None:
[206] Fix | Delete
return 1
[207] Fix | Delete
if isinstance(other, DictMixin):
[208] Fix | Delete
other = dict(other.iteritems())
[209] Fix | Delete
return cmp(dict(self.iteritems()), other)
[210] Fix | Delete
def __len__(self):
[211] Fix | Delete
return len(self.keys())
[212] Fix | Delete
[213] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function