1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Argument checking functions for the relax user functions."""
25
26
27 from numpy import ndarray
28
29
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
51 if isinstance(arg, bool):
52 return True
53
54
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
79 if can_be_none and arg == None:
80 return True
81
82
83 if lib.check_types.is_float(arg):
84 return True
85
86
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
115 fail = False
116
117
118 if can_be_none and arg == None:
119 return True
120
121
122 if not isinstance(arg, list) and not isinstance(arg, ndarray):
123 fail = True
124
125
126 elif size != None and len(arg) != size:
127 fail = True
128
129
130 else:
131 for i in range(len(arg)):
132
133 if not lib.check_types.is_float(arg[i]):
134 fail = True
135
136
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
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
175 fail = False
176
177
178 if can_be_none and arg == None:
179 return True
180
181
182 if not isinstance(arg, list) and not isinstance(arg, ndarray):
183 fail = True
184
185
186 elif not len(arg):
187 fail = True
188
189
190 elif not isinstance(arg[0], list) and not isinstance(arg[0], ndarray):
191 fail = True
192
193
194 elif dim != None and len(arg) != dim[0]:
195 fail = True
196
197
198 else:
199 for i in range(len(arg)):
200
201 if arg[i] == None:
202 if not none_elements:
203 fail = True
204 continue
205
206
207 if not (isinstance(arg[i], list) or isinstance(arg[i], ndarray)):
208 fail = True
209
210
211 elif dim != None and len(arg[i]) != dim[1]:
212 fail = True
213
214
215 for j in range(len(arg[i])):
216 if not lib.check_types.is_float(arg[i][j]):
217 fail = True
218
219
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
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
256 fail = False
257 if isinstance(dim, int):
258 dim = [dim]
259
260
261 if can_be_none and arg == None:
262 return True
263
264
265 if not isinstance(arg, list) and not isinstance(arg, ndarray):
266 fail = True
267
268
269 elif not len(arg):
270 fail = True
271
272
273 elif dim != None and len(arg) != dim[0]:
274 fail = True
275
276
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
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
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
289 else:
290
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
297 for i in range(len(elements)):
298 if not lib.check_types.is_float(elements[i]):
299 fail = True
300
301
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
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
336 if can_be_none and arg == None:
337 return True
338
339
340 if isinstance(arg, FunctionType) or isinstance(arg, MethodType):
341 return True
342
343
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
370 if can_be_none and arg == None:
371 return True
372
373
374 if isinstance(arg, int) and not isinstance(arg, bool):
375 return True
376
377
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
412 fail = False
413
414
415 if can_be_none and arg == None:
416 return True
417
418
419 if not isinstance(arg, list):
420 fail = True
421
422
423 else:
424
425 if size != None and len(arg) != size:
426 fail = True
427
428
429 if not can_be_empty and arg == []:
430 fail = True
431
432
433 for i in range(len(arg)):
434
435 if arg[i] == None and none_elements:
436 continue
437
438
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
445 else:
446 if not isinstance(arg[i], int):
447 fail = True
448
449
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
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
490 fail = False
491
492
493 if can_be_none and arg == None:
494 return True
495
496
497 if not isinstance(arg, list):
498 if not is_int(arg, raise_error=False):
499 fail = True
500
501
502 else:
503
504 if size != None and len(arg) != size:
505 fail = True
506
507
508 if not can_be_empty and arg == []:
509 fail = True
510
511
512 for i in range(len(arg)):
513
514 if arg[i] == None and none_elements:
515 continue
516
517
518 if not is_int(arg[i], raise_error=False):
519 fail = True
520
521
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
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
562 fail = False
563
564
565 if can_be_none and arg == None:
566 return True
567
568
569 if not isinstance(arg, list):
570 fail = True
571
572
573 else:
574
575 if size != None and len(arg) != size:
576 fail = True
577
578
579 if not can_be_empty and arg == []:
580 fail = True
581
582
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
596 return True
597
598
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
623
624
625 fail = False
626
627
628 if can_be_none and arg == None:
629 return True
630
631
632 if not isinstance(arg, list):
633 fail = True
634
635
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
654 if arg == None:
655 return True
656
657
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
682 if can_be_none and arg == None:
683 return True
684
685
686 if (lib.check_types.is_float(arg) or isinstance(arg, int)) and not isinstance(arg, bool):
687 return True
688
689
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
720 fail = False
721
722
723 if can_be_none and arg == None:
724 return True
725
726
727 if not isinstance(arg, list) and not isinstance(arg, ndarray):
728 fail = True
729
730
731 else:
732
733 if size != None and len(arg) != size:
734 fail = True
735
736
737 if not can_be_empty and arg == []:
738 fail = True
739
740
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
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
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
784 fail = False
785 if size != None and not isinstance(size, list):
786 size = [size]
787
788
789 if can_be_none and arg == None:
790 return True
791
792
793 if not isinstance(arg, tuple):
794 if not is_num(arg, raise_error=False):
795 fail = True
796
797
798 else:
799
800 if size != None and len(arg) not in size:
801 fail = True
802
803
804 if not can_be_empty and not len(arg):
805 fail = True
806
807
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
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
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
851 fail = False
852
853
854 if can_be_none and arg == None:
855 return True
856
857
858 if not isinstance(arg, tuple):
859 fail = True
860
861
862 else:
863
864 if size != None and len(arg) != size:
865 fail = True
866
867
868 if not can_be_empty and arg == []:
869 fail = True
870
871
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
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
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
911 if can_be_none and arg == None:
912 return True
913
914
915 if isinstance(arg, str):
916 return True
917
918
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
951 fail = False
952
953
954 if can_be_none and arg == None:
955 return True
956
957
958 if not isinstance(arg, list):
959 fail = True
960
961
962 else:
963
964 if size != None and len(arg) != size:
965 fail = True
966
967
968 if not can_be_empty and arg == []:
969 fail = True
970
971
972 for i in range(len(arg)):
973
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
980 else:
981 if not isinstance(arg[i], str):
982 fail = True
983
984
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
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
1019 if can_be_none and arg == None:
1020 return True
1021
1022
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
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
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
1057 fail = False
1058
1059
1060 if can_be_none and arg == None:
1061 return True
1062
1063
1064 if not isinstance(arg, list):
1065
1066 if not (is_str(arg, raise_error=False) or is_num(arg, raise_error=False)):
1067 fail = True
1068
1069
1070 else:
1071
1072 if size != None and len(arg) != size:
1073 fail = True
1074
1075
1076 if not can_be_empty and arg == []:
1077 fail = True
1078
1079
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
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
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
1123 fail = False
1124
1125
1126 if can_be_none and arg == None:
1127 return True
1128
1129
1130 if not isinstance(arg, list):
1131 if not is_str(arg, raise_error=False):
1132 fail = True
1133
1134
1135 else:
1136
1137 if size != None and len(arg) != size:
1138 fail = True
1139
1140
1141 if not can_be_empty and arg == []:
1142 fail = True
1143
1144
1145 for i in range(len(arg)):
1146 if not is_num(arg[i], raise_error=False):
1147 fail = True
1148
1149
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
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
1188 fail = False
1189
1190
1191 if can_be_none and arg == None:
1192 return True
1193
1194
1195 if not isinstance(arg, list):
1196 if not is_str(arg, raise_error=False):
1197 fail = True
1198
1199
1200 else:
1201
1202 if size != None and len(arg) != size:
1203 fail = True
1204
1205
1206 if not can_be_empty and arg == []:
1207 fail = True
1208
1209
1210 for i in range(len(arg)):
1211 if not is_str(arg[i], raise_error=False):
1212 fail = True
1213
1214
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
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
1253 fail = False
1254
1255
1256 if can_be_none and arg == None:
1257 return True
1258
1259
1260 if not isinstance(arg, tuple):
1261 fail = True
1262
1263
1264 else:
1265
1266 if size != None and len(arg) != size:
1267 fail = True
1268
1269
1270 if not can_be_empty and arg == []:
1271 fail = True
1272
1273
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
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
1312 fail = False
1313
1314
1315 if can_be_none and arg == None:
1316 return True
1317
1318
1319 if not isinstance(arg, list):
1320
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
1325 else:
1326
1327 if size != None and len(arg) != size:
1328 fail = True
1329
1330
1331 if not can_be_empty and arg == []:
1332 fail = True
1333
1334
1335 for i in range(len(arg)):
1336
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
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
1354 return True
1355