Package lib :: Module arg_check
[hide private]
[frames] | no frames]

Source Code for Module lib.arg_check

   1  ############################################################################### 
   2  #                                                                             # 
   3  # Copyright (C) 2009-2013 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 lib.check_types 
  30  from lib.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 lib.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 lib.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 lib.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 lib.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 if isinstance(dim, int): 249 dim = [dim] 250 251 # An argument of None is allowed. 252 if can_be_none and arg == None: 253 return True 254 255 # Fail if not a list. 256 if not isinstance(arg, list) and not isinstance(arg, ndarray): 257 fail = True 258 259 # Fail on empty lists. 260 elif not len(arg): 261 fail = True 262 263 # Fail if not the right dimension. 264 elif dim != None and len(arg) != dim[0]: 265 fail = True 266 267 # Fail if not a rank-2 array. 268 elif dim != None and len(dim) == 2 and not isinstance(arg[0], list) and not isinstance(arg[0], ndarray): 269 fail = True 270 271 # Fail if not a rank-3 array. 272 elif dim != None and len(dim) == 3 and not isinstance(arg[0][0], list) and not isinstance(arg[0][0], ndarray): 273 fail = True 274 275 # Fail if not a rank-4 array. 276 elif dim != None and len(dim) == 4 and not isinstance(arg[0][0][0], list) and not isinstance(arg[0][0][0], ndarray): 277 fail = True 278 279 # Individual element checks. 280 else: 281 # Create a flat list. 282 if isinstance(arg[0], list) or isinstance(arg[0], ndarray): 283 elements = [item for sublist in arg for item in sublist] 284 else: 285 elements = arg 286 287 # Check for float elements. 288 for i in range(len(elements)): 289 if not lib.check_types.is_float(elements[i]): 290 fail = True 291 292 # Fail. 293 if fail: 294 if not raise_error: 295 return False 296 if can_be_none and dim != None: 297 raise RelaxNoneListFloatError(name, arg, dim) 298 elif can_be_none: 299 raise RelaxNoneListFloatError(name, arg) 300 elif dim != None: 301 raise RelaxListFloatError(name, arg, dim) 302 else: 303 raise RelaxListFloatError(name, arg) 304 305 # Success. 306 return True
307 308
309 -def is_func(arg, name=None, can_be_none=False, raise_error=True):
310 """Test if the argument is a function. 311 312 @param arg: The argument. 313 @type arg: anything 314 @keyword name: The plain English name of the argument. 315 @type name: str 316 @keyword can_be_none: A flag specifying if the argument can be none. 317 @type can_be_none: bool 318 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 319 @type raise_error: bool 320 @raise RelaxFunctionError: If not a function (and the raise_error flag is set). 321 @raise RelaxNoneFunctionError: If not a function or not None (and the raise_error flag is set). 322 @return: The answer to the question (if raise_error is not set). 323 @rtype: bool 324 """ 325 326 # An argument of None is allowed. 327 if can_be_none and arg == None: 328 return True 329 330 # Check for a function. 331 if isinstance(arg, FunctionType) or isinstance(arg, MethodType): 332 return True 333 334 # Fail. 335 if not raise_error: 336 return False 337 if not can_be_none: 338 raise RelaxFunctionError(name, arg) 339 else: 340 raise RelaxNoneFunctionError(name, arg)
341 342
343 -def is_int(arg, name=None, can_be_none=False, raise_error=True):
344 """Test if the argument is an integer. 345 346 @param arg: The argument. 347 @type arg: anything 348 @keyword name: The plain English name of the argument. 349 @type name: str 350 @keyword can_be_none: A flag specifying if the argument can be none. 351 @type can_be_none: bool 352 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 353 @type raise_error: bool 354 @raise RelaxIntError: If not an integer (and the raise_error flag is set). 355 @raise RelaxNoneIntError: If not an integer or not None (and the raise_error flag is set). 356 @return: The answer to the question (if raise_error is not set). 357 @rtype: bool 358 """ 359 360 # An argument of None is allowed. 361 if can_be_none and arg == None: 362 return True 363 364 # Check for an integer (avoiding Booleans). 365 if isinstance(arg, int) and not isinstance(arg, bool): 366 return True 367 368 # Fail. 369 if not raise_error: 370 return False 371 if not can_be_none: 372 raise RelaxIntError(name, arg) 373 else: 374 raise RelaxNoneIntError(name, arg)
375 376
377 -def is_int_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, none_elements=False, raise_error=True):
378 """Test if the argument is a list of integers. 379 380 @param arg: The argument. 381 @type arg: anything 382 @keyword name: The plain English name of the argument. 383 @type name: str 384 @keyword size: The number of elements required. 385 @type size: None or int 386 @keyword can_be_none: A flag specifying if the argument can be none. 387 @type can_be_none: bool 388 @keyword can_be_empty: A flag which if True allows the list to be empty. 389 @type can_be_empty: bool 390 @keyword none_elements: A flag which if True allows the list to contain None. 391 @type none_elements: bool 392 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 393 @type raise_error: bool 394 @raise RelaxIntListIntError: If not an integer or a list of integers (and the raise_error flag is set). 395 @raise RelaxNoneIntListIntError: If not an integer, a list of integers, or None (and the raise_error flag is set). 396 @return: The answer to the question (if raise_error is not set). 397 @rtype: bool 398 """ 399 400 # Init. 401 fail = False 402 403 # An argument of None is allowed. 404 if can_be_none and arg == None: 405 return True 406 407 # Not a list. 408 if not isinstance(arg, list): 409 fail = True 410 411 # A list. 412 else: 413 # Fail size is wrong. 414 if size != None and len(arg) != size: 415 fail = True 416 417 # Fail if empty. 418 if not can_be_empty and arg == []: 419 fail = True 420 421 # Check the arguments. 422 for i in range(len(arg)): 423 # None. 424 if arg[i] == None and none_elements: 425 continue 426 427 # Check if it is an integer. 428 if not isinstance(arg[i], int): 429 fail = True 430 431 # Fail. 432 if fail: 433 if not raise_error: 434 return False 435 if can_be_none and size != None: 436 raise RelaxNoneListIntError(name, arg, size) 437 elif can_be_none: 438 raise RelaxNoneListIntError(name, arg) 439 elif size != None: 440 raise RelaxListIntError(name, arg, size) 441 else: 442 raise RelaxListIntError(name, arg) 443 444 # Success. 445 return True
446 447
448 -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):
449 """Test if the argument is an integer or a list of integers. 450 451 @param arg: The argument. 452 @type arg: anything 453 @keyword name: The plain English name of the argument. 454 @type name: str 455 @keyword size: The number of elements required. 456 @type size: None or int 457 @keyword can_be_none: A flag specifying if the argument can be none. 458 @type can_be_none: bool 459 @keyword can_be_empty: A flag which if True allows the list to be empty. 460 @type can_be_empty: bool 461 @keyword none_elements: A flag which if True allows the list to contain None. 462 @type none_elements: bool 463 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 464 @type raise_error: bool 465 @raise RelaxIntListIntError: If not an integer or a list of integers (and the raise_error flag is set). 466 @raise RelaxNoneIntListIntError: If not an integer, a list of integers, or None (and the raise_error flag is set). 467 @return: The answer to the question (if raise_error is not set). 468 @rtype: bool 469 """ 470 471 # Init. 472 fail = False 473 474 # An argument of None is allowed. 475 if can_be_none and arg == None: 476 return True 477 478 # An integer 479 if not isinstance(arg, list): 480 if not is_int(arg, raise_error=False): 481 fail = True 482 483 # A list. 484 else: 485 # Fail size is wrong. 486 if size != None and len(arg) != size: 487 fail = True 488 489 # Fail if empty. 490 if not can_be_empty and arg == []: 491 fail = True 492 493 # Check the arguments. 494 for i in range(len(arg)): 495 # None. 496 if arg[i] == None and none_elements: 497 continue 498 499 # Check if it is an integer. 500 if not is_int(arg[i], raise_error=False): 501 fail = True 502 503 # Fail. 504 if fail: 505 if not raise_error: 506 return False 507 if can_be_none and size != None: 508 raise RelaxNoneIntListIntError(name, arg, size) 509 elif can_be_none: 510 raise RelaxNoneIntListIntError(name, arg) 511 elif size != None: 512 raise RelaxIntListIntError(name, arg, size) 513 else: 514 raise RelaxIntListIntError(name, arg) 515 516 # Success. 517 return True
518 519
520 -def is_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, list_of_lists=False, raise_error=True):
521 """Test if the argument is a list. 522 523 @param arg: The argument. 524 @type arg: anything 525 @keyword name: The plain English name of the argument. 526 @type name: str 527 @keyword size: The number of elements required. 528 @type size: None or int 529 @keyword can_be_none: A flag specifying if the argument can be none. 530 @type can_be_none: bool 531 @keyword can_be_empty: A flag which if True allows the list to be empty. 532 @type can_be_empty: bool 533 @keyword list_of_lists: A flag which if True allows the argument to be a list of lists of strings. 534 @type list_of_lists: bool 535 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 536 @type raise_error: bool 537 @raise RelaxListStrError: If not a list of strings (and the raise_error flag is set). 538 @raise RelaxNoneListStrError: If not a list of strings or None (and the raise_error flag is set). 539 @return: The answer to the question (if raise_error is not set). 540 @rtype: bool 541 """ 542 543 # Init. 544 fail = False 545 546 # An argument of None is allowed. 547 if can_be_none and arg == None: 548 return True 549 550 # Fail if not a list. 551 if not isinstance(arg, list): 552 fail = True 553 554 # Other checks. 555 else: 556 # Fail size is wrong. 557 if size != None and len(arg) != size: 558 fail = True 559 560 # Fail if empty. 561 if not can_be_empty and arg == []: 562 fail = True 563 564 # Fail. 565 if fail: 566 if not raise_error: 567 return False 568 if can_be_none and size != None: 569 raise RelaxNoneListError(name, arg, size) 570 elif can_be_none: 571 raise RelaxNoneListError(name, arg) 572 elif size != None: 573 raise RelaxListError(name, arg, size) 574 else: 575 raise RelaxListError(name, arg) 576 577 # Success. 578 return True
579 580
581 -def is_list_val_or_list_of_list_val(arg, name=None, size=None, can_be_none=False, can_be_empty=False, list_of_lists=False, raise_error=True):
582 """Test if the argument is a list of values or list of list with values. 583 584 @param arg: The argument. 585 @type arg: anything 586 @keyword name: The plain English name of the argument. 587 @type name: str 588 @keyword size: The number of elements required. 589 @type size: None or int 590 @keyword can_be_none: A flag specifying if the argument can be none. 591 @type can_be_none: bool 592 @keyword can_be_empty: A flag which if True allows the list to be empty. 593 @type can_be_empty: bool 594 @keyword list_of_lists: A flag which if True allows the argument to be a list of lists of values. 595 @type list_of_lists: bool 596 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 597 @type raise_error: bool 598 @raise RelaxNumStrListNumStrError: If not a float, a string, or a list of floats or strings (and the raise_error flag is set). 599 @raise RelaxNoneNumStrListNumStrError: If not a float, a string, a list of floats or strings, or None (and the raise_error flag is set). 600 @return: The answer to the question (if raise_error is not set). 601 @rtype: bool 602 """ 603 604 # FIXME: This still needs to be implemented as a check for is_list_val_or_list_of_list_val: 605 606 # Init. 607 fail = False 608 609 # An argument of None is allowed. 610 if can_be_none and arg == None: 611 return True 612 613 # A list. 614 if not isinstance(arg, list): 615 fail = True 616 617 # Success. 618 return True
619 620
621 -def is_none(arg, name, raise_error=True):
622 """Test if the argument is None. 623 624 @param arg: The argument. 625 @type arg: anything 626 @keyword name: The plain English name of the argument. 627 @type name: str 628 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 629 @type raise_error: bool 630 @raise RelaxNoneError: If not None (and the raise_error flag is set). 631 @return: The answer to the question (if raise_error is not set). 632 @rtype: bool 633 """ 634 635 # Check for None. 636 if arg == None: 637 return True 638 639 # Fail. 640 if not raise_error: 641 return False 642 else: 643 raise RelaxNoneError(name)
644 645
646 -def is_num(arg, name=None, can_be_none=False, raise_error=True):
647 """Test if the argument is a number. 648 649 @param arg: The argument. 650 @type arg: anything 651 @keyword name: The plain English name of the argument. 652 @type name: str 653 @keyword can_be_none: A flag specifying if the argument can be none. 654 @type can_be_none: bool 655 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 656 @type raise_error: bool 657 @raise RelaxNumError: If not a number (and the raise_error flag is set). 658 @raise RelaxNoneNumError: If not a number or not None (and the raise_error flag is set). 659 @return: The answer to the question (if raise_error is not set). 660 @rtype: bool 661 """ 662 663 # An argument of None is allowed. 664 if can_be_none and arg == None: 665 return True 666 667 # Check for floats and integers (avoiding Booleans). 668 if (lib.check_types.is_float(arg) or isinstance(arg, int)) and not isinstance(arg, bool): 669 return True 670 671 # Fail. 672 if not raise_error: 673 return False 674 if not can_be_none: 675 raise RelaxNumError(name, arg) 676 else: 677 raise RelaxNoneNumError(name, arg)
678 679
680 -def is_num_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
681 """Test if the argument is a list of numbers. 682 683 @param arg: The argument. 684 @type arg: anything 685 @keyword name: The plain English name of the argument. 686 @type name: str 687 @keyword size: The number of elements required. 688 @type size: None or int 689 @keyword can_be_none: A flag specifying if the argument can be none. 690 @type can_be_none: bool 691 @keyword can_be_empty: A flag which if True allows the list to be empty. 692 @type can_be_empty: bool 693 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 694 @type raise_error: bool 695 @raise RelaxListError: If not a list (and the raise_error flag is set). 696 @raise RelaxListNumError: If not a list of numbers (and the raise_error flag is set). 697 @return: The answer to the question (if raise_error is not set). 698 @rtype: bool 699 """ 700 701 # Init. 702 fail = False 703 704 # An argument of None is allowed. 705 if can_be_none and arg == None: 706 return True 707 708 # Fail if not a list. 709 if not isinstance(arg, list) and not isinstance(arg, ndarray): 710 fail = True 711 712 # Other checks. 713 else: 714 # Fail size is wrong. 715 if size != None and len(arg) != size: 716 fail = True 717 718 # Fail if empty. 719 if not can_be_empty and arg == []: 720 fail = True 721 722 # Fail if not numbers. 723 for i in range(len(arg)): 724 if (not lib.check_types.is_float(arg[i]) and not isinstance(arg[i], int)) or isinstance(arg, bool): 725 fail = True 726 727 # Fail. 728 if fail: 729 if not raise_error: 730 return False 731 if can_be_none and size != None: 732 raise RelaxNoneListNumError(name, arg, size) 733 elif can_be_none: 734 raise RelaxNoneListNumError(name, arg) 735 elif size != None: 736 raise RelaxListNumError(name, arg, size) 737 else: 738 raise RelaxListNumError(name, arg) 739 740 # Success. 741 return True
742 743
744 -def is_num_or_num_tuple(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
745 """Test if the argument is a tuple of numbers. 746 747 @param arg: The argument. 748 @type arg: anything 749 @keyword name: The plain English name of the argument. 750 @type name: str 751 @keyword size: The number of elements required. 752 @type size: None or int 753 @keyword can_be_none: A flag specifying if the argument can be none. 754 @type can_be_none: bool 755 @keyword can_be_empty: A flag which if True allows the list to be empty. 756 @type can_be_empty: bool 757 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 758 @type raise_error: bool 759 @raise RelaxTupleError: If not a tuple (and the raise_error flag is set). 760 @raise RelaxTupleNumError: If not a tuple of numbers (and the raise_error flag is set). 761 @return: The answer to the question (if raise_error is not set). 762 @rtype: bool 763 """ 764 765 # Init. 766 fail = False 767 if size != None and not isinstance(size, list): 768 size = [size] 769 770 # An argument of None is allowed. 771 if can_be_none and arg == None: 772 return True 773 774 # A number. 775 if not isinstance(arg, tuple): 776 if not is_num(arg, raise_error=False): 777 fail = True 778 779 # Other checks. 780 else: 781 # Fail size is wrong. 782 if size != None and len(arg) not in size: 783 fail = True 784 785 # Fail if empty. 786 if not can_be_empty and not len(arg): 787 fail = True 788 789 # Fail if not numbers. 790 for i in range(len(arg)): 791 if not is_num(arg[i], raise_error=False): 792 fail = True 793 794 # Fail. 795 if fail: 796 if not raise_error: 797 return False 798 if can_be_none and size != None: 799 raise RelaxNoneNumTupleNumError(name, arg, size) 800 elif can_be_none: 801 raise RelaxNoneNumTupleNumError(name, arg) 802 elif size != None: 803 raise RelaxNumTupleNumError(name, arg, size) 804 else: 805 raise RelaxNumTupleNumError(name, arg) 806 807 # Success. 808 return True
809 810
811 -def is_num_tuple(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
812 """Test if the argument is a tuple of numbers. 813 814 @param arg: The argument. 815 @type arg: anything 816 @keyword name: The plain English name of the argument. 817 @type name: str 818 @keyword size: The number of elements required. 819 @type size: None or int 820 @keyword can_be_none: A flag specifying if the argument can be none. 821 @type can_be_none: bool 822 @keyword can_be_empty: A flag which if True allows the list to be empty. 823 @type can_be_empty: bool 824 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 825 @type raise_error: bool 826 @raise RelaxTupleError: If not a tuple (and the raise_error flag is set). 827 @raise RelaxTupleNumError: If not a tuple of numbers (and the raise_error flag is set). 828 @return: The answer to the question (if raise_error is not set). 829 @rtype: bool 830 """ 831 832 # Init. 833 fail = False 834 835 # An argument of None is allowed. 836 if can_be_none and arg == None: 837 return True 838 839 # Fail if not a tuple. 840 if not isinstance(arg, tuple): 841 fail = True 842 843 # Other checks. 844 else: 845 # Fail size is wrong. 846 if size != None and len(arg) != size: 847 fail = True 848 849 # Fail if empty. 850 if not can_be_empty and arg == []: 851 fail = True 852 853 # Fail if not numbers. 854 for i in range(len(arg)): 855 if (not lib.check_types.is_float(arg[i]) and not isinstance(arg[i], int)) or isinstance(arg, bool): 856 fail = True 857 858 # Fail. 859 if fail: 860 if not raise_error: 861 return False 862 if can_be_none and size != None: 863 raise RelaxNoneTupleNumError(name, arg, size) 864 elif can_be_none: 865 raise RelaxNoneTupleNumError(name, arg) 866 elif size != None: 867 raise RelaxTupleNumError(name, arg, size) 868 else: 869 raise RelaxTupleNumError(name, arg) 870 871 # Success. 872 return True
873 874
875 -def is_str(arg, name=None, can_be_none=False, raise_error=True):
876 """Test if the argument is a string. 877 878 @param arg: The argument. 879 @type arg: anything 880 @keyword name: The plain English name of the argument. 881 @type name: str 882 @keyword can_be_none: A flag specifying if the argument can be none. 883 @type can_be_none: bool 884 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 885 @type raise_error: bool 886 @raise RelaxStrError: If not an integer (and the raise_error flag is set). 887 @raise RelaxNoneStrError: If not an integer or not None (and the raise_error flag is set). 888 @return: The answer to the question (if raise_error is not set). 889 @rtype: bool 890 """ 891 892 # An argument of None is allowed. 893 if can_be_none and arg == None: 894 return True 895 896 # Check for a string. 897 if isinstance(arg, str): 898 return True 899 900 # Fail. 901 if not raise_error: 902 return False 903 if not can_be_none: 904 raise RelaxStrError(name, arg) 905 else: 906 raise RelaxNoneStrError(name, arg)
907 908
909 -def is_str_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, list_of_lists=False, raise_error=True):
910 """Test if the argument is a list of strings. 911 912 @param arg: The argument. 913 @type arg: anything 914 @keyword name: The plain English name of the argument. 915 @type name: str 916 @keyword size: The number of elements required. 917 @type size: None or int 918 @keyword can_be_none: A flag specifying if the argument can be none. 919 @type can_be_none: bool 920 @keyword can_be_empty: A flag which if True allows the list to be empty. 921 @type can_be_empty: bool 922 @keyword list_of_lists: A flag which if True allows the argument to be a list of lists 923 of strings. 924 @type list_of_lists: bool 925 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 926 @type raise_error: bool 927 @raise RelaxListStrError: If not a list of strings (and the raise_error flag is set). 928 @raise RelaxNoneListStrError: If not a list of strings or None (and the raise_error flag is set). 929 @return: The answer to the question (if raise_error is not set). 930 @rtype: bool 931 """ 932 933 # Init. 934 fail = False 935 936 # An argument of None is allowed. 937 if can_be_none and arg == None: 938 return True 939 940 # Fail if not a list. 941 if not isinstance(arg, list): 942 fail = True 943 944 # Other checks. 945 else: 946 # Fail size is wrong. 947 if size != None and len(arg) != size: 948 fail = True 949 950 # Fail if empty. 951 if not can_be_empty and arg == []: 952 fail = True 953 954 # Fail if not strings. 955 for i in range(len(arg)): 956 # List of lists. 957 if list_of_lists and isinstance(arg[i], list): 958 for j in range(len(arg[i])): 959 if not isinstance(arg[i][j], str): 960 fail = True 961 962 963 # Simple list. 964 else: 965 if not isinstance(arg[i], str): 966 fail = True 967 968 # Fail. 969 if fail: 970 if not raise_error: 971 return False 972 if can_be_none and size != None: 973 raise RelaxNoneListStrError(name, arg, size) 974 elif can_be_none: 975 raise RelaxNoneListStrError(name, arg) 976 elif size != None: 977 raise RelaxListStrError(name, arg, size) 978 else: 979 raise RelaxListStrError(name, arg) 980 981 # Success. 982 return True
983 984
985 -def is_str_or_inst(arg, name=None, can_be_none=False, raise_error=True):
986 """Test if the argument is a string. 987 988 @param arg: The argument. 989 @type arg: anything 990 @keyword name: The plain English name of the argument. 991 @type name: str 992 @keyword can_be_none: A flag specifying if the argument can be none. 993 @type can_be_none: bool 994 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 995 @type raise_error: bool 996 @raise RelaxStrError: If not an integer (and the raise_error flag is set). 997 @raise RelaxNoneStrError: If not an integer or not None (and the raise_error flag is set). 998 @return: The answer to the question (if raise_error is not set). 999 @rtype: bool 1000 """ 1001 1002 # An argument of None is allowed. 1003 if can_be_none and arg == None: 1004 return True 1005 1006 # Check for a string. 1007 if isinstance(arg, str) or lib.check_types.is_filetype(arg) or isinstance(arg, DummyFileObject): 1008 return True 1009 1010 # Fail. 1011 if not raise_error: 1012 return False 1013 if not can_be_none: 1014 raise RelaxStrFileError(name, arg) 1015 else: 1016 raise RelaxNoneStrFileError(name, arg)
1017 1018
1019 -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):
1020 """Test if the argument is a number, a string, a list of numbers, or a list of strings. 1021 1022 @param arg: The argument. 1023 @type arg: anything 1024 @keyword name: The plain English name of the argument. 1025 @type name: str 1026 @keyword size: The number of elements required. 1027 @type size: None or int 1028 @keyword can_be_none: A flag specifying if the argument can be none. 1029 @type can_be_none: bool 1030 @keyword can_be_empty: A flag which if True allows the list to be empty. 1031 @type can_be_empty: bool 1032 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 1033 @type raise_error: bool 1034 @raise RelaxNumStrListNumStrError: If not a float, a string, or a list of floats or strings (and the raise_error flag is set). 1035 @raise RelaxNoneNumStrListNumStrError: If not a float, a string, a list of floats or strings, or None (and the raise_error flag is set). 1036 @return: The answer to the question (if raise_error is not set). 1037 @rtype: bool 1038 """ 1039 1040 # Init. 1041 fail = False 1042 1043 # An argument of None is allowed. 1044 if can_be_none and arg == None: 1045 return True 1046 1047 # A number or a string. 1048 if not isinstance(arg, list): 1049 # Check if it is a string or number. 1050 if not (is_str(arg, raise_error=False) or is_num(arg, raise_error=False)): 1051 fail = True 1052 1053 # A list. 1054 else: 1055 # Fail size is wrong. 1056 if size != None and len(arg) != size: 1057 fail = True 1058 1059 # Fail if empty. 1060 if not can_be_empty and arg == []: 1061 fail = True 1062 1063 # Check the arguments. 1064 for i in range(len(arg)): 1065 if not (is_str(arg[i], raise_error=False) or is_num(arg[i], raise_error=False)): 1066 fail = True 1067 1068 # Fail. 1069 if fail: 1070 if not raise_error: 1071 return False 1072 if can_be_none and size != None: 1073 raise RelaxNoneNumStrListNumStrError(name, arg, size) 1074 elif can_be_none: 1075 raise RelaxNoneNumStrListNumStrError(name, arg) 1076 elif size != None: 1077 raise RelaxNumStrListNumStrError(name, arg, size) 1078 else: 1079 raise RelaxNumStrListNumStrError(name, arg) 1080 1081 # Success. 1082 return True
1083 1084
1085 -def is_str_or_num_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
1086 """Test if the argument is a string or a list of numbers. 1087 1088 @param arg: The argument. 1089 @type arg: anything 1090 @keyword name: The plain English name of the argument. 1091 @type name: str 1092 @keyword size: The number of elements required. 1093 @type size: None or int 1094 @keyword can_be_none: A flag specifying if the argument can be none. 1095 @type can_be_none: bool 1096 @keyword can_be_empty: A flag which if True allows the list to be empty. 1097 @type can_be_empty: bool 1098 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 1099 @type raise_error: bool 1100 @raise RelaxStrListNumError: If not a string or a list of strings (and the raise_error flag is set). 1101 @raise RelaxNoneStrListNumError: If not a string, a list of strings, or None (and the raise_error flag is set). 1102 @return: The answer to the question (if raise_error is not set). 1103 @rtype: bool 1104 """ 1105 1106 # Init. 1107 fail = False 1108 1109 # An argument of None is allowed. 1110 if can_be_none and arg == None: 1111 return True 1112 1113 # A string. 1114 if not isinstance(arg, list): 1115 if not is_str(arg, raise_error=False): 1116 fail = True 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 if not is_num(arg[i], raise_error=False): 1131 fail = True 1132 1133 # Fail. 1134 if fail: 1135 if not raise_error: 1136 return False 1137 if can_be_none and size != None: 1138 raise RelaxNoneStrListNumError(name, arg, size) 1139 elif can_be_none: 1140 raise RelaxNoneStrListNumError(name, arg) 1141 elif size != None: 1142 raise RelaxStrListNumError(name, arg, size) 1143 else: 1144 raise RelaxStrListNumError(name, arg) 1145 1146 # Success. 1147 return True
1148 1149
1150 -def is_str_or_str_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
1151 """Test if the argument is a string or a list of strings. 1152 1153 @param arg: The argument. 1154 @type arg: anything 1155 @keyword name: The plain English name of the argument. 1156 @type name: str 1157 @keyword size: The number of elements required. 1158 @type size: None or int 1159 @keyword can_be_none: A flag specifying if the argument can be none. 1160 @type can_be_none: bool 1161 @keyword can_be_empty: A flag which if True allows the list to be empty. 1162 @type can_be_empty: bool 1163 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 1164 @type raise_error: bool 1165 @raise RelaxStrListStrError: If not a string or a list of strings (and the raise_error flag is set). 1166 @raise RelaxNoneStrListStrError: If not a string, a list of strings, or None (and the raise_error flag is set). 1167 @return: The answer to the question (if raise_error is not set). 1168 @rtype: bool 1169 """ 1170 1171 # Init. 1172 fail = False 1173 1174 # An argument of None is allowed. 1175 if can_be_none and arg == None: 1176 return True 1177 1178 # A string. 1179 if not isinstance(arg, list): 1180 if not is_str(arg, raise_error=False): 1181 fail = True 1182 1183 # A list. 1184 else: 1185 # Fail size is wrong. 1186 if size != None and len(arg) != size: 1187 fail = True 1188 1189 # Fail if empty. 1190 if not can_be_empty and arg == []: 1191 fail = True 1192 1193 # Check the arguments. 1194 for i in range(len(arg)): 1195 if not is_str(arg[i], raise_error=False): 1196 fail = True 1197 1198 # Fail. 1199 if fail: 1200 if not raise_error: 1201 return False 1202 if can_be_none and size != None: 1203 raise RelaxNoneStrListStrError(name, arg, size) 1204 elif can_be_none: 1205 raise RelaxNoneStrListStrError(name, arg) 1206 elif size != None: 1207 raise RelaxStrListStrError(name, arg, size) 1208 else: 1209 raise RelaxStrListStrError(name, arg) 1210 1211 # Success. 1212 return True
1213 1214
1215 -def is_tuple(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
1216 """Test if the argument is a tuple. 1217 1218 @param arg: The argument. 1219 @type arg: anything 1220 @keyword name: The plain English name of the argument. 1221 @type name: str 1222 @keyword size: The number of elements required. 1223 @type size: None or int 1224 @keyword can_be_none: A flag specifying if the argument can be none. 1225 @type can_be_none: bool 1226 @keyword can_be_empty: A flag which if True allows the list to be empty. 1227 @type can_be_empty: bool 1228 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 1229 @type raise_error: bool 1230 @raise RelaxTupleError: If not a tuple (and the raise_error flag is set). 1231 @raise RelaxNoneTupleError: If not a tuple or not None (and the raise_error flag is set). 1232 @return: The answer to the question (if raise_error is not set). 1233 @rtype: bool 1234 """ 1235 1236 # Init. 1237 fail = False 1238 1239 # An argument of None is allowed. 1240 if can_be_none and arg == None: 1241 return True 1242 1243 # Fail if not a tuple. 1244 if not isinstance(arg, tuple): 1245 fail = True 1246 1247 # Other checks. 1248 else: 1249 # Fail size is wrong. 1250 if size != None and len(arg) != size: 1251 fail = True 1252 1253 # Fail if empty. 1254 if not can_be_empty and arg == []: 1255 fail = True 1256 1257 # Fail. 1258 if fail: 1259 if not raise_error: 1260 return False 1261 if can_be_none and size != None: 1262 raise RelaxNoneTupleError(name, arg, size) 1263 elif can_be_none: 1264 raise RelaxNoneTupleError(name, arg) 1265 elif size != None: 1266 raise RelaxTupleError(name, arg, size) 1267 else: 1268 raise RelaxTupleError(name, arg) 1269 1270 # Success. 1271 return True
1272 1273
1274 -def is_val_or_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
1275 """Test if the argument is a value or a list of values. 1276 1277 @param arg: The argument. 1278 @type arg: anything 1279 @keyword name: The plain English name of the argument. 1280 @type name: str 1281 @keyword size: The number of elements required. 1282 @type size: None or int 1283 @keyword can_be_none: A flag specifying if the argument can be none. 1284 @type can_be_none: bool 1285 @keyword can_be_empty: A flag which if True allows the list to be empty. 1286 @type can_be_empty: bool 1287 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised. 1288 @type raise_error: bool 1289 @raise RelaxNumStrListNumStrError: If not a float, a string, or a list of floats or strings (and the raise_error flag is set). 1290 @raise RelaxNoneNumStrListNumStrError: If not a float, a string, a list of floats or strings, or None (and the raise_error flag is set). 1291 @return: The answer to the question (if raise_error is not set). 1292 @rtype: bool 1293 """ 1294 1295 # Init. 1296 fail = False 1297 1298 # An argument of None is allowed. 1299 if can_be_none and arg == None: 1300 return True 1301 1302 # A value. 1303 if not isinstance(arg, list): 1304 # Check for all types of value. 1305 if not (is_bool(arg, raise_error=False) or is_str(arg, raise_error=False) or is_num(arg, raise_error=False)): 1306 fail = True 1307 1308 # A list. 1309 else: 1310 # Fail size is wrong. 1311 if size != None and len(arg) != size: 1312 fail = True 1313 1314 # Fail if empty. 1315 if not can_be_empty and arg == []: 1316 fail = True 1317 1318 # Check the arguments. 1319 for i in range(len(arg)): 1320 # Check for all types of value. 1321 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)): 1322 fail = True 1323 1324 # Fail. 1325 if fail: 1326 if not raise_error: 1327 return False 1328 if can_be_none and size != None: 1329 raise RelaxNoneValListValError(name, arg, size) 1330 elif can_be_none: 1331 raise RelaxNoneValListValError(name, arg) 1332 elif size != None: 1333 raise RelaxValListValError(name, arg, size) 1334 else: 1335 raise RelaxValListValError(name, arg) 1336 1337 # Success. 1338 return True
1339