Re: [Python-Dev] PEP 455: TransformDict

2013-10-30 Thread Nigel Small
It strikes me that there could be an alternative approach to some of the
use cases discussed here. Instead of a new type of dictionary, the
case-insensitivity problem could be solved with something akin to a *
CaseInsensitiveString* class used for keys within a standard dictionary.
This would be very similar to a normal string except with comparison and
hashing. It would mean that CaseInsensitiveString("Foo") is considered
equal to CaseInsensitiveString("foo") and allow code such as the following:

>>> headers = {}
>>> headers[CaseInsensitiveString("content-type")] = "text/plain"
>>> headers[CaseInsensitiveString("Content-Type")]
"text/plain"

This would obviously also be usable in other places where case-insensitive
strings are required.

Just my two pence/cents/other minor currency units.
Nigel


On 30 October 2013 14:18, Ethan Furman  wrote:

> On 10/30/2013 12:12 AM, Raymond Hettinger wrote:
>
>>
>> Hopefully, this post will make the thought process more transparent.
>>
>
> Thanks, Raymond.  Your time is appreciated.
>
> --
> ~Ethan~
>
> __**_
> Python-Dev mailing list
> [email protected]
> https://mail.python.org/**mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/**mailman/options/python-dev/**
> nigel%40nigelsmall.com
>
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 455: TransformDict

2013-10-30 Thread Nigel Small
True, but I could similarly argue that the nice thing about
CaseInsensitiveString is it is usable for much more than dictionary keys -
it just depends on your point of view.

There would be nothing stopping other types of dictionary key
transformation being covered by other key data types in a similar way, I'm
simply trying to raise the question of where the genericity could sit: in
the dictionary or in the key.

Nigel


On 30 October 2013 17:04, Ethan Furman  wrote:

> On 10/30/2013 09:34 AM, Nigel Small wrote:
>
>>
>> It strikes me that there could be an alternative approach to some of the
>> use cases discussed here. Instead of a new type
>> of dictionary, the case-insensitivity problem could be solved with
>> something akin to a *CaseInsensitiveString* class [...]
>>
>
> The nice thing about the TransformDict is it is usable for much more than
> simple case-insensitivity.
>
>
> --
> ~Ethan~
> __**_
> Python-Dev mailing list
> [email protected]
> https://mail.python.org/**mailman/listinfo/python-dev<https://mail.python.org/mailman/listinfo/python-dev>
> Unsubscribe: https://mail.python.org/**mailman/options/python-dev/**
> nigel%40nigelsmall.com<https://mail.python.org/mailman/options/python-dev/nigel%40nigelsmall.com>
>
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Add a "transformdict" to collections

2013-09-10 Thread Nigel Small
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  wrote:

> On 10/09/2013 3:15pm, Armin Rigo wrote:
>
>> Hi Richard,
>>
>> On Tue, Sep 10, 2013 at 3:42 PM, Richard Oudkerk 
>> 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 "", 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
> [email protected]
> https://mail.python.org/**mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/**mailman/options/python-dev/**
> nigel%40nigelsmall.com
>
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com