lisa-lab/pylearn2 — pull request #1043 by madisonmay (2014-07-24 21:12:38+00:00)
Title: Revamp KeyAwareDefaultDict

The logic in the old implementation of `KeyAwareDefaultDict` meant that although missing keys were passed to the dictionary `default_factory` as arguments, trying to access valid keys in the dictionary always raised a `KeyError`. The modified version subclasses `collections.defaultdict` and overrides `__missing__` for a rather clean and simple implementation of `KeyAwareDefaultDict`.

```python
>>> from pylearn2.utils.key_aware import KeyAwareDefaultDict
>>> d = KeyAwareDefaultDict(lambda x: x*2)
>>> print d[2]
4
>>> d[2] = 5
>>> d[2]
5
```