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

Allow string literal to compare equal to strings. #215

Merged
merged 3 commits into from
May 27, 2024
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
7 changes: 4 additions & 3 deletions tests/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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"
Expand Down
23 changes: 12 additions & 11 deletions tripper/literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -57,17 +57,19 @@ class Literal(str):
),
str: (
XSD.string,
RDF.HTML,
RDFS.Literal,
RDF.PlainLiteral,
RDF.HTML,
RDF.JSON,
RDF.XMLLiteral,
RDFS.Literal,
RDF.langString,
XSD.NCName,
XSD.NMTOKEN,
XSD.Name,
XSD.anyURI,
XSD.language,
XSD.Name,
XSD.NMName,
XSD.normalizedString,
XSD.token,
XSD.NMTOKEN,
),
}

Expand Down Expand Up @@ -135,9 +137,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 (
Expand Down Expand Up @@ -177,7 +176,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 (
Expand Down
6 changes: 1 addition & 5 deletions tripper/mappings/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down