| 1 |
from copy import deepcopy |
| 2 |
|
| 3 |
|
| 4 |
def immutable(self, *_args, **_kwargs): |
| 5 |
r""" |
| 6 |
Function for not implemented method since the object is immutable |
| 7 |
""" |
| 8 |
|
| 9 |
raise AttributeError( |
| 10 |
f"'{self.__class__.__name__}' object is read-only" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
_empty_frozendict = None |
| 15 |
_module_name = "frozendict" |
| 16 |
|
| 17 |
|
| 18 |
# noinspection PyPep8Naming |
| 19 |
class frozendict(dict): |
| 20 |
r""" |
| 21 |
A simple immutable dictionary. |
| 22 |
|
| 23 |
The API is the same as `dict`, without methods that can change the |
| 24 |
immutability. In addition, it supports __hash__(). |
| 25 |
""" |
| 26 |
|
| 27 |
__slots__ = ( |
| 28 |
"_hash", |
| 29 |
) |
| 30 |
|
| 31 |
@classmethod |
| 32 |
def fromkeys(cls, *args, **kwargs): |
| 33 |
r""" |
| 34 |
Identical to dict.fromkeys(). |
| 35 |
""" |
| 36 |
|
| 37 |
return cls(dict.fromkeys(*args, **kwargs)) |
| 38 |
|
| 39 |
# noinspection PyMethodParameters |
| 40 |
def __new__(e4b37cdf_d78a_4632_bade_6f0579d8efac, *args, **kwargs): |
| 41 |
cls = e4b37cdf_d78a_4632_bade_6f0579d8efac |
| 42 |
|
| 43 |
has_kwargs = bool(kwargs) |
| 44 |
continue_creation = True |
| 45 |
self = None |
| 46 |
|
| 47 |
# check if there's only an argument and it's of the same class |
| 48 |
if len(args) == 1 and not has_kwargs: |
| 49 |
it = args[0] |
| 50 |
|
| 51 |
# no isinstance, to avoid subclassing problems |
| 52 |
if it.__class__ == frozendict and cls == frozendict: |
| 53 |
self = it |
| 54 |
continue_creation = False |
| 55 |
|
| 56 |
if continue_creation: |
| 57 |
self = dict.__new__(cls, *args, **kwargs) |
| 58 |
|
| 59 |
dict.__init__(self, *args, **kwargs) |
| 60 |
|
| 61 |
# empty singleton - start |
| 62 |
|
| 63 |
if self.__class__ == frozendict and not len(self): |
| 64 |
global _empty_frozendict |
| 65 |
|
| 66 |
if _empty_frozendict is None: |
| 67 |
_empty_frozendict = self |
| 68 |
else: |
| 69 |
self = _empty_frozendict |
| 70 |
continue_creation = False |
| 71 |
|
| 72 |
# empty singleton - end |
| 73 |
|
| 74 |
if continue_creation: |
| 75 |
object.__setattr__(self, "_hash", -1) |
| 76 |
|
| 77 |
return self |
| 78 |
|
| 79 |
# noinspection PyMissingConstructor |
| 80 |
def __init__(self, *args, **kwargs): |
| 81 |
pass |
| 82 |
|
| 83 |
def __hash__(self, *args, **kwargs): |
| 84 |
r""" |
| 85 |
Calculates the hash if all values are hashable, otherwise |
| 86 |
raises a TypeError. |
| 87 |
""" |
| 88 |
|
| 89 |
if self._hash != -1: |
| 90 |
_hash = self._hash |
| 91 |
else: |
| 92 |
fs = frozenset(self.items()) |
| 93 |
_hash = hash(fs) |
| 94 |
|
| 95 |
object.__setattr__(self, "_hash", _hash) |
| 96 |
|
| 97 |
return _hash |
| 98 |
|
| 99 |
def __repr__(self, *args, **kwargs): |
| 100 |
r""" |
| 101 |
Identical to dict.__repr__(). |
| 102 |
""" |
| 103 |
|
| 104 |
body = super().__repr__(*args, **kwargs) |
| 105 |
klass = self.__class__ |
| 106 |
|
| 107 |
if klass == frozendict: |
| 108 |
name = f"{_module_name}.{klass.__name__}" |
| 109 |
else: |
| 110 |
name = klass.__name__ |
| 111 |
|
| 112 |
return f"{name}({body})" |
| 113 |
|
| 114 |
def copy(self): |
| 115 |
r""" |
| 116 |
Return the object itself, as it's an immutable. |
| 117 |
""" |
| 118 |
|
| 119 |
klass = self.__class__ |
| 120 |
|
| 121 |
if klass == frozendict: |
| 122 |
return self |
| 123 |
|
| 124 |
return klass(self) |
| 125 |
|
| 126 |
def __copy__(self, *args, **kwargs): |
| 127 |
r""" |
| 128 |
See copy(). |
| 129 |
""" |
| 130 |
|
| 131 |
return self.copy() |
| 132 |
|
| 133 |
def __deepcopy__(self, memo, *args, **kwargs): |
| 134 |
r""" |
| 135 |
As for tuples, if hashable, see copy(); otherwise, it returns a |
| 136 |
deepcopy. |
| 137 |
""" |
| 138 |
|
| 139 |
klass = self.__class__ |
| 140 |
return_copy = klass == frozendict |
| 141 |
|
| 142 |
if return_copy: |
| 143 |
try: |
| 144 |
hash(self) |
| 145 |
except TypeError: |
| 146 |
return_copy = False |
| 147 |
|
| 148 |
if return_copy: |
| 149 |
return self.copy() |
| 150 |
|
| 151 |
tmp = deepcopy(dict(self)) |
| 152 |
|
| 153 |
return klass(tmp) |
| 154 |
|
| 155 |
def __reduce__(self, *args, **kwargs): |
| 156 |
r""" |
| 157 |
Support for `pickle`. |
| 158 |
""" |
| 159 |
|
| 160 |
return (self.__class__, (dict(self),)) |
| 161 |
|
| 162 |
def set(self, key, val): |
| 163 |
new_self = dict(self) |
| 164 |
new_self[key] = val |
| 165 |
|
| 166 |
return self.__class__(new_self) |
| 167 |
|
| 168 |
def setdefault(self, key, default=None): |
| 169 |
if key in self: |
| 170 |
return self |
| 171 |
|
| 172 |
new_self = dict(self) |
| 173 |
|
| 174 |
new_self[key] = default |
| 175 |
|
| 176 |
return self.__class__(new_self) |
| 177 |
|
| 178 |
def delete(self, key): |
| 179 |
new_self = dict(self) |
| 180 |
del new_self[key] |
| 181 |
|
| 182 |
if new_self: |
| 183 |
return self.__class__(new_self) |
| 184 |
|
| 185 |
return self.__class__() |
| 186 |
|
| 187 |
def _get_by_index(self, collection, index): |
| 188 |
try: |
| 189 |
return collection[index] |
| 190 |
except IndexError: |
| 191 |
maxindex = len(collection) - 1 |
| 192 |
name = self.__class__.__name__ |
| 193 |
raise IndexError( |
| 194 |
f"{name} index {index} out of range {maxindex}" |
| 195 |
) from None |
| 196 |
|
| 197 |
def key(self, index=0): |
| 198 |
collection = tuple(self.keys()) |
| 199 |
|
| 200 |
return self._get_by_index(collection, index) |
| 201 |
|
| 202 |
def value(self, index=0): |
| 203 |
collection = tuple(self.values()) |
| 204 |
|
| 205 |
return self._get_by_index(collection, index) |
| 206 |
|
| 207 |
def item(self, index=0): |
| 208 |
collection = tuple(self.items()) |
| 209 |
|
| 210 |
return self._get_by_index(collection, index) |
| 211 |
|
| 212 |
def __setitem__(self, key, val, *args, **kwargs): |
| 213 |
raise TypeError( |
| 214 |
f"'{self.__class__.__name__}' object doesn't support item " |
| 215 |
"assignment" |
| 216 |
) |
| 217 |
|
| 218 |
def __delitem__(self, key, *args, **kwargs): |
| 219 |
raise TypeError( |
| 220 |
f"'{self.__class__.__name__}' object doesn't support item " |
| 221 |
"deletion" |
| 222 |
) |
| 223 |
|
| 224 |
|
| 225 |
def frozendict_or(self, other, *_args, **_kwargs): |
| 226 |
res = {} |
| 227 |
res.update(self) |
| 228 |
res.update(other) |
| 229 |
|
| 230 |
return self.__class__(res) |
| 231 |
|
| 232 |
|
| 233 |
frozendict.__or__ = frozendict_or |
| 234 |
frozendict.__ior__ = frozendict_or |
| 235 |
|
| 236 |
try: |
| 237 |
# noinspection PyStatementEffect |
| 238 |
frozendict.__reversed__ |
| 239 |
except AttributeError: # pragma: no cover |
| 240 |
def frozendict_reversed(self, *_args, **_kwargs): |
| 241 |
return reversed(tuple(self)) |
| 242 |
|
| 243 |
|
| 244 |
frozendict.__reversed__ = frozendict_reversed |
| 245 |
|
| 246 |
frozendict.clear = immutable |
| 247 |
frozendict.pop = immutable |
| 248 |
frozendict.popitem = immutable |
| 249 |
frozendict.update = immutable |
| 250 |
frozendict.__delattr__ = immutable |
| 251 |
frozendict.__setattr__ = immutable |
| 252 |
frozendict.__module__ = _module_name |
| 253 |
|
| 254 |
__all__ = (frozendict.__name__,) |