Skip to content

Commit 81ada1c

Browse files
committed
Refactor complex function
1 parent c990c73 commit 81ada1c

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/pyobo/struct/struct.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -709,23 +709,32 @@ def from_obo_path(
709709
obo = Obo.from_obonet(graph, strict=strict)
710710
return obo
711711

712-
@classmethod
713-
def from_obonet(cls, graph: nx.MultiDiGraph, *, strict: bool = True) -> "Obo": # noqa:C901
714-
"""Get all of the terms from a OBO graph."""
715-
ontology = normalize_prefix(graph.graph["ontology"]) # probably always okay
716-
logger.info("[%s] extracting OBO using obonet", ontology)
717-
712+
@staticmethod
713+
def _get_name(graph, ontology: str) -> str:
718714
try:
719-
date = datetime.strptime(graph.graph["date"], DATE_FORMAT)
715+
rv = graph.graph["name"]
720716
except KeyError:
721-
logger.info("[%s] does not report a date", ontology)
722-
date = None
717+
logger.info("[%s] does not report a name", ontology)
718+
rv = ontology
719+
return rv
723720

721+
@staticmethod
722+
def _get_date(graph, ontology: str) -> Optional[datetime]:
724723
try:
725-
name = graph.graph["name"]
724+
rv = datetime.strptime(graph.graph["date"], DATE_FORMAT)
726725
except KeyError:
727-
logger.info("[%s] does not report a name", ontology)
728-
name = ontology
726+
logger.info("[%s] does not report a date", ontology)
727+
rv = None
728+
return rv
729+
730+
@classmethod
731+
def from_obonet(cls, graph: nx.MultiDiGraph, *, strict: bool = True) -> "Obo":
732+
"""Get all of the terms from a OBO graph."""
733+
ontology = normalize_prefix(graph.graph["ontology"]) # probably always okay
734+
logger.info("[%s] extracting OBO using obonet", ontology)
735+
736+
date = cls._get_date(graph=graph, ontology=ontology)
737+
name = cls._get_name(graph=graph, ontology=ontology)
729738

730739
data_version = graph.graph.get("data-version")
731740
if not data_version:
@@ -759,9 +768,8 @@ def from_obonet(cls, graph: nx.MultiDiGraph, *, strict: bool = True) -> "Obo":
759768
(prefix, identifier): Reference(
760769
prefix=prefix,
761770
identifier=identifier,
762-
name=data.get(
763-
"name"
764-
), # if name isn't available, it means its external to this ontology
771+
# if name isn't available, it means its external to this ontology
772+
name=data.get("name"),
765773
)
766774
for prefix, identifier, data in _iter_obo_graph(graph=graph)
767775
}

0 commit comments

Comments
 (0)