(original) (raw)
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:Seems to work for me:
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).
\>>> 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 "", line 1, in
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
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/nigel%40nigelsmall.com