Module arg_check
[hide private]
[frames] | no frames]

Source Code for Module arg_check

   1  ############################################################################### 
   2  #                                                                             # 
   3  # Copyright (C) 2009-2012 Edward d'Auvergne                                   # 
   4  #                                                                             # 
   5  # This file is part of the program relax.                                     # 
   6  #                                                                             # 
   7  # relax is free software; you can redistribute it and/or modify               # 
   8  # it under the terms of the GNU General Public License as published by        # 
   9  # the Free Software Foundation; either version 2 of the License, or           # 
  10  # (at your option) any later version.                                         # 
  11  #                                                                             # 
  12  # relax is distributed in the hope that it will be useful,                    # 
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
  15  # GNU General Public License for more details.                                # 
  16  #                                                                             # 
  17  # You should have received a copy of the GNU General Public License           # 
  18  # along with relax; if not, write to the Free Software                        # 
  19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
  20  #                                                                             # 
  21  ############################################################################### 
  22   
  23  # Module docstring. 
  24  """Argument checking functions for the relax user functions.""" 
  25   
  26  # Python module imports. 
  27  from numpy import float32, float64, ndarray 
  28  try: 
  29      from numpy import float16 
  30  except ImportError: 
  31      float16 = float32    # Support for old numpy versions. 
  32  try: 
  33      from numpy import float128 
  34  except ImportError: 
  35      float128 = float64    # Support for 32-bit numpy versions. 
  36   
  37  # relax module imports. 
  38  from relax_errors import RelaxBoolError, RelaxFloatError, RelaxFunctionError, RelaxIntError, RelaxIntListIntError, RelaxListFloatError, RelaxListIntError, RelaxMatrixFloatError, RelaxNoneFloatError, RelaxNoneFunctionError, RelaxListNumError, RelaxListStrError, RelaxNoneError, RelaxNoneIntError, RelaxNoneIntListIntError, RelaxNoneListFloatError, RelaxNoneListIntError, RelaxNoneMatrixFloatError, RelaxNoneListNumError, RelaxNoneListStrError, RelaxNoneNumError, RelaxNoneNumStrListNumStrError, RelaxNoneNumTupleNumError, RelaxNoneStrError, RelaxNoneStrFileError, RelaxNoneStrListNumError, RelaxNoneStrListStrError, RelaxNumError, RelaxNumStrListNumStrError, RelaxNumTupleNumError, RelaxStrError, RelaxStrFileError, RelaxStrListNumError, RelaxStrListStrError, RelaxTupleError, RelaxTupleNumError 
  39  from relax_io import DummyFileObject 
  40  from types import FunctionType, MethodType 
  41   
  42   
43 -def check_float(num):
44 """Check if the given number is a Python or numpy float. 45 46 @param num: The number to check. 47 @type num: anything. 48 @return: True if the number is a float, False otherwise. 49 @rtype: bool 50 """ 51 52 # Standard float. 53 if isinstance(num, float): 54 return True 55 56 # Numpy floats. 57 if isinstance(num, float16): 58 return True 59 if isinstance(num, float32): 60 return True 61 if isinstance(num, float64): 62 return True 63 if isinstance(num, float128): 64 return True 65 66 # Not a float. 67 return False
68 69
70 -def is_bool(arg, name=None, raise_error=True):
71 """Test if the argument is a Boolean. 72 73 @param arg: The argument. 74 @type arg: anything 75 @keyword name: The plain English name of the argument. 76 @type name: str 77 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 78 @type raise_error: bool 79 @raise RelaxBoolError: If not a Boolean (and the raise_error flag is set). 80 @return: The answer to the question (if raise_error is not set). 81 @rtype: bool 82 """ 83 84 # Check for a Boolean. 85 if isinstance(arg, bool): 86 return True 87 88 # Fail. 89 if not raise_error: 90 return False 91 else: 92 raise RelaxBoolError(name, arg)
93 94
95 -def is_float(arg, name=None, can_be_none=False, raise_error=True):
96 """Test if the argument is a float. 97 98 @param arg: The argument. 99 @type arg: anything 100 @keyword name: The plain English name of the argument. 101 @type name: str 102 @keyword can_be_none: A flag specifying if the argument can be none. 103 @type can_be_none: bool 104 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 105 @type raise_error: bool 106 @raise RelaxFloatError: If not an integer (and the raise_error flag is set). 107 @raise RelaxNoneFloatError: If not an integer or not None (and the raise_error flag is set). 108 @return: The answer to the question (if raise_error is not set). 109 @rtype: bool 110 """ 111 112 # An argument of None is allowed. 113 if can_be_none and arg == None: 114 return True 115 116 # Check for a float. 117 if check_float(arg): 118 return True 119 120 # Fail. 121 if not raise_error: 122 return False 123 if not can_be_none: 124 raise RelaxFloatError(name, arg) 125 else: 126 raise RelaxNoneFloatError(name, arg)
127 128
129 -def is_float_array(arg, name=None, size=None, can_be_none=False, raise_error=True):
130 """Test if the argument is an array of floats. 131 132 @param arg: The argument. 133 @type arg: anything 134 @keyword name: The plain English name of the argument. 135 @type name: str 136 @keyword size: The dimension of the array. 137 @type size: None or int 138 @keyword can_be_none: A flag specifying if the argument can be none. 139 @type can_be_none: bool 140 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 141 @type raise_error: bool 142 @raise RelaxListFloatError: If not a matrix of floats (and the raise_error flag is set). 143 @raise RelaxNoneListFloatError: If not a matrix of floats or not None (and the raise_error flag is set). 144 @return: The answer to the question (if raise_error is not set). 145 @rtype: bool 146 """ 147 148 # Init. 149 fail = False 150 151 # An argument of None is allowed. 152 if can_be_none and arg == None: 153 return True 154 155 # Fail if not a list. 156 if not isinstance(arg, list) and not isinstance(arg, ndarray): 157 fail = True 158 159 # Fail if not the right dimension. 160 elif size != None and len(arg) != size: 161 fail = True 162 163 # Loop over the array. 164 else: 165 for i in range(len(arg)): 166 # Fail if not a float. 167 if not check_float(arg[i]): 168 fail = True 169 170 # Fail. 171 if fail: 172 if not raise_error: 173 return False 174 if can_be_none and size != None: 175 raise RelaxNoneListFloatError(name, arg, size) 176 elif can_be_none: 177 raise RelaxNoneListFloatError(name, arg) 178 elif size != None: 179 raise RelaxListFloatError(name, arg, size) 180 else: 181 raise RelaxListFloatError(name, arg) 182 183 # Success. 184 return True
185 186
187 -def is_float_matrix(arg, name=None, dim=(3, 3), can_be_none=False, raise_error=True):
188 """Test if the argument is a matrix of floats. 189 190 @param arg: The argument. 191 @type arg: anything 192 @keyword name: The plain English name of the argument. 193 @type name: str 194 @keyword dim: The m,n dimensions of the matrix. 195 @type dim: tuple of int 196 @keyword can_be_none: A flag specifying if the argument can be none. 197 @type can_be_none: bool 198 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 199 @type raise_error: bool 200 @raise RelaxMatrixFloatError: If not a matrix of floats (and the raise_error flag is set). 201 @raise RelaxNoneMatrixFloatError: If not a matrix of floats or not None (and the raise_error flag is set). 202 @return: The answer to the question (if raise_error is not set). 203 @rtype: bool 204 """ 205 206 # Init. 207 fail = False 208 209 # An argument of None is allowed. 210 if can_be_none and arg == None: 211 return True 212 213 # Fail if not a list. 214 if not isinstance(arg, list) and not isinstance(arg, ndarray): 215 fail = True 216 217 # Fail if not the right dimension. 218 elif dim != None and len(arg) != dim[0]: 219 fail = True 220 221 # Loop over the first dimension. 222 else: 223 for i in range(len(arg)): 224 # Fail if not a list. 225 if not isinstance(arg[i], list) and not isinstance(arg, ndarray): 226 fail = True 227 228 # Fail if not the right dimension. 229 elif len(arg[i]) != dim[1]: 230 fail = True 231 232 # Check for float elements. 233 for j in range(len(arg[i])): 234 if not check_float(arg[i][j]): 235 fail = True 236 237 # Fail. 238 if fail: 239 if not raise_error: 240 return False 241 if can_be_none and dim != None: 242 raise RelaxNoneMatrixFloatError(name, arg, dim) 243 elif can_be_none: 244 raise RelaxNoneMatrixFloatError(name, arg) 245 elif dim != None: 246 raise RelaxMatrixFloatError(name, arg, dim) 247 else: 248 raise RelaxMatrixFloatError(name, arg) 249 250 # Success. 251 return True
252 253
254 -def is_func(arg, name=None, can_be_none=False, raise_error=True):
255 """Test if the argument is a function. 256 257 @param arg: The argument. 258 @type arg: anything 259 @keyword name: The plain English name of the argument. 260 @type name: str 261 @keyword can_be_none: A flag specifying if the argument can be none. 262 @type can_be_none: bool 263 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 264 @type raise_error: bool 265 @raise RelaxFunctionError: If not a function (and the raise_error flag is set). 266 @raise RelaxNoneFunctionError: If not a function or not None (and the raise_error flag is set). 267 @return: The answer to the question (if raise_error is not set). 268 @rtype: bool 269 """ 270 271 # An argument of None is allowed. 272 if can_be_none and arg == None: 273 return True 274 275 # Check for a function. 276 if isinstance(arg, FunctionType) or isinstance(arg, MethodType): 277 return True 278 279 # Fail. 280 if not raise_error: 281 return False 282 if not can_be_none: 283 raise RelaxFunctionError(name, arg) 284 else: 285 raise RelaxNoneFunctionError(name, arg)
286 287
288 -def is_int(arg, name=None, can_be_none=False, raise_error=True):
289 """Test if the argument is an integer. 290 291 @param arg: The argument. 292 @type arg: anything 293 @keyword name: The plain English name of the argument. 294 @type name: str 295 @keyword can_be_none: A flag specifying if the argument can be none. 296 @type can_be_none: bool 297 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 298 @type raise_error: bool 299 @raise RelaxIntError: If not an integer (and the raise_error flag is set). 300 @raise RelaxNoneIntError: If not an integer or not None (and the raise_error flag is set). 301 @return: The answer to the question (if raise_error is not set). 302 @rtype: bool 303 """ 304 305 # An argument of None is allowed. 306 if can_be_none and arg == None: 307 return True 308 309 # Check for an integer (avoiding Booleans). 310 if isinstance(arg, int) and not isinstance(arg, bool): 311 return True 312 313 # Fail. 314 if not raise_error: 315 return False 316 if not can_be_none: 317 raise RelaxIntError(name, arg) 318 else: 319 raise RelaxNoneIntError(name, arg)
320 321
322 -def is_int_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, none_elements=False, raise_error=True):
323 """Test if the argument is a list of integers. 324 325 @param arg: The argument. 326 @type arg: anything 327 @keyword name: The plain English name of the argument. 328 @type name: str 329 @keyword size: The number of elements required. 330 @type size: None or int 331 @keyword can_be_none: A flag specifying if the argument can be none. 332 @type can_be_none: bool 333 @keyword can_be_empty: A flag which if True allows the list to be empty. 334 @type can_be_empty: bool 335 @keyword none_elements: A flag which if True allows the list to contain None. 336 @type none_elements: bool 337 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 338 @type raise_error: bool 339 @raise RelaxIntListIntError: If not an integer or a list of integers (and the raise_error flag is set). 340 @raise RelaxNoneIntListIntError: If not an integer, a list of integers, or None (and the raise_error flag is set). 341 @return: The answer to the question (if raise_error is not set). 342 @rtype: bool 343 """ 344 345 # Init. 346 fail = False 347 348 # An argument of None is allowed. 349 if can_be_none and arg == None: 350 return True 351 352 # Not a list. 353 if not isinstance(arg, list): 354 fail = True 355 356 # A list. 357 else: 358 # Fail size is wrong. 359 if size != None and len(arg) != size: 360 fail = True 361 362 # Fail if empty. 363 if not can_be_empty and arg == []: 364 fail = True 365 366 # Check the arguments. 367 for i in range(len(arg)): 368 # None. 369 if arg[i] == None and none_elements: 370 continue 371 372 # Check if it is an integer. 373 if not isinstance(arg[i], int): 374 fail = True 375 376 # Fail. 377 if fail: 378 if not raise_error: 379 return False 380 if can_be_none and size != None: 381 raise RelaxNoneListIntError(name, arg, size) 382 elif can_be_none: 383 raise RelaxNoneListIntError(name, arg) 384 elif size != None: 385 raise RelaxListIntError(name, arg, size) 386 else: 387 raise RelaxListIntError(name, arg) 388 389 # Success. 390 return True
391 392
393 -def is_int_or_int_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, none_elements=False, raise_error=True):
394 """Test if the argument is an integer or a list of integers. 395 396 @param arg: The argument. 397 @type arg: anything 398 @keyword name: The plain English name of the argument. 399 @type name: str 400 @keyword size: The number of elements required. 401 @type size: None or int 402 @keyword can_be_none: A flag specifying if the argument can be none. 403 @type can_be_none: bool 404 @keyword can_be_empty: A flag which if True allows the list to be empty. 405 @type can_be_empty: bool 406 @keyword none_elements: A flag which if True allows the list to contain None. 407 @type none_elements: bool 408 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 409 @type raise_error: bool 410 @raise RelaxIntListIntError: If not an integer or a list of integers (and the raise_error flag is set). 411 @raise RelaxNoneIntListIntError: If not an integer, a list of integers, or None (and the raise_error flag is set). 412 @return: The answer to the question (if raise_error is not set). 413 @rtype: bool 414 """ 415 416 # Init. 417 fail = False 418 419 # An argument of None is allowed. 420 if can_be_none and arg == None: 421 return True 422 423 # An integer 424 if not isinstance(arg, list): 425 # Check if it is an integer. 426 try: 427 is_int(arg, name) 428 except: 429 fail = True # Not an integer. 430 431 # A list. 432 else: 433 # Fail size is wrong. 434 if size != None and len(arg) != size: 435 fail = True 436 437 # Fail if empty. 438 if not can_be_empty and arg == []: 439 fail = True 440 441 # Check the arguments. 442 for i in range(len(arg)): 443 # None. 444 if arg[i] == None and none_elements: 445 continue 446 447 # Check if it is an integer. 448 try: 449 is_int(arg[i], name) 450 except: 451 fail = True # Not an integer. 452 453 # Fail. 454 if fail: 455 if not raise_error: 456 return False 457 if can_be_none and size != None: 458 raise RelaxNoneIntListIntError(name, arg, size) 459 elif can_be_none: 460 raise RelaxNoneIntListIntError(name, arg) 461 elif size != None: 462 raise RelaxIntListIntError(name, arg, size) 463 else: 464 raise RelaxIntListIntError(name, arg) 465 466 # Success. 467 return True
468 469
470 -def is_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, list_of_lists=False, raise_error=True):
471 """Test if the argument is a list. 472 473 @param arg: The argument. 474 @type arg: anything 475 @keyword name: The plain English name of the argument. 476 @type name: str 477 @keyword size: The number of elements required. 478 @type size: None or int 479 @keyword can_be_none: A flag specifying if the argument can be none. 480 @type can_be_none: bool 481 @keyword can_be_empty: A flag which if True allows the list to be empty. 482 @type can_be_empty: bool 483 @keyword list_of_lists: A flag which if True allows the argument to be a list of lists of strings. 484 @type list_of_lists: bool 485 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 486 @type raise_error: bool 487 @raise RelaxListStrError: If not a list of strings (and the raise_error flag is set). 488 @raise RelaxNoneListStrError: If not a list of strings or None (and the raise_error flag is set). 489 @return: The answer to the question (if raise_error is not set). 490 @rtype: bool 491 """ 492 493 # Init. 494 fail = False 495 496 # An argument of None is allowed. 497 if can_be_none and arg == None: 498 return True 499 500 # Fail if not a list. 501 if not isinstance(arg, list): 502 fail = True 503 504 # Other checks. 505 else: 506 # Fail size is wrong. 507 if size != None and len(arg) != size: 508 fail = True 509 510 # Fail if empty. 511 if not can_be_empty and arg == []: 512 fail = True 513 514 # Fail. 515 if fail: 516 if not raise_error: 517 return False 518 if can_be_none and size != None: 519 raise RelaxNoneListError(name, arg, size) 520 elif can_be_none: 521 raise RelaxNoneListError(name, arg) 522 elif size != None: 523 raise RelaxListError(name, arg, size) 524 else: 525 raise RelaxListError(name, arg) 526 527 # Success. 528 return True
529 530
531 -def is_none(arg, name, raise_error=True):
532 """Test if the argument is None. 533 534 @param arg: The argument. 535 @type arg: anything 536 @keyword name: The plain English name of the argument. 537 @type name: str 538 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 539 @type raise_error: bool 540 @raise RelaxNoneError: If not None (and the raise_error flag is set). 541 @return: The answer to the question (if raise_error is not set). 542 @rtype: bool 543 """ 544 545 # Check for None. 546 if arg == None: 547 return True 548 549 # Fail. 550 if not raise_error: 551 return False 552 else: 553 raise RelaxNoneError(name)
554 555
556 -def is_num(arg, name=None, can_be_none=False, raise_error=True):
557 """Test if the argument is a number. 558 559 @param arg: The argument. 560 @type arg: anything 561 @keyword name: The plain English name of the argument. 562 @type name: str 563 @keyword can_be_none: A flag specifying if the argument can be none. 564 @type can_be_none: bool 565 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 566 @type raise_error: bool 567 @raise RelaxNumError: If not a number (and the raise_error flag is set). 568 @raise RelaxNoneNumError: If not a number or not None (and the raise_error flag is set). 569 @return: The answer to the question (if raise_error is not set). 570 @rtype: bool 571 """ 572 573 # An argument of None is allowed. 574 if can_be_none and arg == None: 575 return True 576 577 # Check for floats and integers (avoiding Booleans). 578 if (check_float(arg) or isinstance(arg, int)) and not isinstance(arg, bool): 579 return True 580 581 # Fail. 582 if not raise_error: 583 return False 584 if not can_be_none: 585 raise RelaxNumError(name, arg) 586 else: 587 raise RelaxNoneNumError(name, arg)
588 589
590 -def is_num_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
591 """Test if the argument is a list of numbers. 592 593 @param arg: The argument. 594 @type arg: anything 595 @keyword name: The plain English name of the argument. 596 @type name: str 597 @keyword size: The number of elements required. 598 @type size: None or int 599 @keyword can_be_none: A flag specifying if the argument can be none. 600 @type can_be_none: bool 601 @keyword can_be_empty: A flag which if True allows the list to be empty. 602 @type can_be_empty: bool 603 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 604 @type raise_error: bool 605 @raise RelaxListError: If not a list (and the raise_error flag is set). 606 @raise RelaxListNumError: If not a list of numbers (and the raise_error flag is set). 607 @return: The answer to the question (if raise_error is not set). 608 @rtype: bool 609 """ 610 611 # Init. 612 fail = False 613 614 # An argument of None is allowed. 615 if can_be_none and arg == None: 616 return True 617 618 # Fail if not a list. 619 if not isinstance(arg, list) and not isinstance(arg, ndarray): 620 fail = True 621 622 # Other checks. 623 else: 624 # Fail size is wrong. 625 if size != None and len(arg) != size: 626 fail = True 627 628 # Fail if empty. 629 if not can_be_empty and arg == []: 630 fail = True 631 632 # Fail if not numbers. 633 for i in range(len(arg)): 634 if (not check_float(arg[i]) and not isinstance(arg[i], int)) or isinstance(arg, bool): 635 fail = True 636 637 # Fail. 638 if fail: 639 if not raise_error: 640 return False 641 if can_be_none and size != None: 642 raise RelaxNoneListNumError(name, arg, size) 643 elif can_be_none: 644 raise RelaxNoneListNumError(name, arg) 645 elif size != None: 646 raise RelaxListNumError(name, arg, size) 647 else: 648 raise RelaxListNumError(name, arg) 649 650 # Success. 651 return True
652 653
654 -def is_num_or_num_tuple(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
655 """Test if the argument is a tuple of numbers. 656 657 @param arg: The argument. 658 @type arg: anything 659 @keyword name: The plain English name of the argument. 660 @type name: str 661 @keyword size: The number of elements required. 662 @type size: None or int 663 @keyword can_be_none: A flag specifying if the argument can be none. 664 @type can_be_none: bool 665 @keyword can_be_empty: A flag which if True allows the list to be empty. 666 @type can_be_empty: bool 667 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 668 @type raise_error: bool 669 @raise RelaxTupleError: If not a tuple (and the raise_error flag is set). 670 @raise RelaxTupleNumError: If not a tuple of numbers (and the raise_error flag is set). 671 @return: The answer to the question (if raise_error is not set). 672 @rtype: bool 673 """ 674 675 # Init. 676 fail = False 677 if size != None and not isinstance(size, list): 678 size = [size] 679 680 # An argument of None is allowed. 681 if can_be_none and arg == None: 682 return True 683 684 # A number. 685 if not isinstance(arg, tuple): 686 # Check if it is a number. 687 try: 688 is_num(arg, name) 689 except: 690 fail = True 691 692 # Other checks. 693 else: 694 # Fail size is wrong. 695 if size != None and len(arg) not in size: 696 fail = True 697 698 # Fail if empty. 699 if not can_be_empty and not len(arg): 700 fail = True 701 702 # Fail if not numbers. 703 for i in range(len(arg)): 704 if (not check_float(arg[i]) and not isinstance(arg[i], int)) or isinstance(arg, bool): 705 fail = True 706 707 # Fail. 708 if fail: 709 if not raise_error: 710 return False 711 if can_be_none and size != None: 712 raise RelaxNoneNumTupleNumError(name, arg, size) 713 elif can_be_none: 714 raise RelaxNoneNumTupleNumError(name, arg) 715 elif size != None: 716 raise RelaxNumTupleNumError(name, arg, size) 717 else: 718 raise RelaxNumTupleNumError(name, arg) 719 720 # Success. 721 return True
722 723
724 -def is_num_tuple(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
725 """Test if the argument is a tuple of numbers. 726 727 @param arg: The argument. 728 @type arg: anything 729 @keyword name: The plain English name of the argument. 730 @type name: str 731 @keyword size: The number of elements required. 732 @type size: None or int 733 @keyword can_be_none: A flag specifying if the argument can be none. 734 @type can_be_none: bool 735 @keyword can_be_empty: A flag which if True allows the list to be empty. 736 @type can_be_empty: bool 737 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 738 @type raise_error: bool 739 @raise RelaxTupleError: If not a tuple (and the raise_error flag is set). 740 @raise RelaxTupleNumError: If not a tuple of numbers (and the raise_error flag is set). 741 @return: The answer to the question (if raise_error is not set). 742 @rtype: bool 743 """ 744 745 # Init. 746 fail = False 747 748 # An argument of None is allowed. 749 if can_be_none and arg == None: 750 return True 751 752 # Fail if not a tuple. 753 if not isinstance(arg, tuple): 754 fail = True 755 756 # Other checks. 757 else: 758 # Fail size is wrong. 759 if size != None and len(arg) != size: 760 fail = True 761 762 # Fail if empty. 763 if not can_be_empty and arg == []: 764 fail = True 765 766 # Fail if not numbers. 767 for i in range(len(arg)): 768 if (not check_float(arg[i]) and not isinstance(arg[i], int)) or isinstance(arg, bool): 769 fail = True 770 771 # Fail. 772 if fail: 773 if not raise_error: 774 return False 775 if can_be_none and size != None: 776 raise RelaxNoneTupleNumError(name, arg, size) 777 elif can_be_none: 778 raise RelaxNoneTupleNumError(name, arg) 779 elif size != None: 780 raise RelaxTupleNumError(name, arg, size) 781 else: 782 raise RelaxTupleNumError(name, arg) 783 784 # Success. 785 return True
786 787
788 -def is_str(arg, name=None, can_be_none=False, raise_error=True):
789 """Test if the argument is a string. 790 791 @param arg: The argument. 792 @type arg: anything 793 @keyword name: The plain English name of the argument. 794 @type name: str 795 @keyword can_be_none: A flag specifying if the argument can be none. 796 @type can_be_none: bool 797 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 798 @type raise_error: bool 799 @raise RelaxStrError: If not an integer (and the raise_error flag is set). 800 @raise RelaxNoneStrError: If not an integer or not None (and the raise_error flag is set). 801 @return: The answer to the question (if raise_error is not set). 802 @rtype: bool 803 """ 804 805 # An argument of None is allowed. 806 if can_be_none and arg == None: 807 return True 808 809 # Check for a string. 810 if isinstance(arg, str): 811 return True 812 813 # Fail. 814 if not raise_error: 815 return False 816 if not can_be_none: 817 raise RelaxStrError(name, arg) 818 else: 819 raise RelaxNoneStrError(name, arg)
820 821
822 -def is_str_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, list_of_lists=False, raise_error=True):
823 """Test if the argument is a list of strings. 824 825 @param arg: The argument. 826 @type arg: anything 827 @keyword name: The plain English name of the argument. 828 @type name: str 829 @keyword size: The number of elements required. 830 @type size: None or int 831 @keyword can_be_none: A flag specifying if the argument can be none. 832 @type can_be_none: bool 833 @keyword can_be_empty: A flag which if True allows the list to be empty. 834 @type can_be_empty: bool 835 @keyword list_of_lists: A flag which if True allows the argument to be a list of lists 836 of strings. 837 @type list_of_lists: bool 838 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 839 @type raise_error: bool 840 @raise RelaxListStrError: If not a list of strings (and the raise_error flag is set). 841 @raise RelaxNoneListStrError: If not a list of strings or None (and the raise_error flag is set). 842 @return: The answer to the question (if raise_error is not set). 843 @rtype: bool 844 """ 845 846 # Init. 847 fail = False 848 849 # An argument of None is allowed. 850 if can_be_none and arg == None: 851 return True 852 853 # Fail if not a list. 854 if not isinstance(arg, list): 855 fail = True 856 857 # Other checks. 858 else: 859 # Fail size is wrong. 860 if size != None and len(arg) != size: 861 fail = True 862 863 # Fail if empty. 864 if not can_be_empty and arg == []: 865 fail = True 866 867 # Fail if not strings. 868 for i in range(len(arg)): 869 # List of lists. 870 if list_of_lists and isinstance(arg[i], list): 871 for j in range(len(arg[i])): 872 if not isinstance(arg[i][j], str): 873 fail = True 874 875 876 # Simple list. 877 else: 878 if not isinstance(arg[i], str): 879 fail = True 880 881 # Fail. 882 if fail: 883 if not raise_error: 884 return False 885 if can_be_none and size != None: 886 raise RelaxNoneListStrError(name, arg, size) 887 elif can_be_none: 888 raise RelaxNoneListStrError(name, arg) 889 elif size != None: 890 raise RelaxListStrError(name, arg, size) 891 else: 892 raise RelaxListStrError(name, arg) 893 894 # Success. 895 return True
896 897
898 -def is_str_or_inst(arg, name=None, can_be_none=False, raise_error=True):
899 """Test if the argument is a string. 900 901 @param arg: The argument. 902 @type arg: anything 903 @keyword name: The plain English name of the argument. 904 @type name: str 905 @keyword can_be_none: A flag specifying if the argument can be none. 906 @type can_be_none: bool 907 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 908 @type raise_error: bool 909 @raise RelaxStrError: If not an integer (and the raise_error flag is set). 910 @raise RelaxNoneStrError: If not an integer or not None (and the raise_error flag is set). 911 @return: The answer to the question (if raise_error is not set). 912 @rtype: bool 913 """ 914 915 # An argument of None is allowed. 916 if can_be_none and arg == None: 917 return True 918 919 # Check for a string. 920 if isinstance(arg, str) or isinstance(arg, file) or isinstance(arg, DummyFileObject): 921 return True 922 923 # Fail. 924 if not raise_error: 925 return False 926 if not can_be_none: 927 raise RelaxStrFileError(name, arg) 928 else: 929 raise RelaxNoneStrFileError(name, arg)
930 931
932 -def is_str_or_num_or_str_num_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
933 """Test if the argument is a number, a string, a list of numbers, or a list of strings. 934 935 @param arg: The argument. 936 @type arg: anything 937 @keyword name: The plain English name of the argument. 938 @type name: str 939 @keyword size: The number of elements required. 940 @type size: None or int 941 @keyword can_be_none: A flag specifying if the argument can be none. 942 @type can_be_none: bool 943 @keyword can_be_empty: A flag which if True allows the list to be empty. 944 @type can_be_empty: bool 945 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 946 @type raise_error: bool 947 @raise RelaxNumStrListNumStrError: If not a float, a string, or a list of floats or strings (and the raise_error flag is set). 948 @raise RelaxNoneNumStrListNumStrError: If not a float, a string, a list of floats or strings, or None (and the raise_error flag is set). 949 @return: The answer to the question (if raise_error is not set). 950 @rtype: bool 951 """ 952 953 # Init. 954 fail = False 955 956 # An argument of None is allowed. 957 if can_be_none and arg == None: 958 return True 959 960 # A number or a string. 961 if not isinstance(arg, list): 962 # Check if it is a string. 963 try: 964 is_str(arg, name) 965 except: 966 # Not a string, therefore check if it is a number. 967 try: 968 is_num(arg, name) 969 except: 970 fail = True # Neither a number or a string. 971 972 # A list. 973 else: 974 # Fail size is wrong. 975 if size != None and len(arg) != size: 976 fail = True 977 978 # Fail if empty. 979 if not can_be_empty and arg == []: 980 fail = True 981 982 # Check the arguments. 983 for i in range(len(arg)): 984 # Check if it is a string. 985 try: 986 is_str(arg[i], name) 987 except: 988 # Not a string, therefore check if it is a number. 989 try: 990 is_num(arg[i], name) 991 except: 992 fail = True # Neither a number or a string. 993 994 # Fail. 995 if fail: 996 if not raise_error: 997 return False 998 if can_be_none and size != None: 999 raise RelaxNoneNumStrListNumStrError(name, arg, size) 1000 elif can_be_none: 1001 raise RelaxNoneNumStrListNumStrError(name, arg) 1002 elif size != None: 1003 raise RelaxNumStrListNumStrError(name, arg, size) 1004 else: 1005 raise RelaxNumStrListNumStrError(name, arg) 1006 1007 # Success. 1008 return True
1009 1010
1011 -def is_str_or_num_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
1012 """Test if the argument is a string or a list of numbers. 1013 1014 @param arg: The argument. 1015 @type arg: anything 1016 @keyword name: The plain English name of the argument. 1017 @type name: str 1018 @keyword size: The number of elements required. 1019 @type size: None or int 1020 @keyword can_be_none: A flag specifying if the argument can be none. 1021 @type can_be_none: bool 1022 @keyword can_be_empty: A flag which if True allows the list to be empty. 1023 @type can_be_empty: bool 1024 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 1025 @type raise_error: bool 1026 @raise RelaxStrListNumError: If not a string or a list of strings (and the raise_error flag is set). 1027 @raise RelaxNoneStrListNumError: If not a string, a list of strings, or None (and the raise_error flag is set). 1028 @return: The answer to the question (if raise_error is not set). 1029 @rtype: bool 1030 """ 1031 1032 # Init. 1033 fail = False 1034 1035 # An argument of None is allowed. 1036 if can_be_none and arg == None: 1037 return True 1038 1039 # A string. 1040 if not isinstance(arg, list): 1041 # Check if it is a string. 1042 try: 1043 is_str(arg, name) 1044 except: 1045 fail = True # Not a string. 1046 1047 # A list. 1048 else: 1049 # Fail size is wrong. 1050 if size != None and len(arg) != size: 1051 fail = True 1052 1053 # Fail if empty. 1054 if not can_be_empty and arg == []: 1055 fail = True 1056 1057 # Check the arguments. 1058 for i in range(len(arg)): 1059 # Check if it is a number. 1060 try: 1061 is_num(arg[i], name) 1062 except: 1063 fail = True # Not a number. 1064 1065 # Fail. 1066 if fail: 1067 if not raise_error: 1068 return False 1069 if can_be_none and size != None: 1070 raise RelaxNoneStrListNumError(name, arg, size) 1071 elif can_be_none: 1072 raise RelaxNoneStrListNumError(name, arg) 1073 elif size != None: 1074 raise RelaxStrListNumError(name, arg, size) 1075 else: 1076 raise RelaxStrListNumError(name, arg) 1077 1078 # Success. 1079 return True
1080 1081
1082 -def is_str_or_str_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
1083 """Test if the argument is a string or a list of strings. 1084 1085 @param arg: The argument. 1086 @type arg: anything 1087 @keyword name: The plain English name of the argument. 1088 @type name: str 1089 @keyword size: The number of elements required. 1090 @type size: None or int 1091 @keyword can_be_none: A flag specifying if the argument can be none. 1092 @type can_be_none: bool 1093 @keyword can_be_empty: A flag which if True allows the list to be empty. 1094 @type can_be_empty: bool 1095 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 1096 @type raise_error: bool 1097 @raise RelaxStrListStrError: If not a string or a list of strings (and the raise_error flag is set). 1098 @raise RelaxNoneStrListStrError: If not a string, a list of strings, or None (and the raise_error flag is set). 1099 @return: The answer to the question (if raise_error is not set). 1100 @rtype: bool 1101 """ 1102 1103 # Init. 1104 fail = False 1105 1106 # An argument of None is allowed. 1107 if can_be_none and arg == None: 1108 return True 1109 1110 # A string. 1111 if not isinstance(arg, list): 1112 # Check if it is a string. 1113 try: 1114 is_str(arg, name) 1115 except: 1116 fail = True # Not a string. 1117 1118 # A list. 1119 else: 1120 # Fail size is wrong. 1121 if size != None and len(arg) != size: 1122 fail = True 1123 1124 # Fail if empty. 1125 if not can_be_empty and arg == []: 1126 fail = True 1127 1128 # Check the arguments. 1129 for i in range(len(arg)): 1130 # Check if it is a string. 1131 try: 1132 is_str(arg[i], name) 1133 except: 1134 fail = True # Not a string. 1135 1136 # Fail. 1137 if fail: 1138 if not raise_error: 1139 return False 1140 if can_be_none and size != None: 1141 raise RelaxNoneStrListStrError(name, arg, size) 1142 elif can_be_none: 1143 raise RelaxNoneStrListStrError(name, arg) 1144 elif size != None: 1145 raise RelaxStrListStrError(name, arg, size) 1146 else: 1147 raise RelaxStrListStrError(name, arg) 1148 1149 # Success. 1150 return True
1151 1152
1153 -def is_tuple(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
1154 """Test if the argument is a tuple. 1155 1156 @param arg: The argument. 1157 @type arg: anything 1158 @keyword name: The plain English name of the argument. 1159 @type name: str 1160 @keyword size: The number of elements required. 1161 @type size: None or int 1162 @keyword can_be_none: A flag specifying if the argument can be none. 1163 @type can_be_none: bool 1164 @keyword can_be_empty: A flag which if True allows the list to be empty. 1165 @type can_be_empty: bool 1166 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 1167 @type raise_error: bool 1168 @raise RelaxTupleError: If not a tuple (and the raise_error flag is set). 1169 @raise RelaxNoneTupleError: If not a tuple or not None (and the raise_error flag is set). 1170 @return: The answer to the question (if raise_error is not set). 1171 @rtype: bool 1172 """ 1173 1174 # Init. 1175 fail = False 1176 1177 # An argument of None is allowed. 1178 if can_be_none and arg == None: 1179 return True 1180 1181 # Fail if not a tuple. 1182 if not isinstance(arg, tuple): 1183 fail = True 1184 1185 # Other checks. 1186 else: 1187 # Fail size is wrong. 1188 if size != None and len(arg) != size: 1189 fail = True 1190 1191 # Fail if empty. 1192 if not can_be_empty and arg == []: 1193 fail = True 1194 1195 # Fail. 1196 if fail: 1197 if not raise_error: 1198 return False 1199 if can_be_none and size != None: 1200 raise RelaxNoneTupleError(name, arg, size) 1201 elif can_be_none: 1202 raise RelaxNoneTupleError(name, arg) 1203 elif size != None: 1204 raise RelaxTupleError(name, arg, size) 1205 else: 1206 raise RelaxTupleError(name, arg) 1207 1208 # Success. 1209 return True
1210