Skip to content

Commit 4389336

Browse files
committed
Simplify more code
1 parent fbb8351 commit 4389336

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

kgx/config.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import importlib
2+
from functools import lru_cache
23
from typing import Dict, Any, Optional
34
import sys
45
from os import path
@@ -10,12 +11,13 @@
1011
import json
1112
import logging
1213

14+
import curies
1315
from kgx.graph.base_graph import BaseGraph
1416

1517
config: Optional[Dict[str, Any]] = None
1618
logger: Optional[logging.Logger] = None
1719
graph_store_class: Optional[BaseGraph] = None
18-
jsonld_context_map: Dict = {}
20+
jsonld_context_map: Dict[str, Dict[str, str]] = {}
1921

2022
CONFIG_FILENAME = path.join(path.dirname(path.abspath(__file__)), "config.yml")
2123

@@ -41,6 +43,13 @@ def get_config(filename: str = CONFIG_FILENAME) -> Dict:
4143
return config
4244

4345

46+
@lru_cache
47+
def get_converter(name: str = "biolink") -> curies.Converter:
48+
"""Get contents of a JSON-LD context."""
49+
filepath = config["jsonld-context"][name] # type: ignore
50+
return curies.load_jsonld_context(filepath)
51+
52+
4453
def get_jsonld_context(name: str = "biolink"):
4554
"""
4655
Get contents of a JSON-LD context.
@@ -55,19 +64,8 @@ def get_jsonld_context(name: str = "biolink"):
5564
if name in jsonld_context_map:
5665
content = jsonld_context_map[name]
5766
else:
58-
filepath = config["jsonld-context"][name] # type: ignore
59-
if filepath.startswith("http"):
60-
try:
61-
content = requests.get(filepath).json()
62-
except ConnectionError:
63-
raise Exception(f"Unable to download JSON-LD context from {filepath}")
64-
else:
65-
if path.exists(filepath):
66-
content = json.load(open(filepath))
67-
68-
if "@context" in content:
69-
content = content["@context"]
70-
jsonld_context_map[name] = content
67+
converter = get_converter(name)
68+
jsonld_context_map[name] = converter.bimap
7169
return content
7270

7371

kgx/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ logging:
1818
format: '[%(name)s][%(filename)s][%(funcName)20s] %(levelname)s: %(message)s'
1919

2020
jsonld-context:
21-
biolink: https://raw.githubusercontent.com/biolink/biolink-model/2.2.5/context.jsonld
21+
biolink: https://raw.githubusercontent.com/biolink/biolink-model/v4.0.0/project/jsonld/biolink_model.context.jsonld
2222
monarch_context: https://raw.githubusercontent.com/prefixcommons/biocontext/master/registry/monarch_context.jsonld
23-
obo_context: https://raw.githubusercontent.com/prefixcommons/biocontext/master/registry/obo_context.jsonld
23+
obo_context: https://raw.githubusercontent.com/prefixcommons/biocontext/master/registry/obo_context.jsonld

kgx/utils/kgx_utils.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
import numpy as np
2424
import curies
2525

26-
from kgx.config import get_logger, get_jsonld_context, get_biolink_model_schema
26+
from kgx.config import (
27+
get_logger,
28+
get_jsonld_context,
29+
get_biolink_model_schema,
30+
get_converter,
31+
)
2732
from kgx.graph.base_graph import BaseGraph
2833

2934
curie_lookup_service = None
@@ -220,12 +225,9 @@ def format_biolink_slots(s: str) -> str:
220225
return f"biolink:{formatted}"
221226

222227

223-
MONARCH_CONVERTER = curies.load_prefix_map(get_jsonld_context("monarch_context"))
224-
OBO_CONVERTER = curies.load_prefix_map(get_jsonld_context("obo_context"))
225-
DEFAULT_CONVERTER = curies.chain([
226-
MONARCH_CONVERTER,
227-
OBO_CONVERTER
228-
])
228+
MONARCH_CONVERTER = get_converter("monarch_context")
229+
OBO_CONVERTER = get_converter("obo_context")
230+
DEFAULT_CONVERTER = curies.chain([MONARCH_CONVERTER, OBO_CONVERTER])
229231

230232

231233
def _resolve_converter(prefix_maps: Optional[List[Dict[str, str]]] = None) -> curies.Converter:

tests/unit/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import docker
21
import pytest
32
from neo4j import GraphDatabase
43

@@ -17,6 +16,8 @@ def check_container():
1716
is currently running.
1817
"""
1918
try:
19+
import docker
20+
2021
client = docker.from_env()
2122
status = False
2223
try:

0 commit comments

Comments
 (0)