You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by dk...@apache.org on 2016/09/15 10:35:47 UTC
[22/37] tinkerpop git commit: added Path object and GraphSON
desrialization for g:Path in Gremlin-Python. Added test cases to ensure both
Path semantics and Path deserialization.
added Path object and GraphSON desrialization for g:Path in Gremlin-Python. Added test cases to ensure both Path semantics and Path deserialization.
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/144a7cdd
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/144a7cdd
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/144a7cdd
Branch: refs/heads/TINKERPOP-1280
Commit: 144a7cdde6fa04c8a2897445cbea005d8a692869
Parents: 0e4e8a1
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Mon Sep 12 16:13:27 2016 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Mon Sep 12 16:13:27 2016 -0600
----------------------------------------------------------------------
.../jython/gremlin_python/structure/graph.py | 32 ++++++++++++++++++++
.../gremlin_python/structure/io/graphson.py | 16 +++++++++-
.../jython/tests/structure/io/test_graphson.py | 13 ++++++++
.../main/jython/tests/structure/test_graph.py | 27 ++++++++++++++++-
4 files changed, 86 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/144a7cdd/gremlin-python/src/main/jython/gremlin_python/structure/graph.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/gremlin_python/structure/graph.py b/gremlin-python/src/main/jython/gremlin_python/structure/graph.py
index 22403b6..b60dc65 100644
--- a/gremlin-python/src/main/jython/gremlin_python/structure/graph.py
+++ b/gremlin-python/src/main/jython/gremlin_python/structure/graph.py
@@ -88,3 +88,35 @@ class Property(object):
def __hash__(self):
return hash(self.key) + hash(self.value)
+
+
+class Path(object):
+ def __init__(self, labels, objects):
+ self.labels = labels
+ self.objects = objects
+
+ def __repr__(self):
+ return str(self.objects)
+
+ def __eq__(self, other):
+ return isinstance(other, self.__class__) and self.objects == other.objects and self.labels == other.labels
+
+ def __hash__(self):
+ return hash(str(self.objects)) + hash(str(self.labels))
+
+ def __getitem__(self, key):
+ if isinstance(key, str):
+ objects = []
+ for i, labels in enumerate(self.labels):
+ if key in labels:
+ objects.append(self.objects[i])
+ if 0 == len(objects):
+ raise Error("The step with label " + key + " does not exist")
+ return objects if len(objects) > 1 else objects[0]
+ elif isinstance(key, int):
+ return self.objects[key]
+ else:
+ raise Error("The step with label " + key + " does not exist")
+
+ def __len__(self):
+ return len(self.objects)
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/144a7cdd/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py
index dcc6976..fd13ae9 100644
--- a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py
+++ b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py
@@ -37,6 +37,7 @@ from gremlin_python.structure.graph import Edge
from gremlin_python.structure.graph import Property
from gremlin_python.structure.graph import Vertex
from gremlin_python.structure.graph import VertexProperty
+from gremlin_python.structure.graph import Path
class GraphSONWriter(object):
@@ -247,6 +248,18 @@ class PropertyDeserializer(GraphSONDeserializer):
return Property(value["key"], GraphSONReader._objectify(value["value"]))
+class PathDeserializer(GraphSONDeserializer):
+ def _objectify(self, dict):
+ value = dict[_SymbolHelper._VALUE]
+ labels = []
+ objects = []
+ for label in value["labels"]:
+ labels.append(set(label))
+ for object in value["objects"]:
+ objects.append(GraphSONReader._objectify(object))
+ return Path(labels, objects)
+
+
class _SymbolHelper(object):
symbolMap = {"global_": "global", "as_": "as", "in_": "in", "and_": "and",
"or_": "or", "is_": "is", "not_": "not", "from_": "from",
@@ -286,5 +299,6 @@ deserializers = {
"g:Vertex": VertexDeserializer(),
"g:Edge": EdgeDeserializer(),
"g:VertexProperty": VertexPropertyDeserializer(),
- "g:Property": PropertyDeserializer()
+ "g:Property": PropertyDeserializer(),
+ "g:Path": PathDeserializer()
}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/144a7cdd/gremlin-python/src/main/jython/tests/structure/io/test_graphson.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/tests/structure/io/test_graphson.py b/gremlin-python/src/main/jython/tests/structure/io/test_graphson.py
index 3153fd6..cae1a53 100644
--- a/gremlin-python/src/main/jython/tests/structure/io/test_graphson.py
+++ b/gremlin-python/src/main/jython/tests/structure/io/test_graphson.py
@@ -24,6 +24,7 @@ import unittest
from unittest import TestCase
from gremlin_python.structure.graph import Vertex
+from gremlin_python.structure.graph import Path
from gremlin_python.structure.io.graphson import GraphSONReader
from gremlin_python.structure.io.graphson import GraphSONWriter
@@ -67,6 +68,18 @@ class TestGraphSONReader(TestCase):
assert isinstance(vertex.id, int)
assert vertex == Vertex(1)
+ def test_path(self):
+ path = GraphSONReader.readObject(
+ """{"@type":"g:Path","@value":{"labels":[["a"],["b","c"],[]],"objects":[{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":1},"label":"person","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":0},"value":"marko","label":"name"}}],"age":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":1},"value":{"@type":"g:Int32","@value":29},"label":"age"}}]}}},{"@type":"g:Vertex","@value":{"id":{"@type":"g:Int32","@value":3},"label":"software","properties":{"name":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":4},"value":"lop","label":"name"}}],"lang":[{"@type":"g:VertexProperty","@value":{"id":{"@type":"g:Int64","@value":5},"value":"java","label":"lang"}}]}}},"lop"]}}"""
+ )
+ assert isinstance(path, Path)
+ assert "[v[1], v[3], u'lop']" == str(path)
+ assert Vertex(1) == path[0]
+ assert Vertex(1) == path["a"]
+ assert "lop" == path[2]
+ assert 3 == len(path)
+
+
class TestGraphSONWriter(TestCase):
def test_numbers(self):
assert """{"@type":"g:Int32","@value":1}""" == GraphSONWriter.writeObject(1)
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/144a7cdd/gremlin-python/src/main/jython/tests/structure/test_graph.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/tests/structure/test_graph.py b/gremlin-python/src/main/jython/tests/structure/test_graph.py
index 6025802..bb0d284 100644
--- a/gremlin-python/src/main/jython/tests/structure/test_graph.py
+++ b/gremlin-python/src/main/jython/tests/structure/test_graph.py
@@ -26,10 +26,11 @@ from gremlin_python.structure.graph import Edge
from gremlin_python.structure.graph import Property
from gremlin_python.structure.graph import Vertex
from gremlin_python.structure.graph import VertexProperty
+from gremlin_python.structure.graph import Path
class TestGraph(TestCase):
- def testGraphObjects(self):
+ def test_graph_objects(self):
vertex = Vertex(1)
assert "v[1]" == str(vertex)
assert "vertex" == vertex.label
@@ -69,6 +70,30 @@ class TestGraph(TestCase):
assert i == j
assert i.__hash__() == hash(i)
+ def test_path(self):
+ path = Path([set(["a", "b"]), set(["c", "b"]), set([])], [1, Vertex(1), "hello"])
+ assert "[1, v[1], 'hello']" == str(path)
+ assert 1 == path["a"]
+ assert Vertex(1) == path["c"]
+ assert [1, Vertex(1)] == path["b"]
+ assert path[0] == 1
+ assert path[1] == Vertex(1)
+ assert path[2] == "hello"
+ assert 3 == len(path)
+ try:
+ temp = path[3]
+ raise Exception("Accessing beyond the list index should throw an error")
+ except IndexError:
+ pass
+ #
+ assert path == path
+ assert hash(path) == hash(path)
+ path2 = Path([set(["a", "b"]), set(["c", "b"]), set([])], [1, Vertex(1), "hello"])
+ assert path == path2
+ assert hash(path) == hash(path2)
+ assert path != Path([set(["a"]), set(["c", "b"]), set([])], [1, Vertex(1), "hello"])
+ assert path != Path([set(["a", "b"]), set(["c", "b"]), set([])], [3, Vertex(1), "hello"])
+
if __name__ == '__main__':
unittest.main()