mailr9761 - /branches/bmrb/prompt/check.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on October 13, 2009 - 19:32:
Author: bugman
Date: Tue Oct 13 19:32:05 2009
New Revision: 9761

URL: http://svn.gna.org/viewcvs/relax?rev=9761&view=rev
Log:
Created the is_int_list() function for argument checking.


Modified:
    branches/bmrb/prompt/check.py

Modified: branches/bmrb/prompt/check.py
URL: 
http://svn.gna.org/viewcvs/relax/branches/bmrb/prompt/check.py?rev=9761&r1=9760&r2=9761&view=diff
==============================================================================
--- branches/bmrb/prompt/check.py (original)
+++ branches/bmrb/prompt/check.py Tue Oct 13 19:32:05 2009
@@ -24,7 +24,7 @@
 """Argument checking functions for the relax user functions."""
 
 # relax module imports.
-from relax_errors import RelaxBoolError, RelaxFloatError, 
RelaxFunctionError, RelaxIntError, RelaxIntListIntError, RelaxNoneFloatError, 
RelaxNoneFunctionError, RelaxListNumError, RelaxListStrError, 
RelaxNoneIntError, RelaxNoneIntListIntError, 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, RelaxListIntError, 
RelaxNoneFloatError, RelaxNoneFunctionError, RelaxListNumError, 
RelaxListStrError, RelaxNoneIntError, RelaxNoneIntListIntError, 
RelaxNoneListIntError, 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
 
@@ -133,6 +133,62 @@
         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):
+    """Test if the argument is an integer or 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
+    @raise RelaxListError:          If not a list.
+    @raise RelaxListIntError:       If not a list of integers.
+    @raise RelaxNoneListIntError:   If not a list of integers 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 not integers.
+        for i in range(len(arg)):
+            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):
@@ -228,18 +284,19 @@
 def is_num_list(arg, name, size=None, can_be_none=False, can_be_empty=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
-    @keyword can_be_empty:      A flag which if True allows the list to be 
empty.
-    @type can_be_empty:         bool
-    @raise RelaxListError:      If not a list.
-    @raise RelaxListNumError:   If not 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
+    @keyword can_be_empty:          A flag which if True allows the list to 
be empty.
+    @type can_be_empty:             bool
+    @raise RelaxListError:          If not a list.
+    @raise RelaxListNumError:       If not a list of numbers.
+    @raise RelaxNoneListIntError:   If not a list of integers or None.
     """
 
     # Init.




Related Messages


Powered by MHonArc, Updated Tue Oct 13 19:40:02 2009