Author: bugman Date: Mon Nov 30 10:16:38 2009 New Revision: 10015 URL: http://svn.gna.org/viewcvs/relax?rev=10015&view=rev Log: Created the arg_check.is_list() function. 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=10015&r1=10014&r2=10015&view=diff ============================================================================== --- 1.3/arg_check.py (original) +++ 1.3/arg_check.py Mon Nov 30 10:16:38 2009 @@ -197,6 +197,58 @@ raise RelaxIntListIntError(name, arg) +def is_list(arg, name, size=None, can_be_none=False, can_be_empty=False, list_of_lists=False): + """Test if the argument is a list. + + @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 list_of_lists: A flag which if True allows the argument to be a list of lists of strings. + @type list_of_lists: bool + @raise RelaxListStrError: If not a list of strings. + @raise RelaxNoneListStrError: If not a list of strings or None. + """ + + # Init. + fail = False + + # An argument of None is allowed. + if can_be_none and arg == None: + return + + # Fail if not a list. + if not isinstance(arg, list): + fail = True + + # Other checks. + 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 + + # Fail. + if fail: + if can_be_none and size != None: + raise RelaxNoneListError(name, arg, size) + elif can_be_none: + raise RelaxNoneListError(name, arg) + elif size != None: + raise RelaxListError(name, arg, size) + else: + raise RelaxListError(name, arg) + + def is_num(arg, name, can_be_none=False): """Test if the argument is a number.