Dictionary, keys and alias

Cyril Bazin cyril.bazin at gmail.com
Mon Jul 18 10:30:12 EDT 2005


I think "hash" doesn't guarantee the unicity of the result. But, it should 
avoid the collisions...

>>> foo = "foo"
>>> hash(foo)
-740391237
>>> hash(-740391237)
-740391237

I think it's like some kind md5sum...

I propose this solution:

-----------------------------------------------------------
from UserDict import UserDict

class DoubleDict(UserDict):
def __init__(self, *args):
UserDict.__init__(self, *args)
self.values = {}
#self.aliases = {}

def setAlias(self, key, alias):
# Point the alias to the same hash as the real key.
if key in self:
self.data[alias] = self.data[key]
else:
self[alias] = key

def __getitem__(self, key):
return self.values[self.data[key]]

def __setitem__(self, key, value):
self.data.setdefault(key, key)
self.values[self.data[key]] = value

print "Result:"
d = DoubleDict()
d["a"] = 1
d["c"] = 4
d.setAlias("a", "b")
print d["a"], d["b"], d["c"]
d["a"] = 2
print d["a"], d["b"], d["c"]
d["b"] = 3
d.setAlias("b", "c")
print d["a"], d["b"], d["c"]
del d["c"]
d["c"] = 5
print d["a"], d["b"], d["c"]
del d["a"]
d["a"] = 6
print d["a"], d["b"], d["c"]

-------------------------------------------------------
Result: 
1 1 4
2 2 4
3 3 3
3 3 5
6 6 5

As you can see the last test (del "a" and reassign "a") fail because it 
reassign "b". 
But, if your application doesn't need to midify the aliases, it's ok (I 
think). Else you can redefine the method __delitem__.

Cyril

On 7/18/05, Steven D'Aprano <steve at removethiscyber.com.au> wrote:
> 
> On Mon, 18 Jul 2005 12:17:37 +0200, Glauco wrote:
> 
> > I want to insert a concept of alias in a dict_based class.
> >
> > The idea is to have a facoltative name in the same dict that correspond
> > at the same value. With this alias i can change original value.
> >
> > example:
> >
> > mydict['a'] = 1
> > I must define an alias example: myFunctAlias( mydict, 'a', 'b')
> > print mydict
> > {'a':1, 'b':1}
> > mydict['b'] = 2
> > print mydict
> > {'a':2, 'b':2}
> >
> >
> > The only idea i have is to implement two dictionary one for convert
> > name, alias in two keys with the same value (eg.numeric) in the first
> > dict. The second for store only one time the k, v .
> 
> You need some sort of redirection, something like this (untested):
> 
> class Doubledict:
> def __init__(self, **kargs):
> self.data = {}
> self.aliases = {}
> for key in kargs:
> # Point the key to a hash.
> self.aliases[key] = hash(key)
> # And point the hash at the value.
> self.data[hash(key)] = kargs[key]
> 
> def setalias(self, key, alias):
> # Point the alias to the same hash as the real key.
> self.aliases[alias] = hash(key)
> 
> def __getitem__(self, key):
> return self.data[self.aliases[key]]
> 
> def __setitem__(self, key, value):
> self.data[self.aliases[key]] = value
> 
> 
> The only niggly worry I have is I'm not sure when hash can be used, when
> it is unique, or even if is it guaranteed to be unique.
> 
> --
> Steven.
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050718/7a6e4897/attachment.html>


More information about the Python-list mailing list