Skip to content

Commit b88fb60

Browse files
committed
StagyyData.nfields_max is a property
This allows for a cleaner handling of spurious values. Values <6 are now forbidden. Attempting to set nfields_max to a value <6 raises an InvalidNfieldsError. No limit is now achieved with the None value.
1 parent 7aac569 commit b88fb60

File tree

4 files changed

+64
-13
lines changed

4 files changed

+64
-13
lines changed

stagpy/_step.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,10 @@ def _set(self, name, fld):
270270
sdat = self.step.sdat
271271
col_fld = sdat.collected_fields
272272
col_fld.append((self.step.istep, name))
273-
while len(col_fld) > sdat.nfields_max > 5:
274-
istep, fld_name = col_fld.pop(0)
275-
del sdat.steps[istep].fields[fld_name]
273+
if sdat.nfields_max is not None:
274+
while len(col_fld) > sdat.nfields_max:
275+
istep, fld_name = col_fld.pop(0)
276+
del sdat.steps[istep].fields[fld_name]
276277
self._data[name] = fld
277278

278279
def __delitem__(self, name):

stagpy/error.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ def __init__(self, fraction):
123123
.format(fraction))
124124

125125

126+
class InvalidNfieldsError(StagpyError):
127+
128+
"""Raised when invalid nfields_max is requested."""
129+
130+
def __init__(self, nfields):
131+
"""Initialization of instances:
132+
133+
Args:
134+
nfields (int): the invalid number of fields.
135+
136+
Attributes:
137+
nfields (int): the invalid number of field.
138+
"""
139+
self.nfields = nfields
140+
super().__init__('nfields_max should be >5 (received {})'
141+
.format(nfields))
142+
143+
126144
class InvalidZoomError(StagpyError):
127145

128146
"""Raised when invalid zoom is requested."""

stagpy/stagyydata.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -519,25 +519,19 @@ class StagyyData:
519519

520520
"""Generic lazy interface to StagYY output data."""
521521

522-
def __init__(self, path, nfields_max=50):
522+
def __init__(self, path):
523523
"""Initialization of instances:
524524
525525
Args:
526526
path (pathlike): path of the StagYY run. It can either be the path
527527
of the directory containing the par file, or the path of the
528528
par file. If the path given is a directory, the path of the par
529529
file is assumed to be path/par.
530-
nfields_max (int): the maximum number of scalar fields that should
531-
be kept in memory. Set to a value smaller than 6 if you want no
532-
limit.
533530
534531
Attributes:
535532
steps (:class:`_Steps`): collection of time steps.
536533
snaps (:class:`_Snaps`): collection of snapshots.
537534
scales (:class:`_Scales`): dimensionful scaling factors.
538-
nfields_max (int): the maximum number of scalar fields that should
539-
be kept in memory. Set to a value smaller than 6 if you want no
540-
limit.
541535
collected_fields (list of (int, str)): list of fields currently in
542536
memory, described by istep and field name.
543537
"""
@@ -558,12 +552,11 @@ def __init__(self, path, nfields_max=50):
558552
self.refstate = _Refstate(self)
559553
self.steps = _Steps(self)
560554
self.snaps = _Snaps(self)
561-
self.nfields_max = nfields_max
555+
self._nfields_max = 50
562556
self.collected_fields = []
563557

564558
def __repr__(self):
565-
return 'StagyyData({}, nfields_max={})'.format(
566-
repr(self.path), self.nfields_max)
559+
return 'StagyyData({})'.format(repr(self.path))
567560

568561
def __str__(self):
569562
return 'StagyyData in {}'.format(self.path)
@@ -684,6 +677,24 @@ def walk(self):
684677
return self.steps[conf.core.timesteps]
685678
return self.snaps[-1, ]
686679

680+
@property
681+
def nfields_max(self):
682+
"""Maximum number of scalar fields kept in memory.
683+
684+
Setting this to a value lower or equal to 5 raises a
685+
:class:`~stagpy.error.InvalidNfieldsError`. Set this to ``None`` if
686+
you do not want any limit on the number of scalar fields kept in
687+
memory. Defaults to 50.
688+
"""
689+
return self._nfields_max
690+
691+
@nfields_max.setter
692+
def nfields_max(self, nfields):
693+
"""Check nfields > 5 or None."""
694+
if nfields is not None and nfields <= 5:
695+
raise error.InvalidNfieldsError(nfields)
696+
self._nfields_max = nfields
697+
687698
def scale(self, data, unit):
688699
"""Scales quantity to obtain dimensionful quantity.
689700

tests/test_stagyydata.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,33 @@
33
import pandas
44
import stagpy.stagyydata
55
import stagpy._step
6+
import stagpy.error
67

78

89
def test_sdat_path(example_dir, sdat):
910
assert sdat.path == example_dir
1011

1112

13+
def test_sdat_deflt_nfields_max(sdat):
14+
assert sdat.nfields_max == 50
15+
16+
17+
def test_sdat_set_nfields_max(sdat):
18+
sdat.nfields_max = 6
19+
assert sdat.nfields_max == 6
20+
21+
22+
def test_sdat_set_nfields_max_none(sdat):
23+
sdat.nfields_max = None
24+
assert sdat.nfields_max is None
25+
26+
27+
def test_sdat_set_nfields_max_invalid(sdat):
28+
with pytest.raises(stagpy.error.InvalidNfieldsError) as err:
29+
sdat.nfields_max = 5
30+
assert err.value.nfields == 5
31+
32+
1233
def test_sdat_par(sdat):
1334
assert isinstance(sdat.par, f90nml.namelist.Namelist)
1435

0 commit comments

Comments
 (0)