changeset 84:36f4318442f2

clean up Literal monkeypatch that makes formatting better
author drewp@bigasterisk.com
date Mon, 04 Apr 2022 22:00:45 -0700
parents bbf4595b34ae
children 7d894e977615
files pyproject.toml rdfdb/rdflibpatch_literal.py rdfdb/rdflibpatch_literal_test.py
diffstat 3 files changed, 46 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/pyproject.toml	Mon Apr 04 21:25:53 2022 -0700
+++ b/pyproject.toml	Mon Apr 04 22:00:45 2022 -0700
@@ -33,3 +33,11 @@
 [build-system]
 requires = ["pdm-pep517>=0.12.0"]
 build-backend = "pdm.pep517.api"
+
+[tool.pytest.ini_options]
+filterwarnings = [
+    # The below warning is a consequence of how pytest doctest detects mocks and how DefinedNamespace behaves when an undefined attribute is being accessed.
+    'ignore:Code. pytest_mock_example_attribute_that_shouldnt_exist is not defined in namespace .*:UserWarning',
+    # The below warning is a consequence of how pytest detects fixtures and how DefinedNamespace behaves when an undefined attribute is being accessed.
+    'ignore:Code. _pytestfixturefunction is not defined in namespace .*:UserWarning',
+]
\ No newline at end of file
--- a/rdfdb/rdflibpatch_literal.py	Mon Apr 04 21:25:53 2022 -0700
+++ b/rdfdb/rdflibpatch_literal.py	Mon Apr 04 22:00:45 2022 -0700
@@ -1,28 +1,12 @@
-import sys
-
-if sys.path[0] == '/usr/lib/python2.7/dist-packages':
-    # nosetests puts this in
-    sys.path = sys.path[1:]
-
-from re import sub
-
-from rdflib.term import (_PLAIN_LITERAL_TYPES, _XSD_DECIMAL, _XSD_DOUBLE, Literal)
+from rdflib.term import _PLAIN_LITERAL_TYPES, Literal
 
 
 def _literal_n3(self, use_plain=False, qname_callback=None):
     if use_plain and self.datatype in _PLAIN_LITERAL_TYPES:
         try:
             self.toPython()  # check validity
-            # this is a bit of a mess -
-            # in py >=2.6 the string.format function makes this easier
-            # we try to produce "pretty" output
-            if self.datatype == _XSD_DOUBLE:
-                # this is the drewp fix
-                return sub(r"\.?0*e", "e", '%e' % float(self))
-            elif self.datatype == _XSD_DECIMAL:
-                return sub("0*$", "0", '%f' % float(self))
-            else:
-                return '%s' % self
+            # py3 makes %s give much nicer presentations
+            return '%s' % self
         except ValueError:
             pass  # if it's in, we let it out?
 
@@ -50,15 +34,3 @@
 
 def patch():
     Literal._literal_n3 = _literal_n3
-
-
-import unittest
-
-
-class TestDoubleOutput(unittest.TestCase):
-
-    def testNoDanglingPoint(self):
-        vv = Literal("0.88", datatype=_XSD_DOUBLE)
-        out = _literal_n3(vv, use_plain=True)
-        print(out)
-        self.assertTrue(out in ["8.8e-01", "0.88"], out)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rdfdb/rdflibpatch_literal_test.py	Mon Apr 04 22:00:45 2022 -0700
@@ -0,0 +1,35 @@
+import unittest
+
+from rdflib.namespace import XSD
+from rdflib.term import _XSD_DOUBLE, Literal
+
+from rdfdb.rdflibpatch_literal import _literal_n3, patch
+patch()
+
+
+class TestDoubleOutput(unittest.TestCase):
+
+    def testNoDanglingPoint(self):
+        vv = Literal("0.88", datatype=_XSD_DOUBLE)
+        out = _literal_n3(vv, use_plain=True)
+        print(out)
+        self.assertTrue(out in ["8.8e-01", "0.88"], out)
+
+    def testDoctestsFromRdflibTerm(self):
+        # these include the changes my patch brings
+        assert Literal(1)._literal_n3(use_plain=True) == '1'
+        assert Literal(1.0)._literal_n3(use_plain=True) == '1.0'
+        assert Literal(1.0, datatype=XSD.decimal)._literal_n3(use_plain=True) == '1.0'
+        assert Literal(1.0, datatype=XSD.float)._literal_n3(use_plain=True) == '"1.0"^^<http://www.w3.org/2001/XMLSchema#float>'
+        assert Literal("foo", datatype=XSD.string)._literal_n3(use_plain=True) == '"foo"^^<http://www.w3.org/2001/XMLSchema#string>'
+        assert Literal(True)._literal_n3(use_plain=True) == 'true'
+        assert Literal(False)._literal_n3(use_plain=True) == 'false'
+        assert Literal(1.91)._literal_n3(use_plain=True) == '1.91'
+
+        # Only limited precision available for floats:
+        assert Literal(0.123456789)._literal_n3(use_plain=True) == '0.123456789'
+        assert Literal('0.123456789', datatype=XSD.decimal)._literal_n3(use_plain=True) == '0.123456789'
+
+        # Using callback for datatype QNames::
+
+        assert Literal(1)._literal_n3(qname_callback=lambda uri: "xsd:integer") == '"1"^^xsd:integer'