Author: bugman Date: Fri Aug 21 19:12:46 2009 New Revision: 9361 URL: http://svn.gna.org/viewcvs/relax?rev=9361&view=rev Log: Added the is_num_list() function. Modified: 1.3/prompt/check.py Modified: 1.3/prompt/check.py URL: http://svn.gna.org/viewcvs/relax/1.3/prompt/check.py?rev=9361&r1=9360&r2=9361&view=diff ============================================================================== --- 1.3/prompt/check.py (original) +++ 1.3/prompt/check.py Fri Aug 21 19:12:46 2009 @@ -24,7 +24,7 @@ """Argument checking functions for the relax user functions.""" # relax module imports. -from relax_errors import RelaxBoolError, RelaxFloatError, RelaxIntError, RelaxNoneFloatError, RelaxNoneIntError, RelaxNoneStrError, RelaxStrError, RelaxTupleError, RelaxTupleNumError +from relax_errors import RelaxBoolError, RelaxFloatError, RelaxIntError, RelaxNoneFloatError, RelaxListNumError, RelaxNoneIntError, RelaxNoneListNumError, RelaxNoneStrError, RelaxStrError, RelaxTupleError, RelaxTupleNumError def is_bool(arg, name): @@ -75,6 +75,77 @@ raise RelaxNoneFloatError(name, arg) +def is_int(arg, name, can_be_none=False): + """Test if the argument is an integer. + + @param arg: The argument. + @type arg: anything + @param name: The plain English name of the argument. + @type name: str + @keyword can_be_none: A flag specifying if the argument can be none. + @type can_be_none: bool + @raise RelaxIntError: If not an integer. + @raise RelaxNoneIntError: If not an integer or not None. + """ + + # An argument of None is allowed. + if can_be_none and arg == None: + return + + # Check for an integer (avoiding Booleans). + elif isinstance(arg, int) and not isinstance(arg, bool): + return + + # Fail. + else: + if not can_be_none: + raise RelaxIntError(name, arg) + else: + raise RelaxNoneIntError(name, arg) + + +def is_num_list(arg, name, size=None, can_be_none=False): + """Test if the argument is a list of numbers. + + @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 + @raise RelaxListError: If not a list. + @raise RelaxListNumError: If not a list of numbers. + """ + + # An argument of None is allowed. + if can_be_none and arg == None: + return + + # Fail if not a list. + if not isinstance(arg, list): + if can_be_none: + raise RelaxNoneListNumError(name, arg) + else: + raise RelaxListNumError(name, arg) + + # Fail size is wrong. + if size != None and len(arg) != size: + if can_be_none: + raise RelaxNoneListNumError(name, arg, size) + else: + raise RelaxListNumError(name, arg, size) + + # Fail if not numbers. + for i in range(len(params)): + if not isinstance(arg[i], float) and not isinstance(arg[i], int): + if can_be_none: + raise RelaxNoneListNumError(name, arg) + else: + raise RelaxListNumError(name, arg) + + def is_num_tuple(arg, name, size=None, can_be_none=False): """Test if the argument is a tuple of numbers. @@ -87,7 +158,7 @@ @keyword can_be_none: A flag specifying if the argument can be none. @type can_be_none: bool @raise RelaxTupleError: If not a tuple. - @raise RelaxNoneFloatError: If not an integer or not None. + @raise RelaxTupleNumError: If not a tuple of numbers. """ # An argument of None is allowed. @@ -96,7 +167,7 @@ # Fail if not a tuple. if not isinstance(arg, tuple): - raise RelaxTupleError(name, arg) + raise RelaxTupleNumError(name, arg) # Fail size is wrong. if size != None and len(arg) != size: @@ -108,35 +179,6 @@ raise RelaxTupleNumError(name, arg) -def is_int(arg, name, can_be_none=False): - """Test if the argument is an integer. - - @param arg: The argument. - @type arg: anything - @param name: The plain English name of the argument. - @type name: str - @keyword can_be_none: A flag specifying if the argument can be none. - @type can_be_none: bool - @raise RelaxIntError: If not an integer. - @raise RelaxNoneIntError: If not an integer or not None. - """ - - # An argument of None is allowed. - if can_be_none and arg == None: - return - - # Check for an integer (avoiding Booleans). - elif isinstance(arg, int) and not isinstance(arg, bool): - return - - # Fail. - else: - if not can_be_none: - raise RelaxIntError(name, arg) - else: - raise RelaxNoneIntError(name, arg) - - def is_str(arg, name, can_be_none=False): """Test if the argument is a string.