Skip to content

Commit

Permalink
add nxontology metadata to new instances
Browse files Browse the repository at this point in the history
  • Loading branch information
dhimmel committed Mar 31, 2022
1 parent 7f4ba41 commit 4c31d04
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
10 changes: 9 additions & 1 deletion nxontology/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .exceptions import DuplicateError, NodeNotFound
from .node import Node_Info
from .similarity import SimilarityIC
from .utils import Freezable, cache_on_frozen
from .utils import Freezable, cache_on_frozen, get_datetime_now, get_nxontology_version

logger = logging.getLogger(__name__)

Expand All @@ -30,9 +30,17 @@ class NXOntology(Freezable, Generic[Node]):

def __init__(self, graph: nx.DiGraph | None = None):
self.graph = nx.DiGraph(graph)
if graph is None:
# Store the nxontology version that created the graph as metadata,
# in case there are compatability issues in the future.
self._add_nxontology_metadata()
self.check_is_dag()
self._node_info_cache: dict[Node, Node_Info[Node]] = {}

def _add_nxontology_metadata(self) -> None:
self.graph.graph["nxontology_version"] = get_nxontology_version()
self.graph.graph["nxontology_created"] = get_datetime_now()

def check_is_dag(self) -> None:
if nx.is_directed_acyclic_graph(self.graph):
return
Expand Down
16 changes: 16 additions & 0 deletions nxontology/tests/ontology_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pathlib
from datetime import date

import networkx
import pytest
Expand All @@ -7,6 +8,21 @@
from nxontology.ontology import NXOntology


def test_add_nxontology_metadata() -> None:
nxo: NXOntology[str] = NXOntology()
assert "nxontology_version" in nxo.graph.graph
assert nxo.graph.graph["nxontology_created"].startswith(str(date.today().year))


def test_add_nxontology_no_metadata() -> None:
graph = networkx.DiGraph()
nxo: NXOntology[str] = NXOntology(graph)
# When providing an existing graph, we do not update metadata,
# although this is not a firm decision that this is the right behavior.
assert "nxontology_created" not in nxo.graph.graph
assert "nxontology_version" not in nxo.graph.graph


def test_n_nodes(metal_nxo: NXOntology[str]) -> None:
assert metal_nxo.n_nodes == 8

Expand Down
14 changes: 14 additions & 0 deletions nxontology/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import abc
import functools
import importlib.metadata
from datetime import datetime, timezone
from typing import Callable, TypeVar


Expand Down Expand Up @@ -44,3 +46,15 @@ def wrapped(self: T_Freezable) -> T:
# But mypy looses track of the return type.
# https://github.com/python/mypy/issues/8083
return wrapped


def get_nxontology_version() -> str | None:
# https://github.com/pypa/setuptools_scm/#retrieving-package-version-at-runtime
try:
return importlib.metadata.version("nxontology")
except importlib.metadata.PackageNotFoundError:
return None


def get_datetime_now() -> str:
return datetime.utcnow().replace(microsecond=0, tzinfo=timezone.utc).isoformat()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ target-version = ['py37', 'py38', 'py39', 'py310']

# https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml-file
[tool.mypy]
python_version = "3.7"
python_version = "3.8"

[[tool.mypy.overrides]]
module = [
Expand Down
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ zip_safe = False
include_package_data = True
python_requires = >=3.7
install_requires =
networkx>=2
pronto>=v2.4.0
fsspec[http]
importlib_metadata; python_version < '3.8'
networkx>=2
pronto>=2.4.0

[options.extras_require]
dev =
Expand Down

0 comments on commit 4c31d04

Please sign in to comment.