OLD | NEW |
(Empty) | |
| 1 __all__ = ('hashkey', 'typedkey') |
| 2 |
| 3 |
| 4 class _HashedTuple(tuple): |
| 5 |
| 6 __hashvalue = None |
| 7 |
| 8 def __hash__(self, hash=tuple.__hash__): |
| 9 hashvalue = self.__hashvalue |
| 10 if hashvalue is None: |
| 11 self.__hashvalue = hashvalue = hash(self) |
| 12 return hashvalue |
| 13 |
| 14 def __add__(self, other, add=tuple.__add__): |
| 15 return _HashedTuple(add(self, other)) |
| 16 |
| 17 def __radd__(self, other, add=tuple.__add__): |
| 18 return _HashedTuple(add(other, self)) |
| 19 |
| 20 _kwmark = (object(),) |
| 21 |
| 22 |
| 23 def hashkey(*args, **kwargs): |
| 24 """Return a cache key for the specified hashable arguments.""" |
| 25 |
| 26 if kwargs: |
| 27 return _HashedTuple(args + sum(sorted(kwargs.items()), _kwmark)) |
| 28 else: |
| 29 return _HashedTuple(args) |
| 30 |
| 31 |
| 32 def typedkey(*args, **kwargs): |
| 33 """Return a typed cache key for the specified hashable arguments.""" |
| 34 |
| 35 key = hashkey(*args, **kwargs) |
| 36 key += tuple(type(v) for v in args) |
| 37 key += tuple(type(v) for _, v in sorted(kwargs.items())) |
| 38 return key |
OLD | NEW |