/
p17_1_operand_data_types.py
79 lines (60 loc) · 2.25 KB
/
p17_1_operand_data_types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from typing import cast, Union
from rdflib import Literal, XSD, URIRef, BNode
from rdflib.term import Node
def is_typed_literal(n: Node) -> bool:
return isinstance(n, Literal) and n.datatype is not None
def is_plain_literal(n: Node) -> bool:
return isinstance(n, Literal) and n.datatype is None
def is_strict_numeric(n: Node) -> bool:
""" numeric denotes typed literals with datatypes xsd:integer, xsd:decimal, xsd:float, and xsd:double. """
return is_typed_literal(n) and cast(Literal, n).datatype in [XSD.integer, XSD.decimal, XSD.float, XSD.double]
def is_simple_literal(n: Node) -> bool:
""" simple literal denotes a plain literal with no language tag. """
return is_typed_literal(n) and cast(Literal, n).datatype is None and cast(Literal, n).language is None
def is_rdf_term(n: Node) -> bool:
return isinstance(n, (URIRef, Literal, BNode))
def is_integer(n: Node) -> bool:
return is_typed_literal(n) and cast(Literal, n).datatype in [
XSD.integer,
XSD.nonPositiveInteger,
XSD.negativeInteger,
XSD.long,
XSD.int,
XSD.short,
XSD.byte,
XSD.nonNegativeInteger,
XSD.unsignedLong,
XSD.unsignedInt,
XSD.unsignedShort,
XSD.unsignedByte,
XSD.positiveInteger
]
def is_decimal(n: Node) -> bool:
return is_integer(n) or (is_typed_literal(n) and cast(Literal, n).datatype in [XSD.decimal])
def is_numeric(n: Node) -> bool:
return is_decimal(n) or (is_typed_literal(n) and cast(Literal, n).datatype in [XSD.float, XSD.double])
def is_sparql_operand_datatype(n: Union[Node, str]) -> bool:
# From: https://www.w3.org/TR/sparql11-query/#operandDataTypes
if isinstance(n, str):
n = URIRef(n)
return is_plain_literal(n) or (is_typed_literal(n) and cast(Literal, n).datatype in [
XSD.integer,
XSD.decimal,
XSD.float,
XSD.double,
XSD.string,
XSD.boolean,
XSD.dateTime,
XSD.nonPositiveInteger,
XSD.negativeInteger,
XSD.long,
XSD.int,
XSD.short,
XSD.byte,
XSD.nonNegativeInteger,
XSD.unsignedLong,
XSD.unsignedInt,
XSD.unsignedShort,
XSD.unsignedByte,
XSD.positiveInteger
])