Search code examples
pythongremlinpython

How to map a python class to a gremlin vertex?


I had hoped tha mapping a class to a vertex in gremlin python would be as simple as:

def toVertex(self,g):
    t=g.addV(type(self))
    for name,value in vars(self).items():
        if debug:
            print("%s=%s" % (name,value))
        t=t.property(name,str(value))
    t.iterate()    

To avoid any type coercion problems i thought that str(value) would make sure all properties are treated as strings.

Unfortunately I get the error message:

  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gremlin_python/structure/io/graphsonV3d0.py", line 341, in dictify
    return writer.toDict(typ())
TypeError: __init__() missing 1 required positional argument: 'record'

What am i missing here - how can this be fixed?


Solution

  • The issues was that type does not return a string but a class and tried to call the constructor of the class which expected a record:

    the following code works even without using str() for the values:

    def toVertex(self,g):
        label=type(self).__name__;
        t=g.addV(label)
        for name,value in vars(self).items():
            if debug:
                print("%s=%s" % (name,value))
            t=t.property(name,value)
        t.iterate()