Skip to content

Commit

Permalink
Fixes #1043.
Browse files Browse the repository at this point in the history
* Added test case for #1043
* Don't make XSD.Decimal pretty during serialization
  • Loading branch information
achaudhary997 committed May 19, 2020
1 parent 231bc6d commit c7c4e15
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,8 +1242,8 @@ def _literal_n3(self, use_plain=False, qname_callback=None):
return sub("\\.?0*e", "e", u'%e' % float(self))
elif self.datatype == _XSD_DECIMAL:
s = '%s' % self
if '.' not in s:
s += '.0'
# if '.' not in s:
# s += '.0'
return s

elif self.datatype == _XSD_BOOLEAN:
Expand Down
33 changes: 33 additions & 0 deletions test/test_issue1043.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import decimal
import unittest
import io
import sys

from rdflib import Graph, Namespace, XSD, RDFS, Literal


class TestIssue1043(unittest.TestCase):

def test_issue_1043(self):
expected = """@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<http://example.org/number> rdfs:label 4e-08 .
"""
capturedOutput = io.StringIO()
sys.stdout = capturedOutput
g = Graph()
g.bind('xsd', XSD)
g.bind('rdfs', RDFS)
n = Namespace("http://example.org/")
g.add((n.number, RDFS.label, Literal(0.00000004, datatype=XSD.decimal)))
print(g.serialize(format="turtle").decode("utf-8"))
sys.stdout = sys.__stdout__
self.assertEqual(capturedOutput.getvalue(), expected)



if __name__ == "__main__":
unittest.main()

0 comments on commit c7c4e15

Please sign in to comment.