Skip to content

Instantly share code, notes, and snippets.

@okram
Created August 15, 2016 18:04
Show Gist options
  • Save okram/ced18257a6e9e9faffd97849ec1d20a8 to your computer and use it in GitHub Desktop.
Save okram/ced18257a6e9e9faffd97849ec1d20a8 to your computer and use it in GitHub Desktop.
# start GremlinServer
# bin/gremlin-server.sh conf/gremlin-server-rest-modern.yaml
from gremlin_python import statics
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 RawExpression
from gremlin_python.driver.rest_remote_connection import RESTRemoteConnection
from gremlin_python.structure.remote_graph import RemoteGraph
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
# 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
g = RemoteGraph(RESTRemoteConnection('http://localhost:8182','g')).traversal()
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'),x.get().value('name').length()]")
g.V().out().map(lambda: "lambda x: [x.get().value('name'),x.get().value('name').length()]").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