Skip to content

Commit ae16fcc

Browse files
author
Martin Ruefenacht
committed
Linked constants with kind
1 parent d832521 commit ae16fcc

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/pympistandard/__init__.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def _resolve_path(
143143
"""Find correct path to load apis.json from."""
144144

145145
if force_bundled:
146-
with importlib.resources.path("pympistandard.data", "apis.json") as datapath:
146+
with importlib.resources.path("pympistandard.data", "mpidb.json") as datapath:
147147
path = datapath
148148

149149
# convert str path to Path
@@ -152,11 +152,11 @@ def _resolve_path(
152152

153153
# use given path
154154
elif isinstance(given_path, Path):
155-
path = given_path / "apis.json"
155+
path = given_path / "mpidb.json"
156156

157157
# use environment variable paths
158158
elif "MPISTANDARD" in os.environ:
159-
path = Path(os.environ["MPISTANDARD"] + "/apis.json")
159+
path = Path(os.environ["MPISTANDARD"] + "/mpidb.json")
160160

161161
# else:
162162
# raise RuntimeError(
@@ -167,7 +167,7 @@ def _resolve_path(
167167
else:
168168
# fallback to packaged data
169169
# AFTER 3.9 path = importlib.resources.files("pympistandard.data").joinpath("apis.json")
170-
with importlib.resources.path("pympistandard.data", "apis.json") as datapath:
170+
with importlib.resources.path("pympistandard.data", "mpidb.json") as datapath:
171171
path = datapath
172172

173173
# require resolved path to exist
@@ -183,11 +183,6 @@ def _load_database_v1(path: Path) -> None:
183183

184184
# TODO discover which files of our database are in path, apis.json, constants.json
185185

186-
with path.joinpath("constants.json").open() as constants_file:
187-
for name, desc in dataset.items():
188-
const = Constant(name, desc)
189-
CONSTANTS[const.name] = const
190-
191186
with path.open("r") as datafile:
192187
if path.suffix == ".json":
193188
dataset = json.load(datafile)
@@ -206,7 +201,7 @@ def _load_database_v1(path: Path) -> None:
206201
raise RuntimeError(f"Unrecognized suffix of data file {path}")
207202

208203
# read in datafile
209-
for name, desc in dataset.items():
204+
for name, desc in dataset["procedures"].items():
210205
if desc["attributes"]["predefined_function"]:
211206
predef = PredefinedFunction(name, desc)
212207
PREDEFINED_FUNCTIONS[predef.name] = predef
@@ -218,3 +213,7 @@ def _load_database_v1(path: Path) -> None:
218213
else:
219214
procedure = Procedure(name, desc)
220215
PROCEDURES[procedure.name] = procedure
216+
217+
for name, desc in dataset["constants"].items():
218+
const = Constant(name, desc)
219+
CONSTANTS[name] = const

src/pympistandard/kind.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
from dataclasses import dataclass
99
from collections import defaultdict
10-
from typing import Optional, MutableMapping, TYPE_CHECKING
10+
from typing import Optional, MutableMapping, TYPE_CHECKING, List
1111

1212

13-
from .storage import CALLBACKS
13+
from .storage import CALLBACKS, CONSTANTS
1414

1515

1616
if TYPE_CHECKING:
@@ -106,7 +106,7 @@ def has_embiggenment(self) -> bool:
106106
return False
107107

108108
@property
109-
def callback(self) -> Optional['Callback']:
109+
def callback(self) -> Optional["Callback"]:
110110
"""Access the relevant Callback object for this Kind."""
111111

112112
# NOTE this should eventually be non-optional, all FUNCTION KINDs will have a Callback
@@ -126,6 +126,12 @@ def callback(self) -> Optional['Callback']:
126126

127127
return None
128128

129+
@property
130+
def constants(self) -> List:
131+
"""Access all constants with this KIND."""
132+
133+
return [const for const in CONSTANTS.values() if const.kind == self]
134+
129135
@property
130136
def express(self) -> KindExpressions:
131137
"""Get the KindExpression object to express the Kind in a language."""

0 commit comments

Comments
 (0)