Skip to content

Instantly share code, notes, and snippets.

@okram
Created August 23, 2016 23:09
Show Gist options
  • Save okram/a78091de307cad3932651465dc3d6b62 to your computer and use it in GitHub Desktop.
Save okram/a78091de307cad3932651465dc3d6b62 to your computer and use it in GitHub Desktop.
# start GremlinServer
# bin/gremlin-server.sh -i org.apache.tinkerpop gremlin-python 3.2.2-SNAPSHOT
# bin/gremlin-server.sh conf/gremlin-server-modern-py.yaml
from gremlin_python.process.graph_traversal import GraphTraversal
from gremlin_python.process.graph_traversal import GraphTraversalSource
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.traversal import Operator
from gremlin_python.process.graphson import GraphSONWriter
from gremlin_python.process.graphson import serializers
from gremlin_python.process.traversal import Bytecode
from gremlin_python.process.traversal import Bindings
from gremlin_python.process.traversal import P
from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
# this allows us to do g.V().repeat(out()) instead of g.V().repeat(__.out())-type traversals
statics.load_statics(globals())
# create a remote connection (using REST) and pass it to GraphTraversalSource
graph = Graph()
g = graph.traversal().withRemote(DriverRemoteConnection('ws://localhost:8182','g'))
g.withSack(1).V().out("knows").repeat(out("created").has(label,within("person","dog")._and(eq(1)))).times(2)
GraphSONWriter.writeObject(g.V().out("knows","created").times(2).limit(10))
GraphSONWriter.writeObject(g.V().has("age",gt(10)._and(lt(20))).out("knows","created").repeat(out()).times(2).limit(10).groupCount().by(label))
# the __repr__ of PythonGraphTraversal is its Gremlin-Groovy compiled form
g.V().repeat(out()).times(2).name
g.V().repeat(__.out()).times(2).values("name")
# toList()/toSet()/next()/etc. do the magic
g.V().repeat(both()).times(2).name.toList()
g.V().repeat(both()).times(2).name.toSet()
g.V().repeat(out()).times(2).name.next()
# bindings
g.V().out(("a","knows"),"created").name
g.V().out(("a","knows"),"created").name.bytecode.bindings
g.V().out(("a","knows"),"created").name.toList()
# lambda
g.V().out().map(lambda: "lambda x: (x.get().value('name'),len(x.get().value('name')))")
g.V().out().map(lambda: "lambda x: (x.get().value('name'),len(x.get().value('name')))").toList()
g.V().out().map(lambda: "x: (x.get().value('name'),len(x.get().value('name')))").toList()
# all the source modulators work
g.withComputer().V().out('created').valueMap()
g.withComputer().V().out('created').valueMap().toList()
g.withSack(0).V().repeat(outE().sack(sum,'weight').inV()).times(2).project('a','b').by('name').by(sack()).toList()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment