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 3 1408
In article
<77**********************************@m45g2000hsb. googlegroups.com>,
Phoe6 <or*******@gmail.comwrote:
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".
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'}?
TIA,
Senthil
--
David C. Ullrich
Use the __str__ and __unicode__ methods to control the printed
representation of a class.
On Jul 5, 1:57*am, Phoe6 <orsent...@gmail.comwrote:
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: bearophileHUGS |
last post by:
I'm frequently using Py2.4 sets, I find them quite useful, and I like
them, even if they seem a little slower than dicts. Sets also need the
same memory of dicts (can they be made to use less...
|
by: Drew |
last post by:
When is it appropriate to use dict.items() vs dict.iteritems. Both
seem to work for something like:
for key,val in mydict.items():
print key,val
for key,val in mydict.iteritems():
print...
|
by: Stef Mientki |
last post by:
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...
|
by: ssecorp |
last post by:
I did nce(I think).
class X
X.__dict__() and ngot a dict of its variables.
Now i get errors doing this. what am i doing wrong?
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |