From a6c7c24eaeb3b4c1a6a6d0a7620187a8aa9b9cb4 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sat, 25 May 2024 12:06:11 +0200 Subject: [PATCH 1/3] Allow string literal to compare equal to strings. This is both a more intuitive behaviour and allow the mappings module to behave as expected when using string literals as dict keys. --- tests/test_literals.py | 7 ++++--- tripper/literal.py | 22 +++++++++++++++++----- tripper/mappings/mappings.py | 6 +----- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/tests/test_literals.py b/tests/test_literals.py index a3f7c8c9..6e6065cf 100644 --- a/tests/test_literals.py +++ b/tests/test_literals.py @@ -31,7 +31,7 @@ def test_string() -> None: assert literal.to_python() == "Hello world!" assert literal.value == "Hello world!" assert literal.n3() == f'"Hello world!"^^<{XSD.string}>' - assert literal != "Hello world!" + assert literal == "Hello world!" def test_string_lang() -> None: @@ -45,6 +45,7 @@ def test_string_lang() -> None: assert literal.datatype is None assert literal.value == "Hello world!" assert literal.n3() == '"Hello world!"@en' + assert literal == "Hello world!" def test_cannot_combine_datatype_and_lang() -> None: @@ -238,8 +239,8 @@ def test_equality() -> None: assert Literal("text", lang="en") != Literal("text", lang="dk") assert Literal("text") == "text" assert Literal("text") != "text2" - assert Literal("text", datatype=XSD.string) != "text" - assert Literal("text", datatype=RDF.HTML) != "text" + assert Literal("text", datatype=XSD.string) == "text" + assert Literal("text", datatype=RDF.HTML) == "text" assert Literal(1) == 1 assert Literal(1) != 1.0 assert Literal(1) != "1" diff --git a/tripper/literal.py b/tripper/literal.py index 5eec455d..a02ddd92 100644 --- a/tripper/literal.py +++ b/tripper/literal.py @@ -58,16 +58,26 @@ class Literal(str): str: ( XSD.string, RDF.HTML, - RDF.PlainLiteral, + RDF.JSON, RDF.XMLLiteral, + RDF.PlainLiteral, + RDF.langString, RDFS.Literal, + XSD.ENTITY, + XSD.ID, + XSD.IDREF, + XSD.NCName, + XSD.NMTOKEN, + XSD.Name, + XSD.QName, XSD.anyURI, + XSD.base64Binary, + XSD.hexBinary, XSD.language, - XSD.Name, - XSD.NMName, XSD.normalizedString, XSD.token, - XSD.NMTOKEN, + # XSD.pattern, + # XSD.whiteSpace, ), } @@ -177,7 +187,9 @@ def __hash__(self): def __eq__(self, other): if not isinstance(other, Literal): - if isinstance(other, str) and self.lang: + if isinstance(other, str) and ( + self.lang or self.datatype in self.datatypes[str] + ): return str(self) == other other = Literal(other) return ( diff --git a/tripper/mappings/mappings.py b/tripper/mappings/mappings.py index 16ceff54..b319e7f5 100644 --- a/tripper/mappings/mappings.py +++ b/tripper/mappings/mappings.py @@ -290,11 +290,6 @@ def eval( inputs, idx = self.get_inputs(routeno) values = get_values(inputs, idx, quantity=quantity) - # if len(inputs) == 1 and all( - # isinstance(v, Value) for v in inputs.values() - # ): - # (value,) = tuple(inputs.values()) - # elif self.function: if self.function: value = self.function(**values) elif len(values) == 1: @@ -658,6 +653,7 @@ def get_values( A mapping between input names and values of expected input unit. """ values = {} + for k, v in inputs.items(): if isinstance(v, MappingStep): value = v.eval(routeno=routeno, quantity=quantity) From c283356fecdb8a3fb282c38a1dfb74673e6effec Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sat, 25 May 2024 14:54:44 +0200 Subject: [PATCH 2/3] Cleaned up list of string literals --- tripper/literal.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tripper/literal.py b/tripper/literal.py index a02ddd92..c0bc7c8a 100644 --- a/tripper/literal.py +++ b/tripper/literal.py @@ -31,8 +31,8 @@ class Literal(str): # utils.parse_literal() when inferring the datatype of a literal. datatypes = { datetime: (XSD.dateTime,), - bytes: (XSD.hexBinary,), - bytearray: (XSD.hexBinary,), + bytes: (XSD.hexBinary, XSD.base64Binary), + bytearray: (XSD.hexBinary, XSD.base64Binary), bool: (XSD.boolean,), int: ( XSD.integer, @@ -57,12 +57,12 @@ class Literal(str): ), str: ( XSD.string, + RDFS.Literal, + RDF.PlainLiteral, RDF.HTML, RDF.JSON, RDF.XMLLiteral, - RDF.PlainLiteral, RDF.langString, - RDFS.Literal, XSD.ENTITY, XSD.ID, XSD.IDREF, @@ -71,8 +71,6 @@ class Literal(str): XSD.Name, XSD.QName, XSD.anyURI, - XSD.base64Binary, - XSD.hexBinary, XSD.language, XSD.normalizedString, XSD.token, @@ -145,9 +143,6 @@ def __new__( string.datatype = XSD.hexBinary elif isinstance(value, datetime): string.datatype = XSD.dateTime - # TODO: - # - XSD.base64Binary - # - XSD.byte, XSD.unsignedByte # Some consistency checking if ( From dd3f3fdc9aa3f95b560bd2e6ac7c67e64968ff43 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sun, 26 May 2024 23:52:17 +0200 Subject: [PATCH 3/3] Removed types that are not recommended to be used. --- tripper/literal.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tripper/literal.py b/tripper/literal.py index c0bc7c8a..4c23bb85 100644 --- a/tripper/literal.py +++ b/tripper/literal.py @@ -63,19 +63,13 @@ class Literal(str): RDF.JSON, RDF.XMLLiteral, RDF.langString, - XSD.ENTITY, - XSD.ID, - XSD.IDREF, XSD.NCName, XSD.NMTOKEN, XSD.Name, - XSD.QName, XSD.anyURI, XSD.language, XSD.normalizedString, XSD.token, - # XSD.pattern, - # XSD.whiteSpace, ), }