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/08/09 19:13:04 UTC

[tinkerpop] 06/23: Added timestamp 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 60204935aac0dbacb7ba0952586adc01ea6964e9
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Mon Jul 22 18:45:58 2019 -0400

    Added timestamp for graphbinary in python
---
 .../gremlin_python/structure/io/graphbinaryV1.py      | 19 +++++++++++++++++++
 .../jython/tests/structure/io/test_graphbinaryV1.py   |  7 ++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

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 a3c7bc9..33342de 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
@@ -52,6 +52,7 @@ class DataType(Enum):
     long = 0x02
     string = 0x03
     date = 0x04
+    timestamp = 0x05
     list = 0x09
 
 
@@ -200,6 +201,24 @@ class DateIO(_GraphBinaryTypeIO):
         return datetime.datetime.utcfromtimestamp(struct.unpack(">q", buff.read(8))[0] / 1000.0)
 
 
+# Based on current implementation, this class must always be declared before FloatIO.
+# Seems pretty fragile for future maintainers. Maybe look into this.
+class TimestampIO(_GraphBinaryTypeIO):
+    python_type = statics.timestamp
+    graphbinary_type = DataType.timestamp
+
+    @classmethod
+    def dictify(cls, obj, writer):
+        # Java timestamp expects milliseconds integer - Have to use int because of legacy Python
+        ts = int(round(obj * 1000))
+        return cls.as_bytes(cls.graphbinary_type, None, struct.pack(">q", ts))
+
+    @classmethod
+    def objectify(cls, buff, reader):
+        # Python timestamp expects seconds
+        return statics.timestamp(struct.unpack(">q", buff.read(8))[0] / 1000.0)
+
+
 class StringIO(_GraphBinaryTypeIO):
 
     python_type = str
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 a0a5be1..5775f3a 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
@@ -27,7 +27,7 @@ from mock import Mock
 
 import six
 
-from gremlin_python.statics import *
+from gremlin_python.statics import timestamp, long
 from gremlin_python.structure.graph import Vertex, Edge, Property, VertexProperty, Graph, Path
 from gremlin_python.structure.io.graphbinaryV1 import GraphBinaryWriter, GraphBinaryReader, DataType
 from gremlin_python.process.traversal import P
@@ -58,6 +58,11 @@ class TestGraphSONWriter(object):
         output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
         assert x == output
 
+    def test_timestamp(self):
+        x = timestamp(1481750076295 / 1000)
+        output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
+        assert x == output
+
     def test_string(self):
         x = "serialize this!"
         output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))