Skip to content

Commit

Permalink
Reduce more code
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt committed Nov 23, 2024
1 parent fc55808 commit 134b6b0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/pyobo/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ def iterate_graph_synonym_typedefs(
for s in graph.graph.get("synonymtypedef", []):
sid, name = s.split(" ", 1)
name = name.strip().strip('"')
if ":" not in sid: # assume it's ad-hoc
reference = Reference(prefix=ontology_prefix, identifier=sid, name=name)
else: # assume it's a curie
if ":" in sid: # assume it's a curie
reference = Reference.from_curie(
sid, name=name, strict=strict, ontology_prefix=ontology_prefix
)
else:
reference = default_reference(ontology_prefix, sid, name=name)

if reference is None:
if strict:
Expand Down
32 changes: 3 additions & 29 deletions src/pyobo/struct/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,38 +93,12 @@ def from_curie( # type:ignore[override]
prefix, identifier = normalize_curie(
curie, strict=strict, ontology_prefix=ontology_prefix, node=node
)
return cls._materialize(prefix=prefix, identifier=identifier, name=name, auto=auto)

@classmethod
def from_iri(
cls,
iri: str,
name: str | None = None,
*,
auto: bool = False,
) -> Reference | None:
"""Get a reference from an IRI using the Bioregistry.
:param iri: The IRI to parse
:param name: The name associated with the CURIE
:param auto: Automatically look up name
"""
prefix, identifier = bioregistry.parse_iri(iri)
return cls._materialize(prefix=prefix, identifier=identifier, name=name, auto=auto)

@classmethod
def _materialize(
cls,
prefix: str | None,
identifier: str | None,
name: str | None = None,
*,
auto: bool = False,
) -> Reference | None:
if prefix is None or identifier is None:
return None
if name is None and auto:
return cls.auto(prefix=prefix, identifier=identifier)
from ..api import get_name

name = get_name(prefix, identifier)
return cls.model_validate({"prefix": prefix, "identifier": identifier, "name": name})

@property
Expand Down

0 comments on commit 134b6b0

Please sign in to comment.