Mapping, with sequence as key, wildcard and subsequence matching

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Jul 16 09:24:08 CEST 2015


On 16/07/2015 08:09, Mark Lawrence wrote:
> On 16/07/2015 07:37, Ben Finney wrote:
>> Ethan Furman <ethan at stoneleaf.us> writes:
>>
>>> On 07/15/2015 10:53 PM, Ben Finney wrote:
>>>> Are those the ‘__contains__’, ‘__getitem__’ methods? What actually
>>>> is the API of a mapping type, that would need to be customised for
>>>> this application?
>>>
>>> The problem is that potential key matches are found by hashes
>>
>> For the Python ‘dict’ type, yes. I already know that I don't want that
>> type, I want a custom mapping type which matches keys according to the
>> algorithm I specify.
>>
>> So, I'm not asking “how do I make ‘dict’ do this?”. I am instead asking
>> “how do I implement a custom type which can duck-type for ‘dict’ but
>> have a different key-lookup implementation?”.
>>
>
> https://docs.python.org/3/reference/datamodel.html?emulating-container-types
>
>
> <quote>
> The collections module provides a MutableMapping abstract base class to
> help create those methods from a base set of __getitem__(),
> __setitem__(), __delitem__(), and keys()
> </quote>
>
> Hence
> https://docs.python.org/3/library/collections.abc.html#collections.abc.MutableMapping
>
>
> Hunting for examples got me to
> http://code.activestate.com/recipes/578096-a-mutablemapping-that-can-use-unhashable-objects-a/
> so fingers crossed, I'm just hoping that we're going in the right
> direction.
>

Drat, should have scrolled down one more page on the activestate recipe, 
Stephen D'Aprano comment.

<quote>
This is very ingenious, but I wouldn't want to touch it with a ten foot 
pole for production code
</quote>

Eric Snow replied.

<quote>
Oh yeah. I wouldn't use it in production either. :) I expect you could 
iron out the wrinkles, but there are better solutions.
</quote>

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Python-list mailing list