You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2019/09/04 13:22:19 UTC

[tinkerpop] 09/40: Added Dict/Set serialization for graphbinary in python

This is an automated email from the ASF dual-hosted git repository.

spmallette pushed a commit to branch TINKERPOP-2279
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit 51c1fa2bb819c82011c5c9b19e77abca47259b82
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Fri Jul 26 14:19:34 2019 -0400

    Added Dict/Set serialization for graphbinary in python
---
 .../gremlin_python/structure/io/graphbinaryV1.py   | 39 ++++++++++++++++++++++
 .../tests/structure/io/test_graphbinaryV1.py       | 23 +++++++++++--
 2 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py
index 4a5dd0e..3d7d7da 100644
--- a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py
+++ b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py
@@ -58,6 +58,8 @@ class DataType(Enum):
     double = 0x07
     float = 0x08
     list = 0x09
+    map = 0x0a
+    set = 0x0b
 
 
 class GraphBinaryTypeType(type):
@@ -311,3 +313,40 @@ class ListIO(_GraphBinaryTypeIO):
             size = size - 1
 
         return the_list
+
+
+class SetIO(ListIO):
+
+    python_type = SetType
+    graphbinary_type = DataType.set
+
+    @classmethod
+    def objectify(cls, buff, reader):
+        return set(ListIO.objectify(buff, reader))
+
+
+class MapIO(_GraphBinaryTypeIO):
+
+    python_type = dict
+    graphbinary_type = DataType.map
+
+    @classmethod
+    def dictify(cls, obj, writer):
+        map_data = bytearray()
+        for k, v in obj.items():
+            map_data.extend(writer.writeObject(k))
+            map_data.extend(writer.writeObject(v))
+
+        return cls.as_bytes(cls.graphbinary_type, len(obj), map_data)
+
+    @classmethod
+    def objectify(cls, buff, reader):
+        size = cls.read_int(buff)
+        the_dict = {}
+        while size > 0:
+            k = reader.readObject(buff)
+            v = reader.readObject(buff)
+            the_dict[k] = v
+            size = size - 1
+
+        return the_dict
diff --git a/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py b/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py
index 75b4392..54acb55 100644
--- a/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py
+++ b/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py
@@ -92,11 +92,30 @@ class TestGraphSONWriter(object):
         assert x == output
 
     def test_homogeneous_list(self):
-        x = ["serialize this!", "serialize that!", "stop telling me what to serialize"]
+        x = ["serialize this!", "serialize that!", "serialize that!","stop telling me what to serialize"]
         output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
         assert x == output
 
     def test_heterogeneous_list(self):
-        x = ["serialize this!", 0, "serialize that!", 1, "stop telling me what to serialize", 2]
+        x = ["serialize this!", 0, "serialize that!", "serialize that!", 1, "stop telling me what to serialize", 2]
+        output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
+        assert x == output
+
+    def test_homogeneous_set(self):
+        x = {"serialize this!", "serialize that!", "stop telling me what to serialize"}
+        output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
+        assert x == output
+
+    def test_heterogeneous_set(self):
+        x = {"serialize this!", 0, "serialize that!", 1, "stop telling me what to serialize", 2}
+        output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
+        assert x == output
+
+    def test_dict(self):
+        x = {"yo": "what?",
+             "go": "no!",
+             "number": 123,
+             321: "crazy with the number for a key",
+             987: ["go", "deep", {"here": "!"}]}
         output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
         assert x == output