/usr/lib/python3/dist-packages/pyutilib/misc/dict_with_default.py is in python3-pyutilib 5.3.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | import sys
__all__ = ['SparseMapping']
try:
from collections import MutableMapping
class SparseMapping(MutableMapping):
"""
Class that provides a default value for keys that are not defined in
the dictionary, a specified index for keys, and a specified value that
mappings are within.
"""
def __init__(self, default=None, index=None, within=None, *args, **kwds):
self._map = kwds
self.default = default
self._index = index
self.within = within
self.update(dict(*args, **kwds))
def nondefault_keys(self):
return self._map.keys()
def nondefault_iter(self):
return iter(self._map)
def __len__(self):
if self._index is None or self.default is None:
return len(self._map)
return len(self._index)
def __contains__(self, key):
if self._index is None:
return key in self._map
return key in self._index
def __iter__(self):
if self._index is None or self.default is None:
return iter(self._map)
return iter(self._index)
def __getitem__(self, key):
if key in self._map:
return self._map[key]
if not self.default is None and (self._index is None or key in self._index):
return self.default
if (self.default is None) and (not self._index is None) and (key in self._index):
raise ValueError("Legal key '%s' specified in SparseMapping, but value is uninitialized and there is no default value" % key)
raise KeyError("Unknown key value: %s" % str(key))
def __setitem__(self, key, value):
if not self._index is None and not key in self._index:
raise KeyError("Unknown key value: %s" % str(key))
if not self.within is None and not value in self.within:
raise ValueError("Bad mapping value: %s" % str(value))
self._map[key] = value
def index(self):
return self._index
def set_item(self, key, value):
self._map[key] = value
def __delitem__(self, key):
del self._map[key]
except ImportError:
class SparseMapping(dict):
"""
Class that provides a default value for keys that are not defined in
the dictionary.
Adapted from code developed by Andreas Kloss and submitted to
the ActiveState Programmer Network http://aspn.activestate.com
"""
def __init__(self, default=None, index=None, within=None, *args, **kwds):
super(SparseMapping, self).__init__()
self.update(kwds)
self.default = default
self._index = index
self.within = within
def nondefault_keys(self):
return self.keys()
def nondefault_iter(self):
return iter(self)
def set_item(self, key, value):
self[key] = value
def index(self):
return self._index
def __len__(self):
if self._index is None or self.default is None:
return dict.__len__(self)
return len(self._index)
def __getitem__(self, key):
try:
return dict.__getitem__(self,key)
except KeyError:
err = sys.exc_info()[1]
if not self.default is None and (self._index is None or key in self._index):
return self.default
raise err
def __copy__(self):
return SparseMapping(self.default, self)
|