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