Object as vertex attribute

I am trying to create a graph with iGraph whose vertices have some attributes, like an object from a class that I defined. Its constructor is as follows:

class Tauler:

def __init__(self, num_files, num_columnes):
    self.files, self.columnes = num_files, num_columnes
    self.tauler = {i: [0] * num_columnes for i in range(num_files)}

When I try to assign a Tauler object (t1) as an attribute of some vertex, I get the following error:

g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": t1})
Traceback (most recent call last):

  File "<ipython-input-10-9167be7715d9>", line 1, in <module>
    g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": t1})

  File "C:\Users\Stohl\anaconda3\lib\site-packages\igraph\__init__.py", line 232, in __init__
    self.vs[key] = value

TypeError: object of type 'Tauler' has no len()

If I then try to define the len method like so:

def __len__(self):
    return self.tauler

I get the next error:

g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": t1})
Traceback (most recent call last):

  File "<ipython-input-13-9167be7715d9>", line 1, in <module>
    g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": t1})

  File "C:\Users\Stohl\anaconda3\lib\site-packages\igraph\__init__.py", line 232, in __init__
    self.vs[key] = value

TypeError: 'dict' object cannot be interpreted as an integer

If I try to use the pickle module, then the following happens:

g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": pickle.dumps(t1)})
g.vs[0]
igraph.Vertex(<igraph.Graph object at 0x000000000960CB88>, 0, {'tauler': 128})
# Why 128?? Why it is even an int?
pickle.loads(g.vs[0]["tauler"])
Traceback (most recent call last):

  File "<ipython-input-30-3b1c9e9c841a>", line 1, in <module>
    pickle.loads(g.vs[0]["tauler"])

TypeError: a bytes-like object is required, not 'int'

However, if I use the pickle.dumps() at the IPython shell, it returns a bytes-object and not an int. What is the problem here? What am I missing? Any help would be much appreciated.

I think you should supply a list. Try

g = igraph.Graph(n=1, directed=True, vertex_attrs={"tauler": [t1]})
1 Like

Yes, it worked! Many thanks :slight_smile: