Skip to content

Commit 1d58b1f

Browse files
committed
Mv _maybe_get in FieldXmf
1 parent a301f6f commit 1d58b1f

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

stagpy/stagyyparsers.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@
3535
Mapping,
3636
Optional,
3737
Tuple,
38+
TypeVar,
3839
)
3940
from xml.etree.ElementTree import Element
4041

4142
from numpy import ndarray
4243
from pandas import DataFrame
4344

45+
T = TypeVar("T")
46+
4447

4548
def _tidy_names(
4649
names: List[str], nnames: int, extra_names: Optional[List[str]] = None
@@ -849,22 +852,6 @@ def _get_field(xdmf_file: Path, data_item: Element) -> Tuple[int, ndarray]:
849852
return icore, fld
850853

851854

852-
def _maybe_get(
853-
elt: Element,
854-
item: str,
855-
info: str,
856-
conversion: Optional[Callable[[str], Any]] = None,
857-
) -> Any:
858-
"""Extract and convert info if item is present."""
859-
maybe_item = elt.find(item)
860-
maybe_info = None
861-
if maybe_item is not None:
862-
maybe_info = maybe_item.get(info)
863-
if maybe_info is not None and conversion is not None:
864-
maybe_info = conversion(maybe_info)
865-
return maybe_info
866-
867-
868855
@dataclass(frozen=True)
869856
class XmfEntry:
870857
time: Optional[float]
@@ -880,15 +867,30 @@ class FieldXmf:
880867
def _root(self) -> Element:
881868
return xmlET.parse(str(self.path)).getroot()
882869

870+
def _maybe_get(
871+
self,
872+
elt: Element,
873+
item: str,
874+
info: str,
875+
conversion: Callable[[str], T],
876+
) -> Optional[T]:
877+
"""Extract and convert info if item is present."""
878+
maybe_item = elt.find(item)
879+
if maybe_item is not None:
880+
maybe_info = maybe_item.get(info)
881+
if maybe_info is not None:
882+
return conversion(maybe_info)
883+
return None
884+
883885
@cached_property
884886
def _data(self) -> Mapping[int, XmfEntry]:
885887
# Geometry stuff from surface field is not useful
886888
data = {}
887889
# FIXME: get isnap from a field name
888890
for isnap, snap in enumerate(self._root[0][0]):
889-
time = _maybe_get(snap, "Time", "Value", float)
890-
mo_lambda = _maybe_get(snap, "mo_lambda", "Value", float)
891-
mo_thick_sol = _maybe_get(snap, "mo_thick_sol", "Value", float)
891+
time = self._maybe_get(snap, "Time", "Value", float)
892+
mo_lambda = self._maybe_get(snap, "mo_lambda", "Value", float)
893+
mo_thick_sol = self._maybe_get(snap, "mo_thick_sol", "Value", float)
892894

893895
data[isnap] = XmfEntry(
894896
time=time,

0 commit comments

Comments
 (0)