Skip to content

Commit

Permalink
Some cosmetic a readability changes after review plus improved error
Browse files Browse the repository at this point in the history
  • Loading branch information
francescalb committed Jun 10, 2023
1 parent 6fb36bd commit 7974faf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
38 changes: 24 additions & 14 deletions ontopy/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
ReadCatalogError,
_validate_installed_version,
LabelDefinitionError,
AmbiguousLabelError,
EntityClassDefinitionError,
EMMOntoPyException,
)
Expand Down Expand Up @@ -204,12 +205,16 @@ def __dir__(self):
lst = list(self.get_entities(imported=self._dir_imported))
if self._dir_preflabel:
dirset.update(
_.prefLabel.first() for _ in lst if hasattr(_, "prefLabel")
dir.prefLabel.first()
for dir in lst
if hasattr(dir, "prefLabel")
)
if self._dir_label:
dirset.update(_.label.first() for _ in lst if hasattr(_, "label"))
dirset.update(
dir.label.first() for dir in lst if hasattr(dir, "label")
)
if self._dir_name:
dirset.update(_.name for _ in lst if hasattr(_, "name"))
dirset.update(dir.name for dir in lst if hasattr(dir, "name"))
dirset.difference_update({None}) # get rid of possible None
return sorted(dirset)

Expand Down Expand Up @@ -339,18 +344,21 @@ def get_by_label(
prefix = splitlabel[0]

if prefix:
entitylist = self.get_by_label_all(
entityset = self.get_by_label_all(
label,
label_annotations=label_annotations,
prefix=prefix,
)
if len(entitylist) == 1:
return next(iter(entitylist))

if len(entityset) == 1:
return entityset.pop()
if len(entityset) > 1:
raise AmbiguousLabelError(
f"Several entities have the same label {label!r} "
f"with prefix {prefix!r}."
)
raise NoSuchLabelError(
f"Either no label annotations matches for {label!r} "
f"with prefix {prefix!r} or several entities have "
"the same label and prefix."
f"No label annotations matches for {label!r} "
f"with prefix {prefix!r}."
)

# Label is a full IRI
Expand Down Expand Up @@ -418,16 +426,16 @@ def get_by_label_all(self, label, label_annotations=None, prefix=None):

if label_annotations:
annotations = (
_.name if hasattr(_, "storid") else _ for _ in label_annotations
ann.name if hasattr(ann, "storid") else ann
for ann in label_annotations
)
elif self._label_annotations:
annotations = (_.name for _ in self.label_annotations)
annotations = (ann.name for ann in self.label_annotations)

else:
annotations = None
entities = set()
if annotations:
# entity = self.world.search(**{next(annotations): label})
for key in annotations:
entities.update(self.world.search(**{key: label}))

Expand All @@ -445,7 +453,9 @@ def get_by_label_all(self, label, label_annotations=None, prefix=None):

if prefix:
return set(
_ for _ in entities if _.namespace.ontology.prefix == prefix
ent
for ent in entities
if ent.namespace.ontology.prefix == prefix
)
return entities

Expand Down
4 changes: 4 additions & 0 deletions ontopy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class NoSuchLabelError(LookupError, AttributeError, EMMOntoPyException):
"""Error raised when a label cannot be found."""


class AmbiguousLabelError(LookupError, AttributeError, EMMOntoPyException):
"""Error raised when a label is ambiguous."""


class LabelDefinitionError(EMMOntoPyException):
"""Error in label definition."""

Expand Down
1 change: 0 additions & 1 deletion tests/ontopy_tests/test_new_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def test_new_entity(testonto: "Ontology") -> None:

# Test that new entity is found by both version of get_by_label
assert testonto.get_by_label("FantasyClass") == testonto.FantasyClass
print(testonto.get_by_label_all("*"))
assert testonto.get_by_label_all("FantasyClass") == {testonto.FantasyClass}

testonto.sync_attributes()
Expand Down

0 comments on commit 7974faf

Please sign in to comment.