Could a more generic variant of this class work? In the same way that `sorted` can accept a comparison function, similar could be done for a dictionary-like class:

d = transformdict(key=str.lower)

Strictly speaking, this would provide case-insensitive but not case-preserving behaviour. For any given use case, though, a function could instead be supplied to "normalise" the key (upper, lower, title case, etc) in a way that fits that case. I can't think of many real cases where multiple types of capitalisation would be useful within the same dictionary.

Nigel



On 10 September 2013 15:40, Richard Oudkerk <shibturn@gmail.com> wrote:
On 10/09/2013 3:15pm, Armin Rigo wrote:
Hi Richard,

On Tue, Sep 10, 2013 at 3:42 PM, Richard Oudkerk <shibturn@gmail.com> wrote:
I guess another example is creating an "identity dict" (see
http://code.activestate.com/lists/python-ideas/7161/) by doing

     d = transformdict(id)

This is bogus, because only the id will be stored, and the original
key object will be forgotten (and its id probably reused).

Seems to work for me:

>>> import collections
>>> d = collections.transformdict(id)
>>> L = [1,2,3]
>>> d[L] = None
>>> L in d
True
>>> [1,2,3] in d
False
>>> print(d[L])
None
>>> d._data
{41444136: ([1, 2, 3], None)}
>>> list(d)
[[1, 2, 3]]

However __repr__() is broken:

>>> d
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Repos\cpython-dirty\lib\collections\abc.py", line 444, in __repr__
    return '{0.__class__.__name__}({0._mapping!r})'.format(self)
  File "C:\Repos\cpython-dirty\lib\collections\__init__.py", line 944, in __repr__
    self._transform, repr(dict(self)))
TypeError: unhashable type: 'list'

--
Richard