Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

caseless dict - questions

Question posted by: Phoe6 (Guest) on July 5th, 2008 01:05 AM
I have a requirement for using caseless dict. I searched the web for
many different implementations and found one snippet which was
implemented in minimal and useful way.

#############
import UserDict

class CaseInsensitiveDict(dict, UserDict.DictMixin):
def __init__(self, *args, **kwargs):
self.orig = {}
super(CaseInsensitiveDict, self).__init__(*args, **kwargs)
def items(self):
keys = dict.keys(self)
values = dict.values(self)
return [(self.orig[k],v) for k in keys for v in values]
def __setitem__(self, k, v):
hash_val = hash(k.lower())
self.orig[hash_val] = k
dict.__setitem__(self, hash_val, v)
def __getitem__(self, k):
return dict.__getitem__(self, hash(k.lower()))


obj = CaseInsensitiveDict()
obj['Name'] = 'senthil'
print obj
print obj.items()

obj1 = {}
obj1['Name'] = 'senthil'
print obj1
print obj1.items()
###########
[ors@goofy python]$ python cid1.py
{15034981: 'senthil'}
[('Name', 'senthil')]
{'Name': 'senthil'}
[('Name', 'senthil')]

---
The difference between the Caselessdict and {} is that when called as
the object, the Caselessdict() is giving me the internal
representation.
obj = CaseInsensitiveDict()
obj['Name'] = 'senthil'
print obj
gives: {15034981: 'senthil'}

obj1 = {}
obj1['Name'] = 'senthil'
print obj1
Correctly gives {'Name': 'senthil'}

What changes should I make to CaseInsensitiveDict ( written above), so
that its instance gives the actual dictionary instead of its internal
representation.
Constructing a dictionary and returning from __init__ method did not
work.

TIA,
Senthil


Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
David C. Ullrich's Avatar
David C. Ullrich
Guest
n/a Posts
July 7th, 2008
09:05 PM
#2

Re: caseless dict - questions
In article
<77798300-820f-4175-a9cb-aef639d42a32@m45g2000hsb.googlegroups.com>,
Phoe6 <orsenthil@gmail.comwrote:
Quote:
Originally Posted by
I have a requirement for using caseless dict. I searched the web for
many different implementations and found one snippet which was
implemented in minimal and useful way.
>
#############
import UserDict
>
class CaseInsensitiveDict(dict, UserDict.DictMixin):
def __init__(self, *args, **kwargs):
self.orig = {}
super(CaseInsensitiveDict, self).__init__(*args, **kwargs)
def items(self):
keys = dict.keys(self)
values = dict.values(self)


This items() can't be what anyone would want items
to be for a "caseless dict".
Quote:
Originally Posted by
return [(self.orig[k],v) for k in keys for v in values]
def __setitem__(self, k, v):
hash_val = hash(k.lower())
self.orig[hash_val] = k
dict.__setitem__(self, hash_val, v)
def __getitem__(self, k):
return dict.__getitem__(self, hash(k.lower()))
>
>
obj = CaseInsensitiveDict()
obj['Name'] = 'senthil'
print obj
print obj.items()
>
obj1 = {}
obj1['Name'] = 'senthil'
print obj1
print obj1.items()
###########
[ors@goofy python]$ python cid1.py
{15034981: 'senthil'}
[('Name', 'senthil')]
{'Name': 'senthil'}
[('Name', 'senthil')]
>
---
The difference between the Caselessdict and {} is that when called as
the object, the Caselessdict() is giving me the internal
representation.
obj = CaseInsensitiveDict()
obj['Name'] = 'senthil'
print obj
gives: {15034981: 'senthil'}
>
obj1 = {}
obj1['Name'] = 'senthil'
print obj1
Correctly gives {'Name': 'senthil'}
>
What changes should I make to CaseInsensitiveDict ( written above), so
that its instance gives the actual dictionary instead of its internal
representation.
Constructing a dictionary and returning from __init__ method did not
work.


It's not entirely clear to me what you want:
Since this is supposed to be a "caseless" dict,
I imagine that if you say

d['Name'] = 'first value'
d['name'] = 'new value'

then d['Name'] should now be 'new value'. Fine.
Now in that case exactly what do you want to see
when you print d? Do you want to see {'name':'new value'}
or {'name':'new value', 'Name': 'newvalue'}?
Quote:
Originally Posted by
TIA,
Senthil


--
David C. Ullrich

Jeff's Avatar
Jeff
Guest
n/a Posts
July 8th, 2008
11:55 AM
#3

Re: caseless dict - questions
Use the __str__ and __unicode__ methods to control the printed
representation of a class.

oj's Avatar
oj
Guest
n/a Posts
July 8th, 2008
03:15 PM
#4

Re: caseless dict - questions
On Jul 5, 1:57*am, Phoe6 <orsent...@gmail.comwrote:
Quote:
Originally Posted by
I have a requirement for using caseless dict. I searched the web for
many different implementations and found one snippet which was
implemented in minimal and useful way.
>
#############
import UserDict
>
class CaseInsensitiveDict(dict, UserDict.DictMixin):
* * def __init__(self, *args, **kwargs):
* * * * self.orig = {}
* * * * super(CaseInsensitiveDict, self).__init__(*args, **kwargs)
* * def items(self):
* * * * keys = dict.keys(self)
* * * * values = dict.values(self)
* * * * return [(self.orig[k],v) for k in keys for v in values]
* * def __setitem__(self, k, v):
* * * * hash_val = hash(k.lower())
* * * * self.orig[hash_val] = k
* * * * dict.__setitem__(self, hash_val, v)
* * def __getitem__(self, k):
* * * * return dict.__getitem__(self, hash(k.lower()))
>
obj = CaseInsensitiveDict()
obj['Name'] = 'senthil'
print obj
print obj.items()
>
obj1 = {}
obj1['Name'] = 'senthil'
print obj1
print obj1.items()
###########
[ors@goofy python]$ python cid1.py
{15034981: 'senthil'}
[('Name', 'senthil')]
{'Name': 'senthil'}
[('Name', 'senthil')]
>
---
The difference between the Caselessdict and {} is that when called as
the object, the Caselessdict() is giving me the internal
representation.
obj = CaseInsensitiveDict()
obj['Name'] = 'senthil'
print obj
gives: {15034981: 'senthil'}
>
obj1 = {}
obj1['Name'] = 'senthil'
print obj1
Correctly gives {'Name': 'senthil'}
>
What changes should I make to CaseInsensitiveDict ( written above), so
that its instance gives the actual dictionary instead of its internal
representation.
Constructing a dictionary and returning from __init__ method did not
work.
>
TIA,
Senthil


What I think you need to do, is define a __repr__(self) method (see
http://docs.python.org/ref/customization.html)

Something like:

def __repr__(self):
return dict(self.items())

I /think/ will work. I haven't tested it though. This isn't exactly
what repr is supposed to do - evaling it won't give you the correct
object back. Defining __str__ might be a better approach.

-Oli

 
Not the answer you were looking for? Post your question . . .
182,080 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors