Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Literal total ordering #793

Merged
merged 5 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,9 @@ def __gt__(self, other):
return self.language > other.language

if self.value != None and other.value != None:
if type(self.value) in _NO_TOTAL_ORDER_TYPES:
comparator = _NO_TOTAL_ORDER_TYPES[type(self.value)]
return comparator(self.value) > comparator(other.value)
return self.value > other.value

if text_type(self) != text_type(other):
Expand Down Expand Up @@ -1397,6 +1400,14 @@ def _writeXML(xmlnode):
_XSD_DECIMAL,
)

# these are not guranteed to sort because it is not possible
# to calculate a total order over all valid members of the type
# the function must partition the type into subtypes that do have total orders
_NO_TOTAL_ORDER_TYPES = {
datetime:lambda value:bool(value.tzinfo),
time:lambda value:bool(value.tzinfo),
xml.dom.minidom.Document:lambda value:value.toxml(),
}

def _castPythonToLiteral(obj):
"""
Expand Down
22 changes: 22 additions & 0 deletions test/test_term.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ def test_base64_values(self):
self.assertEqual(lit.value, decoded_b64msg)
self.assertEqual(str(lit), b64msg)

def test_total_order(self):
types = {
XSD.dateTime:('0001-01-01T00:00:00', '0001-01-01T00:00:00Z',
'0001-01-01T00:00:00-00:00'),
XSD.date:('0001-01-01', '0001-01-01Z', '0001-01-01-00:00'),
XSD.time:('00:00:00', '00:00:00Z', '00:00:00-00:00'),
XSD.gYear:('0001', '0001Z', '0001-00:00'), # interval
XSD.gYearMonth:('0001-01', '0001-01Z', '0001-01-00:00'),
}
literals = [Literal(literal, datatype=type)
for type, literals in types.items()
for literal in literals]
try:
sorted(literals)
orderable = True
except TypeError as e:
for l in literals:
print(repr(l), repr(l.value))
print(e)
orderable = False
self.assertTrue(orderable)


class TestValidityFunctions(unittest.TestCase):

Expand Down