caseless dictionary howto ?

Steven Bethard steven.bethard at gmail.com
Wed Jun 20 00:40:10 CEST 2007


Stef Mientki wrote:
> Evan Klitzke wrote:
>> On 6/19/07, Stef Mientki <S.Mientki-nospam at mailbox.kun.nl> wrote:
>>> hello,
>>>
>>> I need to search a piece of text and make all words that are equal
>>> (except their case) also equal in their case, based on the first 
>>> occurrence.
>>> So I'm using a dictionary to store names and attributes of objects.
>>> As as I need to search on the caseless name (so I've choosen lowercase),
>>> My dictionairy looks like this:
>>>
>>>      self.procs [ "serial_hw_read"  ] = ( "Serial_HW_Read", "F", 
>>> "++", T)
>>>
>>> Is this really a good solution,
>>> or are there better ways ?
>>
>> Since you want an almost dictionary, you should create a new class
>> that inherits from dict and overloads the methods that you want to
>> change.
> 
> Suppose I succeed in creating my own type, derived from dictionary,
> I would be able to add/set a key like this:
> 
>     self.procs [ "Serial_HW_Read"  ] = ( "F", "++", T)
> 
> and now I can query the dictionary by
> 
>     self.procs.has_key ( "serial_hw_read")
> 
> but how do I get the original string "Serial_HW_Read" back ?

I probably wouldn't inherit from dict -- you're going to need to 
redefine most of the methods anyway. I'd do something like::

 >>> class D(object):
...     def __init__(self):
...         self._dict = {}
...         self._original_keys = {}
...     def __iter__(self):
...         for key in self._dict:
...             yield self._original_keys[key]
...     def __contains__(self, key):
...         return key.lower() in self._dict
...     def __getitem__(self, key):
...         return self._dict[key.lower()]
...     def __setitem__(self, key, value):
...         self._dict[key.lower()] = value
...         if key not in self._original_keys:
...             self._original_keys[key.lower()] = key
...
 >>> procs = D()
 >>> procs['Serial_HW_Read'] = 'F', '++', 'T'
 >>> 'serial_hw_read' in procs
True
 >>> procs['SErial_Hw_reAD']
('F', '++', 'T')
 >>> list(procs)
['Serial_HW_Read']

Note that because I store the first form encountered for every key, I 
can always use the lower-case version to retrieve the "original string".

STeVe



More information about the Python-list mailing list