Search code examples
pythonloopsgremlintinkerpopgremlinpython

Adding multiple vertices, changing one property-value (tinkerpop3 + python GLV)


I'm trying to size down my code, but I need several instances of data of this type:

g.addV('A').property('a-type','thing-x').property('a-value',1).next()
g.addV('A').property('a-type','thing-x').property('a-value',2).next()
...   
g.addV('A').property('a-type','thing-x').property('a-value',n).next()

Up to 'a-value' being n (for instance 50).

# I tried a version of a loop I found here 
# https://stackoverflow.com/questions/40907529/create-multiple-edges-having-vertex-id-number-0-to-49
# 
g.inject((0..<50).toArray()).as('i').addV('a-value',select('i')).iterate()

But I get an error:

g.inject((0..<50).toArray()).as('i').addV('a-value',select('i')).iterate()
             ^
    SyntaxError: invalid syntax

What would be the proper way to do this?

EDIT: Having tried to build upon the answer, I'd just like to add that for my case, calling t.iterate() within the for-loop gives the expected result, but not so if this is called outside of the loop, as mentioned below.


Solution

  • The example you found was being used inside the Gremlin console and hence using Groovy constructs. From Python you would do something like:

    for i in range(1,51):
        g.addV('test').property('mykey',i).iterate()
    

    However that will add the vertices one at a time so would be better to write them in small batches in a way similar to this.

    t = g.addV('test').property('mykey',1)
    for i in range(2,51):
        t.addV('test').property('mykey',i)
    t.iterate()