Skip to content

Commit d913560

Browse files
committed
New NoRefstateError raised when no refstate output
This allow for simpler types associated with refstate. The stagyydata._Refstate class is annotated in this commit.
1 parent 60a6d23 commit d913560

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

stagpy/error.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ def __init__(self, step: Step):
4848
super().__init__(f"no geometry info found for {step!r}")
4949

5050

51+
class NoRefstateError(StagpyError):
52+
"""Raised when no refstate output can be found.
53+
54+
Attributes:
55+
sdat: the :class:`~stagpy.stagyydata.StagyyData` instance for which no
56+
refstate output was found.
57+
"""
58+
59+
def __init__(self, sdat: StagyyData):
60+
self.sdat = sdat
61+
super().__init__(f"no refstate found for {sdat!r}")
62+
63+
5164
class NoParFileError(StagpyError):
5265
"""Raised when no par file can be found.
5366

stagpy/refstate.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ def cmd():
4242
conf.plot
4343
"""
4444
sdat = StagyyData()
45-
if sdat.refstate.adiabats is None:
46-
return
4745

4846
lov = conf.refstate.plot.split(',')
4947
if not lov:

stagpy/stagyydata.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@
88
"""
99

1010
from __future__ import annotations
11+
from itertools import zip_longest
1112
import re
1213
import pathlib
13-
from itertools import zip_longest
14+
import typing
1415

1516
import numpy as np
1617

1718
from . import conf, error, parfile, phyvars, stagyyparsers, _helpers, _step
1819
from ._helpers import CachedReadOnlyProperty as crop
1920
from .datatypes import Rprof, Tseries, Vart
2021

22+
if typing.TYPE_CHECKING:
23+
from typing import Tuple, List
24+
from pandas import DataFrame
25+
2126

2227
def _as_view_item(obj):
2328
"""Return None or a suitable iterable to build a _StepsView."""
@@ -107,24 +112,26 @@ class _Refstate:
107112
object.
108113
109114
Args:
110-
sdat (:class:`StagyyData`): the StagyyData instance owning the
111-
:class:`_Steps` instance.
115+
sdat: the StagyyData instance owning the :class:`_Refstate` instance.
112116
"""
113117

114-
def __init__(self, sdat):
118+
def __init__(self, sdat: StagyyData):
115119
self._sdat = sdat
116120

117121
@crop
118-
def _data(self):
122+
def _data(self) -> Tuple[List[List[DataFrame]], List[DataFrame]]:
119123
"""Read reference state profile."""
120124
reffile = self._sdat.filename('refstat.dat')
121125
if self._sdat.hdf5 and not reffile.is_file():
122126
# check legacy folder as well
123127
reffile = self._sdat.filename('refstat.dat', force_legacy=True)
124-
return stagyyparsers.refstate(reffile)
128+
data = stagyyparsers.refstate(reffile)
129+
if data is None:
130+
raise error.NoRefstateError(self._sdat)
131+
return data
125132

126133
@property
127-
def systems(self):
134+
def systems(self) -> List[List[DataFrame]]:
128135
"""Reference state profiles of phases.
129136
130137
It is a list of list of :class:`pandas.DataFrame` containing
@@ -139,7 +146,7 @@ def systems(self):
139146
return self._data[0]
140147

141148
@property
142-
def adiabats(self):
149+
def adiabats(self) -> List[DataFrame]:
143150
"""Adiabatic reference state profiles.
144151
145152
It is a list of :class:`pandas.DataFrame` containing the reference

stagpy/stagyyparsers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def _clean_names_refstate(names: List[str]) -> List[str]:
247247

248248
def refstate(
249249
reffile: Path, ncols: int = 8
250-
) -> Tuple[Optional[List[List[DataFrame]]], Optional[List[DataFrame]]]:
250+
) -> Optional[Tuple[List[List[DataFrame]], List[DataFrame]]]:
251251
"""Extract reference state profiles.
252252
253253
Args:
@@ -266,7 +266,7 @@ def refstate(
266266
item being the combined adiabat.
267267
"""
268268
if not reffile.is_file():
269-
return None, None
269+
return None
270270
data = pd.read_csv(reffile, delim_whitespace=True, dtype=str,
271271
header=None, names=range(ncols),
272272
engine='c', memory_map=True, on_bad_lines='skip')

0 commit comments

Comments
 (0)