case insensitive dictionary

J

John Henry

I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option. I found some work done by others
to do that at:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/283455

but the problem with that approach is that they lowercase the keys
immediately when you create the dictionary and so the true identity of
the key is lost.

Of course, I can subclass it and save a copy of the "real" key but
that's kind of messcy.

In other words:

If I have:

pets=caselessDict()

pets["Cat"] = 3
pets["Dog"] = 2

I would like to see:

pets["cat"] prints 3
pets["DOG"] prints 2

but

print pets.keys()

should print:

"Cat", "Dog"

not:

"cat", "dog"
 
R

Rob Williscroft

John Henry wrote in @l39g2000cwd.googlegroups.com in comp.lang.python:
I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option.

class idict( dict ):

class __istr( str ):

def __eq__( self, other ):
return self.lower() == other.lower()

def __hash__( self ):
return self.lower().__hash__()

def __setitem__( self, k, v ):
dict.__setitem__( self, idict.__istr( k ), v )

d = idict( a = 1, b = 2 )
d['A'] = 3

print d

Rob.
 
V

vbgunz

John said:
I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option. I found some work done by others
to do that at:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/283455

but the problem with that approach is that they lowercase the keys
immediately when you create the dictionary and so the true identity of
the key is lost.

Of course, I can subclass it and save a copy of the "real" key but
that's kind of messcy.

In other words:

If I have:

pets=caselessDict()

pets["Cat"] = 3
pets["Dog"] = 2

I would like to see:

pets["cat"] prints 3
pets["DOG"] prints 2

but

print pets.keys()

should print:

"Cat", "Dog"

not:

"cat", "dog"

You can try to title-case the list returned with keys() like so:
print [x.title() for x in pets.keys()]

Not a perfect solution but you get what you want... just an idea :)
 
T

tomasz.kulawik.groups.1

John Henry napisal(a):
I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option. I found some work done by others
to do that at:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/283455

but the problem with that approach is that they lowercase the keys
immediately when you create the dictionary and so the true identity of
the key is lost.

Of course, I can subclass it and save a copy of the "real" key but
that's kind of messcy.

In other words:

If I have:

pets=caselessDict()

pets["Cat"] = 3
pets["Dog"] = 2

I would like to see:

pets["cat"] prints 3
pets["DOG"] prints 2

but

print pets.keys()

should print:

"Cat", "Dog"

not:

"cat", "dog"

How about:
.... def __hash__(self):
.... return str.__hash__(self.lower())
.... def __eq__(self,other):
.... return self.lower()==other.lower()
........ def __setitem__(self, key, value):
.... if isinstance(key, str):
.... dict.__setitem__(self, Dictstr(key), value)
.... else:
.... dict.__setitem__(self, key, value)
.... def __getitem__(self, key):
.... if isinstance(key, str):
.... return dict.__getitem__(self, Dictstr(key))
.... else:
.... return dict.__getitem__(self, key)
....
d=dictt()
d[1]=1
d['Cat']='Cat'
d['Dog']='Dog'
d['dOg'] 'Dog'
d.keys()
[1, 'Dog', 'Cat']

Note that you would need to redefine also __init__, __contains__ and
other methods.
 
J

John Henry

I don't think that's sufficient. See how many methods the author of
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/283455 had to
redefine.

Rob said:
John Henry wrote in @l39g2000cwd.googlegroups.com in comp.lang.python:
I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option.

class idict( dict ):

class __istr( str ):

def __eq__( self, other ):
return self.lower() == other.lower()

def __hash__( self ):
return self.lower().__hash__()

def __setitem__( self, k, v ):
dict.__setitem__( self, idict.__istr( k ), v )

d = idict( a = 1, b = 2 )
d['A'] = 3

print d

Rob.
 
J

J. Clifford Dyer

John said:
print pets.keys()

should print:

"Cat", "Dog"

If you do:

Py> pets['Cat'] = 2
Py> pets['Dog'] = 3
Py> pets['DOG'] = 4
Py> pets['cat'] += 5
Py> pets.keys()

What should the result be?

"['Cat', 'Dog']" or "['cat', 'DOG']"?


That is to say, if you use a new case in redefining the values of your
case insensitive dictionary, does the key take on the new case, or will
it always and forever be the case originally given to it?

Cheers,
Cliff
 
F

Fuzzyman

John said:
I believe the standard dictionary should be amened to allow the use of
case insensitive keys - as an option. I found some work done by others
to do that at:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/283455

but the problem with that approach is that they lowercase the keys
immediately when you create the dictionary and so the true identity of
the key is lost.

A later version of this class does give you access to the original case
:

http://www.voidspace.org.uk/python/archive.shtml#caseless

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
J

John Henry

I believe that if you redefine the value, the key should not change.
So, yes, I would expect that they value of the key to remain as they
were.


J. Clifford Dyer said:
John said:
print pets.keys()

should print:

"Cat", "Dog"

If you do:

Py> pets['Cat'] = 2
Py> pets['Dog'] = 3
Py> pets['DOG'] = 4
Py> pets['cat'] += 5
Py> pets.keys()

What should the result be?

"['Cat', 'Dog']" or "['cat', 'DOG']"?


That is to say, if you use a new case in redefining the values of your
case insensitive dictionary, does the key take on the new case, or will
it always and forever be the case originally given to it?

Cheers,
Cliff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top