Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 import collections | |
|
Vadim Sh.
2014/06/27 18:26:22
nit: \n
iannucci
2014/06/28 08:33:35
Done.
| |
| 5 import operator | |
| 6 | |
| 7 | |
| 8 def freeze(obj): | |
|
Vadim Sh.
2014/06/27 18:26:22
if |obj| is already frozen, it makes a copy. In th
iannucci
2014/06/28 08:33:35
Don't think this is the case
| |
| 9 """Takes a jsonish object |obj|, and returns an immutable version of it.""" | |
|
Vadim Sh.
2014/06/27 18:26:22
There's no asserts anywhere that verify it's 'json
iannucci
2014/06/28 08:33:35
Clarified in the comment
| |
| 10 if isinstance(obj, dict): | |
| 11 return FrozenDict((freeze(k), freeze(v)) for k, v in obj.iteritems()) | |
| 12 elif isinstance(obj, list): | |
|
Vadim Sh.
2014/06/27 18:26:22
tuples with non-frozen items will not be frozen.
iannucci
2014/06/28 08:33:35
Done.
| |
| 13 return tuple(freeze(i) for i in obj) | |
| 14 elif isinstance(obj, set): | |
| 15 return frozenset(freeze(i) for i in obj) | |
| 16 else: | |
| 17 hash(obj) | |
|
Vadim Sh.
2014/06/27 18:26:21
assert getattr(obj, '__hash__' , None), 'Can\'t fr
iannucci
2014/06/28 08:33:35
it recurses, so 'guarantees' that all objects insi
| |
| 18 return obj | |
| 19 | |
| 20 | |
| 21 def thaw(obj): | |
| 22 """Takes an object from freeze() and returns a mutable copy of it.""" | |
| 23 if isinstance(obj, FrozenDict): | |
| 24 return collections.OrderedDict( | |
| 25 (thaw(k), thaw(v)) for k, v in obj.iteritems()) | |
| 26 elif isinstance(obj, tuple): | |
| 27 return list(thaw(i) for i in obj) | |
|
Vadim Sh.
2014/06/27 18:26:22
distinction between tuples and lists will be lost
iannucci
2014/06/28 08:33:35
disinclined to care... that seems like a much hard
| |
| 28 elif isinstance(obj, frozenset): | |
| 29 return set(thaw(i) for i in obj) | |
| 30 else: | |
| 31 return obj | |
| 32 | |
| 33 | |
| 34 class FrozenDict(collections.Mapping): | |
| 35 """An immutable OrderedDict. | |
| 36 | |
| 37 Modified From: http://stackoverflow.com/a/2704866 | |
| 38 """ | |
| 39 def __init__(self, *args, **kwargs): | |
| 40 self._d = collections.OrderedDict(*args, **kwargs) | |
| 41 self._hash = reduce(operator.xor, | |
|
Vadim Sh.
2014/06/27 18:26:22
Why not make it lazy?
iannucci
2014/06/28 08:33:35
Added comment
| |
| 42 (hash(i) for i in enumerate(self._d.iteritems())), 0) | |
| 43 | |
| 44 def __eq__(self, other): | |
| 45 if not isinstance(other, collections.Mapping): | |
| 46 return NotImplemented | |
| 47 if self is other: | |
| 48 return True | |
| 49 if len(self) != len(other): | |
| 50 return False | |
| 51 for k, v in self.iteritems(): | |
| 52 if k not in other or other[k] != v: | |
| 53 return False | |
| 54 return True | |
| 55 | |
| 56 def __iter__(self): | |
| 57 return iter(self._d) | |
| 58 | |
| 59 def __len__(self): | |
| 60 return len(self._d) | |
| 61 | |
| 62 def __getitem__(self, key): | |
| 63 return self._d[key] | |
| 64 | |
| 65 def __hash__(self): | |
| 66 return self._hash | |
| 67 | |
| 68 def __repr__(self): | |
| 69 return 'FrozenDict(%r)' % (self._d.items(),) | |
| OLD | NEW |