hashables.py
11.6 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"""This module proposes hashable version of the mutable containers
`list`, `dict` and `set`, called respectively `hlist`, `hdict` and
`hset`.
After one object has been hashed, it becomes not mutable and raises a
`ValueError` if a method which changes the object (let call a
_mutation_ such a method) is invoqued. The object can be then
un-hashed by calling `unhash` on it so that it becomes mutable again.
Notice that this may cause troubles if the object is stored in a set
or dict which uses its hashcode to locate it. Notice also that
hashable containers cannot be hashed if they contain non hashable
objects.
>>> l = hlist(range(5))
>>> l
hlist([0, 1, 2, 3, 4])
>>> _ = hash(l)
>>> l.append(5)
Traceback (most recent call last):
...
ValueError: hashed 'hlist' object is not mutable
>>> unhash(l)
>>> l.append(5)
>>> l
hlist([0, 1, 2, 3, 4, 5])
Testing if a `hlist` is in a `set`, `dict`, `hset` or `hdict` causes
its hashing. If this is not desirable, you should either compensate
with a call to `unhash`, or test if a copy is in the set:
>>> s = set()
>>> s.add(list(range(4)))
Traceback (most recent call last):
...
TypeError: ...
>>> s.add(hlist(range(4)))
>>> l = hlist(range(3))
>>> l in s
False
>>> l.append(3)
Traceback (most recent call last):
...
ValueError: hashed 'hlist' object is not mutable
>>> unhash(l)
>>> l.append(3)
>>> l[:] in s
True
>>> l.append(4)
"""
import inspect
from operator import xor
from snakes.compat import *
def unhash (obj) :
"""Make the object mutable again. This should be used with
caution, especially if the object is stored in a dict or a set.
>>> l = hlist(range(3))
>>> _ = hash(l)
>>> l.append(3)
Traceback (most recent call last):
...
ValueError: hashed 'hlist' object is not mutable
>>> unhash(l)
>>> l.append(3)
@param obj: any object
@type obj: `object`
"""
try :
del obj._hash
except :
pass
# apidoc skip
def hashable (cls) :
"""Wrap methods in a class in order to make it hashable.
"""
classname, bases, classdict = cls.__name__, cls.__bases__, cls.__dict__
for name, attr in classdict.items() :
try :
doc = inspect.getdoc(attr)
if doc is None :
attr.__doc__ = getattr(bases[0], name).__doc__
else :
attr.__doc__ = "\n".join([inspect.getdoc(getattr(bases[0],
name)),
doc])
except :
pass
def __hash__ (self) :
if not hasattr(self, "_hash") :
self._hash = self.__hash__.HASH(self)
return self._hash
__hash__.HASH = classdict["__hash__"]
__hash__.__doc__ = classdict["__hash__"].__doc__
cls.__hash__ = __hash__
def __mutable__ (self) :
"Raise `ValueError` if the %s has been hashed."
if self.hashed() :
raise ValueError("hashed '%s' object is not mutable" % classname)
try :
__mutable__.__doc__ = __mutable__.__doc__ % classname
except :
pass
cls.__mutable__ = __mutable__
def hashed (self) :
"Return 'True' if the %s has been hashed, 'False' otherwise."
return hasattr(self, "_hash")
try :
hashed.__doc__ = hashed.__doc__ % classname
except :
pass
cls.hashed = hashed
def mutable (self) :
"Return 'True' if the %s is not hashed, 'False' otherwise."
return not self.hashed()
try :
mutable.__doc__ = mutable.__doc__ % classname
except :
pass
cls.mutable = mutable
return cls
class hlist (list) :
"""Hashable lists. They support all standard methods from `list`.
>>> l = hlist(range(5))
>>> l
hlist([0, 1, 2, 3, 4])
>>> l.append(5)
>>> l
hlist([0, 1, 2, 3, 4, 5])
>>> _ = hash(l)
>>> l.append(6)
Traceback (most recent call last):
...
ValueError: hashed 'hlist' object is not mutable
>>> unhash(l)
>>> l.append(6)
"""
# apidoc stop
def __add__ (self, other) :
return self.__class__(list.__add__(self, other))
def __delitem__ (self, item) :
self.__mutable__()
list.__delitem__(self, item)
def __delslice__ (self, *l, **d) :
self.__mutable__()
list.__delslice__(self, *l, **d)
def __getslice__ (self, first, last) :
return self.__class__(list.__getslice__(self, first, last))
def __getitem__ (self, item) :
ret = list.__getitem__(self, item)
if ret.__class__ is list :
return self.__class__(ret)
return ret
def __hash__ (self) :
return hash(tuple(self))
def __iadd__ (self, other) :
return self.__class__(list.__iadd__(self, other))
def __imul__ (self, n) :
return self.__class__(list.__imul__(self, n))
def __mul__ (self, n) :
return self.__class__(list.__mul__(self, n))
def __repr__ (self) :
"""
>>> repr(hlist(range(3)))
'hlist([0, 1, 2])'
"""
return "%s(%s)" % (self.__class__.__name__, list.__repr__(self))
def __rmul__ (self, n) :
return self._class__(list.__rmul__(self, n))
def __setitem__ (self, index, item) :
self.__mutable__()
list.__setitem__(self, index, item)
def __setslice__ (self, first, last, item) :
self.__mutable__()
list.__setslice__(self, first, last, item)
def append (self, item) :
self.__mutable__()
list.append(self, item)
def extend (self, iterable) :
self.__mutable__()
list.extend(self, iterable)
def insert (self, index, item) :
self.__mutable__()
list.insert(self, index, item)
def pop (self, index=-1) :
self.__mutable__()
return list.pop(self, index)
def remove (self, item) :
self.__mutable__()
list.remove(self, item)
def reverse (self) :
self.__mutable__()
list.reverse(self)
def sort (self, cmp=None, key=None, reverse=False) :
self.__mutable__()
list.sort(self, cmp, key, reverse)
hlist = hashable(hlist)
class hdict (dict) :
"""Hashable dictionnaries. They support all standard methods from
`dict`.
>>> l = hlist(range(5))
>>> d = hdict([(l, 0)])
>>> d
hdict({hlist([0, 1, 2, 3, 4]): 0})
>>> l in d
True
>>> [0, 1, 2, 3, 4] in d
Traceback (most recent call last):
...
TypeError: ...
>>> hlist([0, 1, 2, 3, 4]) in d
True
>>> d[hlist([0, 1, 2, 3, 4])]
0
>>> l.append(5)
Traceback (most recent call last):
...
ValueError: hashed 'hlist' object is not mutable
>>> _ = hash(d)
>>> d.pop(l) # any mutation would produce the same error
Traceback (most recent call last):
...
ValueError: hashed 'hdict' object is not mutable
>>> unhash(d)
>>> d.pop(l)
0
"""
# apidoc stop
def __delitem__ (self, key) :
self.__mutable__()
dict.__delitem__(self, key)
def __hash__ (self) :
"""
>>> _ = hash(hdict(a=1, b=2))
"""
# 252756382 = hash("snakes.hashables.hlist")
return reduce(xor, (hash(i) for i in self.items()), 252756382)
def __repr__ (self) :
"""
>>> repr(hdict(a=1))
"hdict({'a': 1})"
"""
return "%s(%s)" % (self.__class__.__name__, dict.__repr__(self))
def __setitem__ (self, key, item) :
self.__mutable__()
dict.__setitem__(self, key, item)
def clear (self) :
self.__mutable__()
dict.clear(self)
def copy (self) :
return self.__class__(dict.copy(self))
@classmethod
def fromkeys (_class, *args) :
return _class(dict.fromkeys(*args))
def pop (self, *args) :
self.__mutable__()
return dict.pop(self, *args)
def popitem (self) :
self.__mutable__()
return dict.popitem(self)
def setdefault (self, key, item=None) :
self.__mutable__()
return dict.setdefault (self, key, item)
def update (self, other, **more) :
self.__mutable__()
return dict.update(self, other, **more)
hdict = hashable(hdict)
class hset (set) :
"""Hashable sets. They support all standard methods from `set`.
>>> s = hset()
>>> l = hlist(range(5))
>>> s.add(l)
>>> s
hset([hlist([0, 1, 2, 3, 4])])
>>> l in s
True
>>> [0, 1, 2, 3, 4] in s
Traceback (most recent call last):
...
TypeError: ...
>>> hlist([0, 1, 2, 3, 4]) in s
True
>>> l.append(5)
Traceback (most recent call last):
...
ValueError: hashed 'hlist' object is not mutable
>>> _ = hash(s)
>>> s.discard(l) # any mutation would produce the same error
Traceback (most recent call last):
...
ValueError: hashed 'hset' object is not mutable
>>> unhash(s)
>>> s.discard(l)
"""
# apidoc stop
def __and__ (self, other) :
return self.__class__(set.__and__(self, other))
def __hash__ (self) :
"""
>>> _ = hash(hset([1, 2, 3]))
>>> _ = hash(hset(range(5)) - set([0, 4]))
"""
# 196496309 = hash("snakes.hashables.hset")
return reduce(xor, (hash(x) for x in self), 196496309)
def __iand__ (self, other) :
return self.__class__(set.__iand__(self, other))
def __ior__ (self, other) :
return self.__class__(set.__ior__(self, other))
def __isub__ (self, other) :
return self.__class__(set.__isub__(self, other))
def __ixor__ (self, other) :
return self.__class__(set.__ixor__(self, other))
def __or__ (self, other) :
return self.__class__(set.__or__(self, other))
def __rand__ (self, other) :
return self.__class__(set.__rand__(self, other))
def __repr__ (self) :
"""
>>> repr(hset([1]))
'hset([1])'
"""
return "%s([%s])" % (self.__class__.__name__,
set.__repr__(self)[6:-2])
def __ror__ (self, other) :
return self.__class__(set.__ror__(self, other))
def __rsub__ (self, other) :
return self.__class__(set.__rsub__(self, other))
def __rxor__ (self, other) :
return self.__class__(set.__rxor__(self, other))
def __str__ (self) :
return self.__class__.__name__ + "(" + set.__str__(self).split("(", 1)[1]
def __sub__ (self, other) :
return self.__class__(set.__sub__(self, other))
def __xor__ (self, other) :
return self.__class__(set.__xor__(self, other))
def add (self, item) :
self.__mutable__()
set.add(self, item)
def clear (self) :
self.__mutable__()
set.clear(self)
def copy (self) :
return self.__class__(set.copy(self))
def difference (self, other) :
return self.__class__(set.difference(self, other))
def difference_update (self, other) :
self.__mutable__()
set.difference_update(self, other)
def discard (self, item) :
self.__mutable__()
set.discard(self, item)
def intersection (self, other) :
return self._class__(set.intersection(self, other))
def intersection_update (self, other) :
self.__mutable__()
set.intersection_update(self, other)
def pop (self) :
self.__mutable__()
return set.pop(self)
def remove (self, item) :
self.__mutable__()
set.remove(self, item)
def symmetric_difference (self, other) :
self.__mutable__()
set.symmetric_difference(self, other)
def symmetric_difference_update (self, other) :
self.__mutable__()
set.symmetric_difference_update(self, other)
def union (self, other) :
return self.__class__(set.union(self, other))
def update (self, other) :
self.__mutable__()
set.update(self, other)
hset = hashable(hset)