473,732 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

caseless dict - questions

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 CaseInsensitive Dict(dict, UserDict.DictMi xin):
def __init__(self, *args, **kwargs):
self.orig = {}
super(CaseInsen sitiveDict, self).__init__( *args, **kwargs)
def items(self):
keys = dict.keys(self)
values = dict.values(sel f)
return [(self.orig[k],v) for k in keys for v in values]
def __setitem__(sel f, k, v):
hash_val = hash(k.lower())
self.orig[hash_val] = k
dict.__setitem_ _(self, hash_val, v)
def __getitem__(sel f, k):
return dict.__getitem_ _(self, hash(k.lower()) )
obj = CaseInsensitive Dict()
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 = CaseInsensitive Dict()
obj['Name'] = 'senthil'
print obj
gives: {15034981: 'senthil'}

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

What changes should I make to CaseInsensitive Dict ( 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
Jul 5 '08 #1
3 1423
In article
<77************ *************** *******@m45g200 0hsb.googlegrou ps.com>,
Phoe6 <or*******@gmai l.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 CaseInsensitive Dict(dict, UserDict.DictMi xin):
def __init__(self, *args, **kwargs):
self.orig = {}
super(CaseInsen sitiveDict, self).__init__( *args, **kwargs)
def items(self):
keys = dict.keys(self)
values = dict.values(sel f)
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__(sel f, k, v):
hash_val = hash(k.lower())
self.orig[hash_val] = k
dict.__setitem_ _(self, hash_val, v)
def __getitem__(sel f, k):
return dict.__getitem_ _(self, hash(k.lower()) )
obj = CaseInsensitive Dict()
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 = CaseInsensitive Dict()
obj['Name'] = 'senthil'
print obj
gives: {15034981: 'senthil'}

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

What changes should I make to CaseInsensitive Dict ( 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
Jul 7 '08 #2
Use the __str__ and __unicode__ methods to control the printed
representation of a class.
Jul 8 '08 #3
oj
On Jul 5, 1:57*am, Phoe6 <orsent...@gmai l.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 CaseInsensitive Dict(dict, UserDict.DictMi xin):
* * def __init__(self, *args, **kwargs):
* * * * self.orig = {}
* * * * super(CaseInsen sitiveDict, self).__init__( *args, **kwargs)
* * def items(self):
* * * * keys = dict.keys(self)
* * * * values = dict.values(sel f)
* * * * return [(self.orig[k],v) for k in keys for v in values]
* * def __setitem__(sel f, k, v):
* * * * hash_val = hash(k.lower())
* * * * self.orig[hash_val] = k
* * * * dict.__setitem_ _(self, hash_val, v)
* * def __getitem__(sel f, k):
* * * * return dict.__getitem_ _(self, hash(k.lower()) )

obj = CaseInsensitive Dict()
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 = CaseInsensitive Dict()
obj['Name'] = 'senthil'
print obj
gives: {15034981: 'senthil'}

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

What changes should I make to CaseInsensitive Dict ( 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
Jul 8 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2150
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 memory, not storing values? Maybe this requires too much code rewriting). I presume such sets are like this because they are kind of dicts. If this is true, then converting a dict to a set (that means converting just the keys; this is often useful for...
19
37270
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 key,val Also, when is it appropriate to use range() vs xrange(). From my
12
1662
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 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", "F", "++", T)
2
1117
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?
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.