From 3c5f09b4e6604c92baabe820a58f8d79d2678ce0 Mon Sep 17 00:00:00 2001 From: Max Linke Date: Fri, 8 Jul 2016 10:23:29 +0200 Subject: [PATCH] Ensure old API still works Since user code might already exist we keep make sure the old API still works as well. --- package/MDAnalysis/analysis/base.py | 17 +++++++++++------ testsuite/MDAnalysisTests/analysis/test_base.py | 11 +++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/package/MDAnalysis/analysis/base.py b/package/MDAnalysis/analysis/base.py index 857be2b2542..068003a2d5d 100644 --- a/package/MDAnalysis/analysis/base.py +++ b/package/MDAnalysis/analysis/base.py @@ -80,13 +80,8 @@ def __init__(self, trajectory, start=None, quiet : bool, optional Turn of verbosity """ - self._setup_frames(trajectory, start, stop, step) - interval = int(self.n_frames // 100) - if interval == 0: - interval = 1 - self._pm = ProgressMeter(self.nframes if self.nframes else 1, - interval=interval, quiet=quiet) self._quiet = quiet + self._setup_frames(trajectory, start, stop, step) def _setup_frames(self, trajectory, start=None, stop=None, step=None): @@ -97,6 +92,16 @@ def _setup_frames(self, trajectory, start=None, self.stop = stop self.step = step self.n_frames = len(range(start, stop, step)) + interval = int(self.n_frames // 100) + if interval == 0: + interval = 1 + + # if _quiet doesn't exists because __init__ wasn't called set it to + # true + if not hasattr(self, '_quiet'): + self._quiet = True + self._pm = ProgressMeter(self.n_frames if self.n_frames else 1, + interval=interval, quiet=self._quiet) def _single_frame(self): """Calculate data from a single frame of trajectory diff --git a/testsuite/MDAnalysisTests/analysis/test_base.py b/testsuite/MDAnalysisTests/analysis/test_base.py index 8a783d4bd7b..359d48b6db8 100644 --- a/testsuite/MDAnalysisTests/analysis/test_base.py +++ b/testsuite/MDAnalysisTests/analysis/test_base.py @@ -44,6 +44,14 @@ def __init__(self, reader, **kwargs): super(IncompleteAnalysis, self).__init__(reader, **kwargs) +class OldAPIAnalysis(AnalysisBase): + def __init__(self, reader, **kwargs): + self._setup_frames(reader, **kwargs) + + def _single_frame(self): + pass + + class TestAnalysisBase(object): @dec.skipif(parser_not_found('DCD'), 'DCD parser not available. Are you using python 3?') @@ -89,3 +97,6 @@ def test_quiet(self): @raises(NotImplementedError) def test_incomplete_defined_analysis(self): IncompleteAnalysis(self.u.trajectory).run() + + def test_old_api(self): + OldAPIAnalysis(self.u.trajectory).run()