@@ -760,31 +760,31 @@ def __gt__(self, other):
760760 This tries to implement this:
761761 http://www.w3.org/TR/sparql11-query/#modOrderBy
762762
763- In short, Literals with compatible data-types are orderd in value space,
764- i.e.
763+ In short, Literals with compatible data-types are ordered in value
764+ space, i.e.
765765 >>> from rdflib import XSD
766766
767- >>> Literal(1)> Literal(2) # int/int
767+ >>> Literal(1) > Literal(2) # int/int
768768 False
769- >>> Literal(2.0)> Literal(1) # double/int
769+ >>> Literal(2.0) > Literal(1) # double/int
770770 True
771771 >>> from decimal import Decimal
772772 >>> Literal(Decimal("3.3")) > Literal(2.0) # decimal/double
773773 True
774774 >>> Literal(Decimal("3.3")) < Literal(4.0) # decimal/double
775775 True
776- >>> Literal('b')> Literal('a') # plain lit/plain lit
776+ >>> Literal('b') > Literal('a') # plain lit/plain lit
777777 True
778- >>> Literal('b')> Literal('a', datatype=XSD.string) # plain lit/xsd:string
778+ >>> Literal('b') > Literal('a', datatype=XSD.string) # plain lit/xsd:str
779779 True
780780
781781 Incompatible datatype mismatches ordered by DT
782782
783- >>> Literal(1)> Literal("2") # int>string
783+ >>> Literal(1) > Literal("2") # int>string
784784 False
785785
786786 Langtagged literals by lang tag
787- >>> Literal("a", lang="en")> Literal("a", lang="fr")
787+ >>> Literal("a", lang="en") > Literal("a", lang="fr")
788788 False
789789 """
790790 if other is None :
@@ -814,10 +814,10 @@ def __gt__(self, other):
814814 else :
815815 return self .language > other .language
816816
817- if self .value != None and other .value != None :
818- if type (self .value ) in _NO_TOTAL_ORDER_TYPES :
819- comparator = _NO_TOTAL_ORDER_TYPES [type (self .value )]
820- return comparator (self .value ) > comparator (other .value )
817+ if self .value is not None and other .value is not None :
818+ if type (self .value ) in _TOTAL_ORDER_CASTERS :
819+ caster = _TOTAL_ORDER_CASTERS [type (self .value )]
820+ return caster (self .value ) > caster (other .value )
821821 return self .value > other .value
822822
823823 if text_type (self ) != text_type (other ):
@@ -1409,13 +1409,21 @@ def _writeXML(xmlnode):
14091409 _XSD_DECIMAL ,
14101410)
14111411
1412- # these are not guranteed to sort because it is not possible
1413- # to calculate a total order over all valid members of the type
1414- # the function must partition the type into subtypes that do have total orders
1415- _NO_TOTAL_ORDER_TYPES = {
1416- datetime :lambda value :bool (value .tzinfo ),
1417- time :lambda value :bool (value .tzinfo ),
1418- xml .dom .minidom .Document :lambda value :value .toxml (),
1412+ # the following types need special treatment for reasonable sorting because
1413+ # certain instances can't be compared to each other. We treat this by
1414+ # partitioning and then sorting within those partitions.
1415+ _TOTAL_ORDER_CASTERS = {
1416+ datetime : lambda value : (
1417+ # naive vs. aware
1418+ value .tzinfo is not None and value .tzinfo .utcoffset (value ) is not None ,
1419+ value
1420+ ),
1421+ time : lambda value : (
1422+ # naive vs. aware
1423+ value .tzinfo is not None and value .tzinfo .utcoffset (None ) is not None ,
1424+ value
1425+ ),
1426+ xml .dom .minidom .Document : lambda value : value .toxml (),
14191427}
14201428
14211429def _castPythonToLiteral (obj ):
0 commit comments