Yet Another Case Question
Lulu of the Lotus-Eaters
mertz at gnosis.cx
Sun Feb 23 03:17:53 EST 2003
"Tim Churches" <tchur at optushome.com.au> wrote previously:
|You haven't had enough conversations with seven year olds lately. It's
|not self-evident at all.
|>>> count == Count
|NameError
And THIS doesn't confuse a seven year old?! Heck, it confuses me... and
I'm old.
|Python, you would need two types of dictionary hashing mechanisms: a
|case-sensitive one for use when values are the keys, and a
|case-insensitive one for use when the names of things are the keys.
This is easy though. Here's a quicky:
class InsensitiveDict(dict):
def upper(self, x):
try: return x.upper()
except AttributeError: return x
def __contains__(self, x):
return dict.__contains__(self, self.upper(x))
def __delitem__(self, x):
dict.__delitem__(self, self.upper(x))
def __getitem__(self, x):
return dict.__getitem__(self, self.upper(x))
def __setitem__(self, k, v):
return dict.__setitem__(self, self.upper(k), v)
Using it is easy enough:
>>> id = InsensitiveDict()
>>> id['foo'] = 1
>>> id['baR'] = 2
>>> id['FOO'] = 3
>>> id
{'FOO': 3, 'BAR': 2}
>>> id['foo']
3
>>> id['fOO']
3
>>> del id['Foo']
>>> id
{'BAR': 2}
In the language FOO, just use an InsensitiveDict for internal
dictionaries, but still provide dict to FOO programmers. Every problem
solved. Well, maybe a slight speed penalty for FOO versus Python... but
the implementation could be optimized.
Yours, Lulu...
--
_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: Postmodern Enterprises _/_/_/
_/_/ ~~~~~~~~~~~~~~~~~~~~[mertz at gnosis.cx]~~~~~~~~~~~~~~~~~~~~~ _/_/
_/_/ The opinions expressed here must be those of my employer... _/_/
_/_/_/_/_/_/_/_/_/_/ Surely you don't think that *I* believe them! _/_/
More information about the Python-list
mailing list