Re: Dictionary, keys and alias

2005-07-19 Thread Terry Reedy

"Steven D'Aprano" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> On Tue, 19 Jul 2005 10:20:04 +0200, Glauco wrote:
>
>>> 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.
>>>
>>
>> Thank Steve, the idea was the same...
>> but yours using hash is much elegant.
>
> I'm still worried about hash of two unrelated objects returning the same
> value.
>
> Another implementation is to keep a hidden attribute of the object, and
> initialise it to the integer 0. Instead of using hash(key), you use the
> current value of the integer, then increment the integer by one.
>
> This is guaranteed to be unique, no matter what.

id(ob) is already guaranteed to be a unique integer while ob exists.  So, 
if I understand the goal, map possibly-multiple-keys each to id and id to 
ob.

Terry J. Reedy



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary, keys and alias

2005-07-19 Thread Cyril Bazin
Glauco, 

Be careful if you decide to use hash. 
There is possibility of bugs due to that approach, (if hash(x) == hash(y) and x != y).
Even if the probability of bug is near 0, your computer will certainly recall you what is the murphy law.
If I were you, I would prefer another approach. 

Cyril
On 7/19/05, Glauco <[EMAIL PROTECTED]> wrote:
Steven D'Aprano 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.>Thank Steve, the idea was the same...
but yours using hash is much elegant.Thank youGlauco--  \\\|///\\  -
-  //
(  @ @  )+-oOOo-( )-oOOo--+||| I have a dream that one day this nation will rise up and   ||   live out the true meaning of its creed: "We hold these   |
|   truths to be self-evident:that all men are created equal.|| I have a dream that one day on the red hills of Georgia||   the sons of former slaves and the sons of former ||   slaveowners will be able to sit down together at a table |
|  
of
brotherhood.  || I have a dream that one day even the state of Mississippi, ||   a desert state, sweltering with the heat of injustice||   and oppression, will be transformed into an oasis of |
|  
freedom and
justice.
|| I have a dream that my four children will one day live in  ||   a nation where they will not be judged by the color of   ||   their skin but by the content of their character.||
I have a dream
today.  ||||Martin
Luther King, Jr  28 Ago 1963 |++|glauco(at)uriland.it||  www.uriland.it  .oooOICQ:
115323690   |+- (   )-- Oooo.-+
\ ((   )  \_))
/(_/--http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary, keys and alias

2005-07-19 Thread Steven D'Aprano
On Tue, 19 Jul 2005 10:20:04 +0200, Glauco wrote:

>> 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.
>> 
> 
> Thank Steve, the idea was the same...
> but yours using hash is much elegant.

I'm still worried about hash of two unrelated objects returning the same
value.

Another implementation is to keep a hidden attribute of the object, and
initialise it to the integer 0. Instead of using hash(key), you use the
current value of the integer, then increment the integer by one.

This is guaranteed to be unique, no matter what.


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary, keys and alias

2005-07-19 Thread Glauco
Steven D'Aprano 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.
> 

Thank Steve, the idea was the same...
but yours using hash is much elegant.

Thank you
Glauco

-- 

  \\\|///
\\  - -  //
 (  @ @  )
+-oOOo-( )-oOOo--+
||
| I have a dream that one day this nation will rise up and   |
|   live out the true meaning of its creed: "We hold these   |
|   truths to be self-evident:that all men are created equal.|
| I have a dream that one day on the red hills of Georgia|
|   the sons of former slaves and the sons of former |
|   slaveowners will be able to sit down together at a table |
|   of brotherhood.  |
| I have a dream that one day even the state of Mississippi, |
|   a desert state, sweltering with the heat of injustice|
|   and oppression, will be transformed into an oasis of |
|   freedom and justice. |
| I have a dream that my four children will one day live in  |
|   a nation where they will not be judged by the color of   |
|   their skin but by the content of their character.|
| I have a dream today.  |
||
|Martin Luther King, Jr  28 Ago 1963 |
++
|glauco(at)uriland.it|
|  www.uriland.it  .oooOICQ: 115323690   |
+- (   )-- Oooo.-+
 \ ((   )
  \_)) /
(_/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary, keys and alias

2005-07-18 Thread Cyril Bazin
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 <[EMAIL PROTECTED]> 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]] = valueThe only niggly worry I have is I'm not sure when hash can be used, whenit is unique, or even if is it guaranteed to be unique.--Steven.--
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary, keys and alias

2005-07-18 Thread Steven D'Aprano
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


Re: Dictionary, keys and alias

2005-07-18 Thread Peter Hansen
Glauco wrote:
> I want to insert a concept of alias in a dict_based class.
...
> Any suggestion ?

Yes, in future don't attempt to start a new thread using "Reply" unless 
you are happy not having your post read by all those who have already 
"killed" the thread to which you replied.  Anyone who was not interested 
in the Python Programming Contest thread is unlikely to have seen your 
question.

-Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Dictionary, keys and alias

2005-07-18 Thread Glauco
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 .


Any suggestion ?
Glauco



-- 

  \\\|///
\\  - -  //
 (  @ @  )
+-oOOo-( )-oOOo--+
||
| I have a dream that one day this nation will rise up and   |
|   live out the true meaning of its creed: "We hold these   |
|   truths to be self-evident:that all men are created equal.|
| I have a dream that one day on the red hills of Georgia|
|   the sons of former slaves and the sons of former |
|   slaveowners will be able to sit down together at a table |
|   of brotherhood.  |
| I have a dream that one day even the state of Mississippi, |
|   a desert state, sweltering with the heat of injustice|
|   and oppression, will be transformed into an oasis of |
|   freedom and justice. |
| I have a dream that my four children will one day live in  |
|   a nation where they will not be judged by the color of   |
|   their skin but by the content of their character.|
| I have a dream today.  |
||
|Martin Luther King, Jr  28 Ago 1963 |
++
|glauco(at)uriland.it|
|  www.uriland.it  .oooOICQ: 115323690   |
+- (   )-- Oooo.-+
 \ ((   )
  \_)) /
(_/
-- 
http://mail.python.org/mailman/listinfo/python-list