Skip to content

Instantly share code, notes, and snippets.

@okram
Last active July 6, 2016 21:43
Show Gist options
  • Save okram/53d4eae84ab1c07b5d69b99e7432eaa1 to your computer and use it in GitHub Desktop.
Save okram/53d4eae84ab1c07b5d69b99e7432eaa1 to your computer and use it in GitHub Desktop.
~ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> # start GremlinServer
... # bin/gremlin-server.sh conf/gremlin-server-rest-modern.yaml
...
>>> import sys
>>> sys.path.append("/Users/marko/software/tinkerpop/gremlin-python/src/main/jython/gremlin_python")
>>> sys.path.append("/Users/marko/software/tinkerpop/gremlin-python/src/main/jython/gremlin_driver")
>>> sys.path.append("/Users/marko/software/tinkerpop/gremlin-python/src/main/jython/gremlin_rest_driver")
>>>
>>> import statics
>>> from graph_traversal import GraphTraversal
>>> from graph_traversal import GraphTraversalSource
>>> from graph_traversal import __
>>> from traversal import RawExpression
>>> from gremlin_rest_driver import RESTRemoteConnection
>>> from remote_graph import RemoteGraph
>>> from groovy_translator import GroovyTranslator
>>> from traversal import Operator
>>>
>>> # 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 = RemoteGraph(GroovyTranslator('g'), RESTRemoteConnection('http://localhost:8182'))
>>> graph
remotegraph[http://localhost:8182]
>>> g = graph.traversal()
>>> g
graphtraversalsource[remotegraph[http://localhost:8182]]
>>> # 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")
>>> g.V().repeat(__.out()).times(2).values("name")
g.V().repeat(__.out()).times(2).values("name")
>>>
>>> # toList()/toSet()/next()/etc. do the magic
...
>>> g.V().repeat(both()).times(2).name.toList()
[u'marko', u'marko', u'marko', u'marko', u'marko', u'marko', u'marko', u'josh', u'josh', u'josh', u'josh', u'josh', u'josh', u'josh', u'peter', u'peter', u'peter', u'ripple', u'ripple', u'ripple', u'lop', u'lop', u'lop', u'lop', u'lop', u'lop', u'lop', u'vadas', u'vadas', u'vadas']
>>> g.V().repeat(both()).times(2).name.toSet()
set([u'vadas', u'marko', u'josh', u'lop', u'ripple', u'peter'])
>>> g.V().repeat(out()).times(2).name.next()
u'ripple'
>>>
>>> # bindings
...
>>> g.V().out(("a","knows"),"created").name
g.V().out(a, "created").values("name")
>>> g.V().out(("a","knows"),"created").name.bindings
{'a': 'knows'}
>>> g.V().out(("a","knows"),"created").name.toList()
[u'vadas', u'josh', u'lop', u'ripple', u'lop', u'lop']
>>>
>>> # lambda (GroovyTranslator so using Groovy syntax lambas)
...
>>> g.V().out().map(lambda: "[it.get().value('name'),it.get().value('name').length()]")
g.V().out().map({[it.get().value('name'),it.get().value('name').length()]})
>>> g.V().out().map(lambda: "[it.get().value('name'),it.get().value('name').length()]").toList()
[[u'lop', 3], [u'vadas', 5], [u'josh', 4], [u'ripple', 6], [u'lop', 3], [u'lop', 3]]
>>>
>>> # all the source modulators work
...
>>> g.withComputer().V().out('created').valueMap()
g.withComputer().V().out("created").valueMap()
>>> g.withComputer().V().out('created').valueMap().toList()
[{u'lang': [u'java'], u'name': [u'lop']}, {u'lang': [u'java'], u'name': [u'lop']}, {u'lang': [u'java'], u'name': [u'lop']}, {u'lang': [u'java'], u'name': [u'ripple']}]
>>> g.withSack(0).V().repeat(outE().sack(sum,'weight').inV()).times(2).project('a','b').by('name').by(sack())
g.withSack(0).V().repeat(__.outE().sack(Operator.sum, "weight").inV()).times(2).project("a", "b").by("name").by(__.sack())
>>> g.withSack(0).V().repeat(outE().sack(sum,'weight').inV()).times(2).project('a','b').by('name').by(sack()).toList()
[{u'a': u'ripple', u'b': 2.0}, {u'a': u'lop', u'b': 1.4}]
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment