tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From spmalle...@apache.org
Subject tinkerpop git commit: TINKERPOP-2055 Support for special numbers in python
Date Tue, 02 Oct 2018 20:56:28 GMT
Repository: tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-2055 afc12bd27 -> 854914e6e


TINKERPOP-2055 Support for special numbers in python


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/854914e6
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/854914e6
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/854914e6

Branch: refs/heads/TINKERPOP-2055
Commit: 854914e6e3adbf7f0854eb0fec0c3a38b61d4644
Parents: afc12bd
Author: Stephen Mallette <spmva@genoprime.com>
Authored: Tue Oct 2 16:55:50 2018 -0400
Committer: Stephen Mallette <spmva@genoprime.com>
Committed: Tue Oct 2 16:55:50 2018 -0400

----------------------------------------------------------------------
 .../gremlin_python/structure/io/graphson.py     | 26 ++++++++++++++++++++
 .../jython/tests/structure/io/test_graphson.py  | 25 +++++++++++++++++++
 2 files changed, 51 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/854914e6/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 24e86bb..f3411ba 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
@@ -20,6 +20,7 @@ import datetime
 import json
 import time
 import uuid
+import math
 from collections import OrderedDict
 
 import six
@@ -381,6 +382,31 @@ class FloatIO(_NumberIO):
     graphson_type = "g:Float"
     graphson_base_type = "Float"
 
+    @classmethod
+    def dictify(cls, n, writer):
+        if isinstance(n, bool):  # because isinstance(False, int) and isinstance(True, int)
+            return n
+        elif math.isnan(n):
+            return GraphSONUtil.typedValue(cls.graphson_base_type, "NaN")
+        elif math.isinf(n) and n > 0:
+            return GraphSONUtil.typedValue(cls.graphson_base_type, "Infinity")
+        elif math.isinf(n) and n < 0:
+            return GraphSONUtil.typedValue(cls.graphson_base_type, "-Infinity")
+        else:
+            return GraphSONUtil.typedValue(cls.graphson_base_type, n)
+
+    @classmethod
+    def objectify(cls, v, _):
+        if isinstance(v, str):
+            if v == 'NaN':
+                return float('nan')
+            elif v == "Infinity":
+                return float('inf')
+            elif v == "-Infinity":
+                return float('-inf')
+
+        return cls.python_type(v)
+
 
 class DoubleIO(FloatIO):
     graphson_type = "g:Double"

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/854914e6/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 928b33f..37e99c6 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
@@ -22,6 +22,7 @@ import datetime
 import time
 import json
 import uuid
+import math
 
 from mock import Mock
 
@@ -70,6 +71,27 @@ class TestGraphSONReader(object):
         }))
         assert isinstance(x, float)
         assert 31.2 == x
+        ##
+        x = self.graphson_reader.readObject(json.dumps({
+            "@type": "g:Double",
+            "@value": "NaN"
+        }))
+        assert isinstance(x, float)
+        assert math.isnan(x)
+        ##
+        x = self.graphson_reader.readObject(json.dumps({
+            "@type": "g:Double",
+            "@value": "Infinity"
+        }))
+        assert isinstance(x, float)
+        assert math.isinf(x) and x > 0
+        ##
+        x = self.graphson_reader.readObject(json.dumps({
+            "@type": "g:Double",
+            "@value": "-Infinity"
+        }))
+        assert isinstance(x, float)
+        assert math.isinf(x) and x < 0
 
     def test_graph(self):
         vertex = self.graphson_reader.readObject(
@@ -163,6 +185,9 @@ class TestGraphSONWriter(object):
         assert {"@type": "g:Int64", "@value": 2} == json.loads(self.graphson_writer.writeObject(long(2)))
         assert {"@type": "g:Int32", "@value": 1} == json.loads(self.graphson_writer.writeObject(1))
         assert {"@type": "g:Double", "@value": 3.2} == json.loads(self.graphson_writer.writeObject(3.2))
+        assert {"@type": "g:Double", "@value": "NaN"} == json.loads(self.graphson_writer.writeObject(float('nan')))
+        assert {"@type": "g:Double", "@value": "Infinity"} == json.loads(self.graphson_writer.writeObject(float('inf')))
+        assert {"@type": "g:Double", "@value": "-Infinity"} == json.loads(self.graphson_writer.writeObject(float('-inf')))
         assert """true""" == self.graphson_writer.writeObject(True)
 
     def test_P(self):


Mime
View raw message