Class point is defined as (there are also some methods, atributes, and stuff in it, but this is minimal part):
class point():
def ___init___(self, x, y):
self.x = x
self.y = y
So, I saw this question, but when I tried applying it, it returns an error:
G = nx.Graph()
p = point(0,0)
G.add_node(0, p)
NetworkXError: The attr_dict argument must be a dictionary.
If i use
G = nx.Graph()
p = point(0,0)
G.add_node(0, data = p)
I don't get an error, but when i try to access the x-coordinate, it turns out it didn't save it as a point.
G[0].x
returns: AttributeError: 'dict' object has no attribute 'x'
doing
G = nx.Graph()
G.add_node(0, data = point(0,0))
G[0]
returns: {}
which means it still saves it as a dictionary.
I saw I can make my points hashable, and use these objects as nodes, so i added atribute id, since points are going to move. I added this to the class, and __repr__ for nice drawing of the graphs:
def __hash__(self):
return self.id_n
def __cmp__(self, p):
if self.id_n < p.id_n: return -1
elif self.id_n == p.id_n: return 0
else: return 1
def __eq__(self, p):
if p.id_n == self.id_n: return True
else: return False
def __repr__(self):
return str(self.id_n)
but that is a bit wierd, since I don't understand how to select a node then, by
G[<what should i put here?>]
So, question is, what is a proper way to do this?
I hoped to be able to use something like
G[node_id].some_method(some_args)