Author: bugman Date: Wed Oct 26 11:40:17 2011 New Revision: 14923 URL: http://svn.gna.org/viewcvs/relax?rev=14923&view=rev Log: Created the arg_check.is_int_list() function for checking for lists of integers. Modified: 1.3/arg_check.py Modified: 1.3/arg_check.py URL: http://svn.gna.org/viewcvs/relax/1.3/arg_check.py?rev=14923&r1=14922&r2=14923&view=diff ============================================================================== --- 1.3/arg_check.py (original) +++ 1.3/arg_check.py Wed Oct 26 11:40:17 2011 @@ -27,7 +27,7 @@ from numpy import ndarray # relax module imports. -from relax_errors import RelaxBoolError, RelaxFloatError, RelaxFunctionError, RelaxIntError, RelaxIntListIntError,RelaxListFloatError, RelaxMatrixFloatError, RelaxNoneFloatError, RelaxNoneFunctionError, RelaxListNumError, RelaxListStrError, RelaxNoneIntError, RelaxNoneIntListIntError, RelaxNoneListFloatError, RelaxNoneMatrixFloatError, RelaxNoneListNumError, RelaxNoneListStrError, RelaxNoneNumError, RelaxNoneNumStrListNumStrError, RelaxNoneNumTupleNumError, RelaxNoneStrError, RelaxNoneStrFileError, RelaxNoneStrListNumError, RelaxNoneStrListStrError, RelaxNumError, RelaxNumStrListNumStrError, RelaxNumTupleNumError, RelaxStrError, RelaxStrFileError, RelaxStrListNumError, RelaxStrListStrError, RelaxTupleError, RelaxTupleNumError +from relax_errors import RelaxBoolError, RelaxFloatError, RelaxFunctionError, RelaxIntError, RelaxIntListIntError,RelaxListFloatError, RelaxListIntError, RelaxMatrixFloatError, RelaxNoneFloatError, RelaxNoneFunctionError, RelaxListNumError, RelaxListStrError, RelaxNoneIntError, RelaxNoneIntListIntError, RelaxNoneListFloatError, RelaxNoneListIntError, RelaxNoneMatrixFloatError, RelaxNoneListNumError, RelaxNoneListStrError, RelaxNoneNumError, RelaxNoneNumStrListNumStrError, RelaxNoneNumTupleNumError, RelaxNoneStrError, RelaxNoneStrFileError, RelaxNoneStrListNumError, RelaxNoneStrListStrError, RelaxNumError, RelaxNumStrListNumStrError, RelaxNumTupleNumError, RelaxStrError, RelaxStrFileError, RelaxStrListNumError, RelaxStrListStrError, RelaxTupleError, RelaxTupleNumError from relax_io import DummyFileObject from types import FunctionType @@ -243,6 +243,68 @@ raise RelaxIntError(name, arg) else: raise RelaxNoneIntError(name, arg) + + +def is_int_list(arg, name, size=None, can_be_none=False, can_be_empty=False, none_elements=False): + """Test if the argument is a list of integers. + + @param arg: The argument. + @type arg: anything + @param name: The plain English name of the argument. + @type name: str + @keyword size: The number of elements required. + @type size: None or int + @keyword can_be_none: A flag specifying if the argument can be none. + @type can_be_none: bool + @keyword can_be_empty: A flag which if True allows the list to be empty. + @type can_be_empty: bool + @keyword none_elements: A flag which if True allows the list to contain None. + @type none_elements: bool + @raise RelaxIntListIntError: If not an integer or a list of integers. + @raise RelaxNoneIntListIntError: If not an integer, a list of integers, or None. + """ + + # Init. + fail = False + + # An argument of None is allowed. + if can_be_none and arg == None: + return + + # Not a list. + if not isinstance(arg, list): + fail = True + + # A list. + else: + # Fail size is wrong. + if size != None and len(arg) != size: + fail = True + + # Fail if empty. + if not can_be_empty and arg == []: + fail = True + + # Check the arguments. + for i in range(len(arg)): + # None. + if arg[i] == None and none_elements: + continue + + # Check if it is an integer. + if not isinstance(arg[i], int): + fail = True + + # Fail. + if fail: + if can_be_none and size != None: + raise RelaxNoneListIntError(name, arg, size) + elif can_be_none: + raise RelaxNoneListIntError(name, arg) + elif size != None: + raise RelaxListIntError(name, arg, size) + else: + raise RelaxListIntError(name, arg) def is_int_or_int_list(arg, name, size=None, can_be_none=False, can_be_empty=False, none_elements=False):