Author: bugman Date: Fri Sep 26 11:31:29 2014 New Revision: 26051 URL: http://svn.gna.org/viewcvs/relax?rev=26051&view=rev Log: Created a special Check class based on the strategy design pattern. This is in the new lib.checks module. The class will be used to simplify and unify all of the check_*() functions in the pipe_control and specific_analyses packages. Added: trunk/lib/checks.py Modified: trunk/lib/__init__.py Modified: trunk/lib/__init__.py URL: http://svn.gna.org/viewcvs/relax/trunk/lib/__init__.py?rev=26051&r1=26050&r2=26051&view=diff ============================================================================== --- trunk/lib/__init__.py (original) +++ trunk/lib/__init__.py Fri Sep 26 11:31:29 2014 @@ -29,6 +29,7 @@ 'arg_check', 'auto_relaxation', 'check_types', + 'checks', 'chemical_shift', 'compat', 'curve_fit', Added: trunk/lib/checks.py URL: http://svn.gna.org/viewcvs/relax/trunk/lib/checks.py?rev=26051&view=auto ============================================================================== --- trunk/lib/checks.py (added) +++ trunk/lib/checks.py Fri Sep 26 11:31:29 2014 @@ -0,0 +1,72 @@ +from __future__ import absolute_import +############################################################################### +# # +# Copyright (C) 2014 Edward d'Auvergne # +# # +# This file is part of the program relax (http://www.nmr-relax.com). # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <http://www.gnu.org/licenses/>. # +# # +############################################################################### + +# Module docstring. +"""Module for the Check class based on the strategy design pattern.""" + +# Python module imports. +from types import MethodType +from warnings import warn + +# relax module imports. +from lib.errors import RelaxError +from lib.warnings import RelaxWarning + + +class Check: + """Data checking class based on the U{strategy design pattern<https://en.wikipedia.org/wiki/Strategy_pattern>}.""" + + def __init__(self, function): + """Convert the function argument into a class instance method. + + @param function: The function to convert into the self.checks class instance method which is called from the __call__ method. + @type function: function + """ + + # Convert the function into a method of this class instance. + self.checks = MethodType(function, self, Check) + + + def __call__(self, escalate=0, *args, **kargs): + """Make the object callable, and perform the checks. + + This will call the function used to initialise the class and then + + + @keyword escalate: The feedback to give if the check fails. This can be 0 for no printouts, 1 to throw a RelaxWarning, or 2 to raise a RelaxError. + @type escalate: int + @raises RelaxError: If escalate is set to 2 and the check fails. + @return: True if the check passes, False otherwise. + @rtype: bool + """ + + # Perform the check. + check_ok, msg = self.checks(*args, **kargs) + + # Warnings and errors. + if not check_ok and escalate == 1: + warn(RelaxWarning(msg)) + elif not check_ok and escalate == 2: + raise RelaxError(msg) + + # Return the status. + return check_ok