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