Skip to content

Commit 603c28a

Browse files
committed
Remove _Steps.last and _Snaps.last
They are relics from when _Steps and _Snaps were not implementing the Mapping interface correctly. The logic in the last property are now in the __len__ special method. Access to the last step or snap should be done with the canonical [-1] syntax.
1 parent 8f257a6 commit 603c28a

File tree

9 files changed

+30
-50
lines changed

9 files changed

+30
-50
lines changed

docs/sources/cookbook/nura.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Plotting a scalar diagnostic as function of control parameter
22
=============================================================
33

4-
Stagpy is suitable for scripting using StagyyData. Suppose you have a group
4+
StagPy is suitable for scripting using StagyyData. Suppose you have a group
55
of directories, each for a given set of parameters,
66
and you want to plot the results of all cases on the same figure, compute
77
statistics etc. The :class:`~stagpy.stagyydata.StagyyData` class comes in handy
@@ -27,7 +27,7 @@ the script is executed::
2727
# get the value of the Rayleigh number
2828
ran.append(sdat.par['refstate']['ra0'])
2929
# get the last value of the Nusselt number
30-
nun.append(sdat.steps.last.timeinfo['Nutop'])
30+
nun.append(sdat.steps[-1].timeinfo['Nutop'])
3131

3232
ran = np.array(ran)
3333
nun = np.array(nun)

docs/sources/stagyydata.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ Tracer data (position, mass, composition...) are accessible through
192192
:attr:`_Step.tracers<stagpy.stagyydata._Step.tracers>` using the
193193
property name as key. They are organized by block. For example,
194194
the masses of tracers in the first block is obtained with
195-
``sdat.snaps.last.tracers['Mass'][0]``. This is a one dimensional
195+
``sdat.snaps[-1].tracers['Mass'][0]``. This is a one dimensional
196196
array containing the mass of each tracers. Their positions can be
197197
recovered through the ``'x'``, ``'y'`` and ``'z'`` items.
198198

stagpy/_step.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ class Step:
361361
instances.
362362
363363
>>> sdat = StagyyData('path/to/run')
364-
>>> istep_last_snap = sdat.snaps.last.istep
365-
>>> assert(sdat.steps[istep_last_snap] is sdat.snaps.last)
364+
>>> istep_last_snap = sdat.snaps[-1].istep
365+
>>> assert(sdat.steps[istep_last_snap] is sdat.snaps[-1])
366366
>>> n = 0 # or any valid time step / snapshot index
367367
>>> assert(sdat.steps[n].sdat is sdat)
368368
>>> assert(sdat.steps[n].istep == n)

stagpy/commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def info_cmd():
2222
"""
2323
varlist = [var for var in conf.info.output.replace(',', ' ').split()]
2424
sdat = stagyydata.StagyyData(conf.core.path)
25-
lsnap = sdat.snaps.last
26-
lstep = sdat.steps.last
25+
lsnap = sdat.snaps[-1]
26+
lstep = sdat.steps[-1]
2727
print('StagYY run in {}'.format(sdat.path))
2828
if lsnap.geom.threed:
2929
dimension = '{} x {} x {}'.format(lsnap.geom.nxtot,

stagpy/processing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def ebalance(sdat, tstart=None, tend=None):
7272
tuple of :class:`numpy.array`: energy balance and time arrays.
7373
"""
7474
tseries = sdat.tseries_between(tstart, tend)
75-
rbot, rtop = misc.get_rbounds(sdat.steps.last)
75+
rbot, rtop = misc.get_rbounds(sdat.steps[-1])
7676
if rbot != 0: # spherical
7777
coefsurf = (rtop / rbot)**2
7878
volume = rbot * ((rtop / rbot)**3 - 1) / 3

stagpy/stagyydata.py

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def __init__(self, sdat):
198198
:class:`_Steps` instance.
199199
"""
200200
self.sdat = sdat
201-
self._last = UNDETERMINED
201+
self._len = UNDETERMINED
202202
self._data = {None: _step.EmptyStep()} # for non existent snaps
203203

204204
def __repr__(self):
@@ -221,7 +221,7 @@ def __getitem__(self, istep):
221221
istep -= len(self)
222222
raise error.InvalidTimestepError(
223223
self.sdat, istep,
224-
'Last istep is {}'.format(self.last.istep))
224+
'Last istep is {}'.format(len(self) - 1))
225225
if istep not in self._data:
226226
self._data[istep] = _step.Step(istep, self.sdat)
227227
return self._data[istep]
@@ -233,7 +233,10 @@ def __delitem__(self, istep):
233233
del self._data[istep]
234234

235235
def __len__(self):
236-
return self.last.istep + 1
236+
if self._len is UNDETERMINED:
237+
# not necessarily the last one...
238+
self._len = self.sdat.tseries.index[-1] + 1
239+
return self._len
237240

238241
def __iter__(self):
239242
return iter(self[:])
@@ -270,19 +273,6 @@ def filter(self, **filters):
270273
"""Build a _StepsView with requested filters."""
271274
return self[:].filter(**filters)
272275

273-
@property
274-
def last(self):
275-
"""Last time step available.
276-
277-
Example:
278-
>>> sdat = StagyyData('path/to/run')
279-
>>> assert(sdat.steps.last is sdat.steps[-1])
280-
"""
281-
if self._last is UNDETERMINED:
282-
# not necessarily the last one...
283-
self._last = self.sdat.tseries.index[-1]
284-
return self[self._last]
285-
286276

287277
class _Snaps(_Steps):
288278

@@ -343,15 +333,15 @@ def __delitem__(self, isnap):
343333
del self.sdat.steps[istep]
344334

345335
def __len__(self):
346-
if self._last is UNDETERMINED:
347-
self._last = -1
336+
if self._len is UNDETERMINED:
337+
self._len = -1
348338
if self.sdat.hdf5:
349339
isnap = -1
350340
for isnap, istep in stagyyparsers.read_time_h5(self.sdat.hdf5):
351341
self._bind(isnap, istep)
352-
self._last = isnap
342+
self._len = isnap
353343
self._all_isteps_known = True
354-
if self._last < 0:
344+
if self._len < 0:
355345
out_stem = re.escape(pathlib.Path(
356346
self.sdat.par['ioin']['output_file_stem'] + '_').name[:-1])
357347
rgx = re.compile(
@@ -360,10 +350,11 @@ def __len__(self):
360350
for fname in self.sdat._files:
361351
match = rgx.match(fname.name)
362352
if match is not None and match.group(1) in fstems:
363-
self._last = max(int(match.group(2)), self._last)
364-
if self._last < 0:
353+
self._len = max(int(match.group(2)), self._len)
354+
if self._len < 0:
365355
raise error.NoSnapshotError(self.sdat)
366-
return self._last + 1
356+
self._len += 1
357+
return self._len
367358

368359
def at_time(self, time, after=False):
369360
"""Return snap corresponding to a given physical time.
@@ -405,16 +396,6 @@ def _bind(self, isnap, istep):
405396
self._isteps[isnap] = istep
406397
self.sdat.steps[istep]._isnap = isnap
407398

408-
@property
409-
def last(self):
410-
"""Last snapshot available.
411-
412-
Example:
413-
>>> sdat = StagyyData('path/to/run')
414-
>>> assert(sdat.snaps.last is sdat.snaps[-1])
415-
"""
416-
return self[len(self) - 1]
417-
418399

419400
class _StepsView:
420401

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ def sdat(example_dir):
2121

2222
@pytest.fixture(scope='module')
2323
def step(sdat):
24-
return sdat.snaps.last
24+
return sdat.snaps[-1]

tests/test_parsers.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,21 @@ def test_rprof_invalid_prs():
2525

2626

2727
def test_fields_prs(sdat):
28-
hdr, flds = prs.fields(sdat.filename('t', sdat.snaps.last.isnap))
28+
hdr, flds = prs.fields(sdat.filename('t', len(sdat.snaps) - 1))
2929
assert flds.shape[0] == 1
3030
assert flds.shape[4] == 1
3131
assert flds.shape[1:4] == tuple(hdr['nts'])
3232

3333

3434
def test_fields_header_only_prs(sdat):
35-
hdr = prs.fields(sdat.filename('t', sdat.snaps.last.isnap),
36-
only_header=True)
35+
hdr = prs.fields(sdat.filename('t', len(sdat.snaps) - 1), only_header=True)
3736
assert hdr['nts'].shape == (3,)
3837

3938

4039
def test_fields_istep_only_prs(sdat):
41-
istep = prs.fields(sdat.filename('t', sdat.snaps.last.isnap),
40+
istep = prs.fields(sdat.filename('t', len(sdat.snaps) - 1),
4241
only_istep=True)
43-
assert istep == sdat.snaps.last.istep
42+
assert istep == sdat.snaps[-1].istep
4443

4544

4645
def test_fields_invalid_prs():

tests/test_stagyydata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_filter_even_steps(sdat):
6868

6969

7070
def test_snaps_last(sdat):
71-
assert sdat.snaps.last is sdat.snaps[-1]
71+
assert sdat.snaps[-1] is sdat.snaps[len(sdat.snaps) - 1]
7272

7373

7474
def test_snaps_empty(sdat):
@@ -77,8 +77,8 @@ def test_snaps_empty(sdat):
7777

7878

7979
def test_step_is_snap(sdat):
80-
istep = sdat.snaps.last.istep
81-
assert sdat.steps[istep] is sdat.snaps.last
80+
istep = sdat.snaps[-1].istep
81+
assert sdat.steps[istep] is sdat.snaps[-1]
8282

8383

8484
def test_step_sdat(sdat):

0 commit comments

Comments
 (0)