Last active
April 20, 2016 04:06
-
-
Save emehrkay/68a9e64789826f6a59e8b5c837dd6ce4 to your computer and use it in GitHub Desktop.
Marko's example wrapping Gremlinpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from gremlinpy.gremlin import * | |
from gremlinpy.gremlin import _, __ | |
class PythonStringGraphTraversalSource(object): | |
def __init__(self, graph_variable): | |
self.graph_variable = graph_variable | |
self.gremlin = Gremlin(self.graph_variable) | |
def V(self, id=None): | |
return PythonStringGraphTraversal(self.gremlin).V(id) | |
def E(self, id=None): | |
return PythonStringGraphTraversal(self.gremlin).E(id) | |
def __getattr__(self, attr): | |
self.get_attr = attr | |
return self | |
def __call__(self, *args): | |
gremlin = Gremlin('') | |
gremlin.__getattr__(self.get_attr) | |
gremlin.__call__(*args) | |
return gremlin | |
class PythonStringGraphTraversal(object): | |
def __init__(self, gremlin): | |
self.gremlin = gremlin | |
self.params = {} | |
def V(self, id=None): | |
args = (id,) if id else () | |
self.gremlin.V(*args) | |
return self | |
def _as(self, *labels): | |
self.gremlin.func('as', *labels) | |
return self | |
def has(self, *args): | |
self.gremlin.has(*args) | |
return self | |
def hasLabel(self, label): | |
self.gremlin.hasLabel('"hasLabel"', label) | |
return self | |
def out(self, *edge_labels): | |
# typical .func(arg, arg1, arg2) would only bind the last one, unbound binds none of them | |
self.gremlin.unbound('out', *edge_labels) | |
return self | |
def values(self, *property_keys): | |
self.gremlin.unbound('values', *property_keys) | |
return self | |
def valueMap(self, *args): | |
self.gremlin.unbound('valueMap', *args) | |
return self | |
def where(self, traversal): | |
self.gremlin.unbound('where', traversal) | |
return self | |
def match(self, *traversals): | |
self.gremlin.unbound('match', *traversals) | |
return self | |
def map(self, closure): | |
self.gremlin.unbound('map', closure) | |
return self | |
def __str__(self): | |
return self.__unicode__() | |
def __unicode__(self): | |
string = str(self.gremlin) | |
self.params = self.gremlin.bound_params | |
self.gremlin.reset() | |
return string | |
def __getattr__(self, attr): | |
self.gremlin.__getattr__(attr) | |
return self | |
def __call__(self, *args): | |
self.gremlin.__call__(*args) | |
return self | |
# try it out | |
g = PythonStringGraphTraversalSource("g") | |
# simple warmup | |
marko = g.V().has("name","marko") | |
print(str(marko), marko.params) | |
g.V().has("name", "marko") | |
# one has()-method, but varargs parsing is smart | |
x = g.V().has("person","name","marko") | |
print(str(x), x.params) | |
y = g.V().has("person", "name", "marko") | |
print(str(y), y.params) | |
# # strings and numbers mixed | |
z = g.V().has("person","age",32) | |
print(str(z), z.params) | |
q = g.V().has("person", "age", 32) | |
print(str(q), q.params) | |
# nested anonymous traversal | |
blank = Gremlin('') | |
x = g.V().where(g.out('knows')) | |
print(str(x), x.params) | |
u = g.V().where(__.out("'knows'")) | |
print(str(u), u.params) | |
# as() is reserved in Python, so _as() is used. | |
a = g.V()._as("a").out("created")._as("b").where(AS("a").out("knows")) | |
print(str(a), a.params) | |
# # multi-traversal match() | |
x = g.V().match(AS("a").out("knows")._as("b"), AS("b").out("knows")._as("a")) | |
print(str(x), x.params) | |
#g.V().match(__.as("a").out("knows").as("b"), __.as("b").out("knows").as("a")) | |
# # P-predicates and .name-sugar (attribute access interception) | |
u = g.V().hasLabel("person").has("age",gt(30)).out("created","knows").name | |
print(str(u), u.params) | |
# g.V().hasLabel("person").has("age", P.gt(30)).out("created", "knows").values("name") | |
# | |
# # smart about boolean conversion | |
# g.V().valueMap(True,"name","age") | |
# g.V().valueMap(true, "name", "age") | |
# | |
# # lambdas -- ghetto as its not a Python lambda, but a Groovy lambda string | |
it = Gremlin('').it.get().value('name') | |
i = g.V().map(it) | |
print(str(i), i.params) | |
# g.V().map(it.get().value("name")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment