I have a hashed identifier to put things in a dictionary:
class identifier():
def __init__(self, d):
self.my_dict = d
self.my_frozenset = frozenset(d.items())
def __getitem__(self, item):
return self.my_dict[item]
def __hash__(self):
return hash(self.my_frozenset)
def __eq__(self, rhs):
return self.my_frozenset == rhs.my_frozenset
def __ne__(self, rhs):
return not self == rhs
I have a node type that encapsulates an identifier for hashing and equality purposes:
class node:
def __init__(self, id, value):
self.id = id
self.value = value
def __hash__(self):
return hash(self.id)
def __eq__(self, rhs):
if isinstance(rhs, node):
return self.id == rhs.id
return self.id == rhs
def __ne__(self, rhs):
return not self == rhs
I put some nodes in the dictionary:
d = {}
n1 = node(identifier({'name':'Bob'}), value=1)
n2 = node(identifier({'name':'Alex'}), value=2)
n3 = node(identifier({'name':'Alex', 'nationality':'Japanese'}), value=3)
d[n1] = 'Node 1'
d[n2] = 'Node 2'
d[n3] = 'Node 3'
After a while, I only have an identifier:
my_id = identifier({'name':'Alex'})
Is there a way to efficiently look for a node that has been stored with this identifier in this dictionary?
Note that this is a little more complicated than it sounds; I know that I can use it trivially d[my_id]to get the associated item 'Node 2', but I want to effectively return the link ton2 .
, , d, , ( , ).
, dict hash eq node n2 , 'Node 2'. , my_id 'Node 2' n2 , .
. ( value), . , (networkX), , . , ( add node, node, , list, add edge .., ).
. !