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

Source Code for Module lib.arg_check

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