Created
December 7, 2013 10:14
-
-
Save mklymyshyn/7839340 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 arango import create | |
from arango.aql import F, V | |
def dataset(): | |
""" | |
Basic dataset | |
""" | |
a.database.create() | |
a.users.create() | |
a.knows.create(type=a.COLLECTION_EDGES) | |
for u in range(10): | |
a.users.docs.create({ | |
"name": "user_{}".format(u), | |
"age": u + 20, | |
"gender": u % 2 == 0}) | |
def relations(a): | |
""" | |
Relations based on dataset | |
""" | |
rels = ( | |
(0, 1), (0, 2), (2, 3), (4, 3), (3, 5), | |
(5, 1), (0, 5), (5, 6), (6, 7), (7, 8), (9, 8)) | |
get_user = lambda id: a.users.query.filter( | |
"obj.name == 'user_{}'".format(id)).execute().first | |
for f, t in rels: | |
what = "user_{} knows user_{}".format(f, t) | |
from_doc, to_doc = get_user(f), get_user(t) | |
a.knows.edges.create(from_doc, to_doc, {"what": what}) | |
print ("{}->{}: {}".format(from_doc.id, to_doc.id, what)) | |
def querying(a): | |
""" | |
FOR p IN PATHS(users, knows, 'outbound') | |
FILTER p.source.name == 'user_5' | |
RETURN p.vertices[*].name | |
""" | |
for data in a.knows.query.over( | |
F.PATHS("users", "knows", ~V("outbound")))\ | |
.filter("obj.source.name == '{}'".format("user_5"))\ | |
.result("obj.vertices[*].name")\ | |
.execute(wrapper=lambda c, i: i): | |
print (data) | |
a = create(db="experiments") | |
dataset(a) | |
relations(a) | |
querying(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment