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, RelaxNumError, RelaxNumStrListNumStrError, RelaxNumTupleNumError, RelaxStrError, RelaxStrFileError, RelaxStrListNumError, RelaxStrListStrError, RelaxTupleError, RelaxTupleNumError
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 dim != None and len(arg) != dim[0]:
219 fail = True
220
221
222 else:
223 for i in range(len(arg)):
224
225 if not isinstance(arg[i], list) and not isinstance(arg, ndarray):
226 fail = True
227
228
229 elif len(arg[i]) != dim[1]:
230 fail = True
231
232
233 for j in range(len(arg[i])):
234 if not check_float(arg[i][j]):
235 fail = True
236
237
238 if fail:
239 if not raise_error:
240 return False
241 if can_be_none and dim != None:
242 raise RelaxNoneMatrixFloatError(name, arg, dim)
243 elif can_be_none:
244 raise RelaxNoneMatrixFloatError(name, arg)
245 elif dim != None:
246 raise RelaxMatrixFloatError(name, arg, dim)
247 else:
248 raise RelaxMatrixFloatError(name, arg)
249
250
251 return True
252
253
254 -def is_func(arg, name=None, can_be_none=False, raise_error=True):
255 """Test if the argument is a function.
256
257 @param arg: The argument.
258 @type arg: anything
259 @keyword name: The plain English name of the argument.
260 @type name: str
261 @keyword can_be_none: A flag specifying if the argument can be none.
262 @type can_be_none: bool
263 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
264 @type raise_error: bool
265 @raise RelaxFunctionError: If not a function (and the raise_error flag is set).
266 @raise RelaxNoneFunctionError: If not a function or not None (and the raise_error flag is set).
267 @return: The answer to the question (if raise_error is not set).
268 @rtype: bool
269 """
270
271
272 if can_be_none and arg == None:
273 return True
274
275
276 if isinstance(arg, FunctionType) or isinstance(arg, MethodType):
277 return True
278
279
280 if not raise_error:
281 return False
282 if not can_be_none:
283 raise RelaxFunctionError(name, arg)
284 else:
285 raise RelaxNoneFunctionError(name, arg)
286
287
288 -def is_int(arg, name=None, can_be_none=False, raise_error=True):
289 """Test if the argument is an integer.
290
291 @param arg: The argument.
292 @type arg: anything
293 @keyword name: The plain English name of the argument.
294 @type name: str
295 @keyword can_be_none: A flag specifying if the argument can be none.
296 @type can_be_none: bool
297 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
298 @type raise_error: bool
299 @raise RelaxIntError: If not an integer (and the raise_error flag is set).
300 @raise RelaxNoneIntError: If not an integer or not None (and the raise_error flag is set).
301 @return: The answer to the question (if raise_error is not set).
302 @rtype: bool
303 """
304
305
306 if can_be_none and arg == None:
307 return True
308
309
310 if isinstance(arg, int) and not isinstance(arg, bool):
311 return True
312
313
314 if not raise_error:
315 return False
316 if not can_be_none:
317 raise RelaxIntError(name, arg)
318 else:
319 raise RelaxNoneIntError(name, arg)
320
321
322 -def is_int_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, none_elements=False, raise_error=True):
323 """Test if the argument is a list of integers.
324
325 @param arg: The argument.
326 @type arg: anything
327 @keyword name: The plain English name of the argument.
328 @type name: str
329 @keyword size: The number of elements required.
330 @type size: None or int
331 @keyword can_be_none: A flag specifying if the argument can be none.
332 @type can_be_none: bool
333 @keyword can_be_empty: A flag which if True allows the list to be empty.
334 @type can_be_empty: bool
335 @keyword none_elements: A flag which if True allows the list to contain None.
336 @type none_elements: bool
337 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
338 @type raise_error: bool
339 @raise RelaxIntListIntError: If not an integer or a list of integers (and the raise_error flag is set).
340 @raise RelaxNoneIntListIntError: If not an integer, a list of integers, or None (and the raise_error flag is set).
341 @return: The answer to the question (if raise_error is not set).
342 @rtype: bool
343 """
344
345
346 fail = False
347
348
349 if can_be_none and arg == None:
350 return True
351
352
353 if not isinstance(arg, list):
354 fail = True
355
356
357 else:
358
359 if size != None and len(arg) != size:
360 fail = True
361
362
363 if not can_be_empty and arg == []:
364 fail = True
365
366
367 for i in range(len(arg)):
368
369 if arg[i] == None and none_elements:
370 continue
371
372
373 if not isinstance(arg[i], int):
374 fail = True
375
376
377 if fail:
378 if not raise_error:
379 return False
380 if can_be_none and size != None:
381 raise RelaxNoneListIntError(name, arg, size)
382 elif can_be_none:
383 raise RelaxNoneListIntError(name, arg)
384 elif size != None:
385 raise RelaxListIntError(name, arg, size)
386 else:
387 raise RelaxListIntError(name, arg)
388
389
390 return True
391
392
393 -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):
394 """Test if the argument is an integer or a list of integers.
395
396 @param arg: The argument.
397 @type arg: anything
398 @keyword name: The plain English name of the argument.
399 @type name: str
400 @keyword size: The number of elements required.
401 @type size: None or int
402 @keyword can_be_none: A flag specifying if the argument can be none.
403 @type can_be_none: bool
404 @keyword can_be_empty: A flag which if True allows the list to be empty.
405 @type can_be_empty: bool
406 @keyword none_elements: A flag which if True allows the list to contain None.
407 @type none_elements: bool
408 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
409 @type raise_error: bool
410 @raise RelaxIntListIntError: If not an integer or a list of integers (and the raise_error flag is set).
411 @raise RelaxNoneIntListIntError: If not an integer, a list of integers, or None (and the raise_error flag is set).
412 @return: The answer to the question (if raise_error is not set).
413 @rtype: bool
414 """
415
416
417 fail = False
418
419
420 if can_be_none and arg == None:
421 return True
422
423
424 if not isinstance(arg, list):
425
426 try:
427 is_int(arg, name)
428 except:
429 fail = True
430
431
432 else:
433
434 if size != None and len(arg) != size:
435 fail = True
436
437
438 if not can_be_empty and arg == []:
439 fail = True
440
441
442 for i in range(len(arg)):
443
444 if arg[i] == None and none_elements:
445 continue
446
447
448 try:
449 is_int(arg[i], name)
450 except:
451 fail = True
452
453
454 if fail:
455 if not raise_error:
456 return False
457 if can_be_none and size != None:
458 raise RelaxNoneIntListIntError(name, arg, size)
459 elif can_be_none:
460 raise RelaxNoneIntListIntError(name, arg)
461 elif size != None:
462 raise RelaxIntListIntError(name, arg, size)
463 else:
464 raise RelaxIntListIntError(name, arg)
465
466
467 return True
468
469
470 -def is_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, list_of_lists=False, raise_error=True):
471 """Test if the argument is a list.
472
473 @param arg: The argument.
474 @type arg: anything
475 @keyword name: The plain English name of the argument.
476 @type name: str
477 @keyword size: The number of elements required.
478 @type size: None or int
479 @keyword can_be_none: A flag specifying if the argument can be none.
480 @type can_be_none: bool
481 @keyword can_be_empty: A flag which if True allows the list to be empty.
482 @type can_be_empty: bool
483 @keyword list_of_lists: A flag which if True allows the argument to be a list of lists of strings.
484 @type list_of_lists: bool
485 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
486 @type raise_error: bool
487 @raise RelaxListStrError: If not a list of strings (and the raise_error flag is set).
488 @raise RelaxNoneListStrError: If not a list of strings or None (and the raise_error flag is set).
489 @return: The answer to the question (if raise_error is not set).
490 @rtype: bool
491 """
492
493
494 fail = False
495
496
497 if can_be_none and arg == None:
498 return True
499
500
501 if not isinstance(arg, list):
502 fail = True
503
504
505 else:
506
507 if size != None and len(arg) != size:
508 fail = True
509
510
511 if not can_be_empty and arg == []:
512 fail = True
513
514
515 if fail:
516 if not raise_error:
517 return False
518 if can_be_none and size != None:
519 raise RelaxNoneListError(name, arg, size)
520 elif can_be_none:
521 raise RelaxNoneListError(name, arg)
522 elif size != None:
523 raise RelaxListError(name, arg, size)
524 else:
525 raise RelaxListError(name, arg)
526
527
528 return True
529
530
531 -def is_none(arg, name, raise_error=True):
532 """Test if the argument is None.
533
534 @param arg: The argument.
535 @type arg: anything
536 @keyword name: The plain English name of the argument.
537 @type name: str
538 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
539 @type raise_error: bool
540 @raise RelaxNoneError: If not None (and the raise_error flag is set).
541 @return: The answer to the question (if raise_error is not set).
542 @rtype: bool
543 """
544
545
546 if arg == None:
547 return True
548
549
550 if not raise_error:
551 return False
552 else:
553 raise RelaxNoneError(name)
554
555
556 -def is_num(arg, name=None, can_be_none=False, raise_error=True):
557 """Test if the argument is a number.
558
559 @param arg: The argument.
560 @type arg: anything
561 @keyword name: The plain English name of the argument.
562 @type name: str
563 @keyword can_be_none: A flag specifying if the argument can be none.
564 @type can_be_none: bool
565 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
566 @type raise_error: bool
567 @raise RelaxNumError: If not a number (and the raise_error flag is set).
568 @raise RelaxNoneNumError: If not a number or not None (and the raise_error flag is set).
569 @return: The answer to the question (if raise_error is not set).
570 @rtype: bool
571 """
572
573
574 if can_be_none and arg == None:
575 return True
576
577
578 if (check_float(arg) or isinstance(arg, int)) and not isinstance(arg, bool):
579 return True
580
581
582 if not raise_error:
583 return False
584 if not can_be_none:
585 raise RelaxNumError(name, arg)
586 else:
587 raise RelaxNoneNumError(name, arg)
588
589
590 -def is_num_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
591 """Test if the argument is a list of numbers.
592
593 @param arg: The argument.
594 @type arg: anything
595 @keyword name: The plain English name of the argument.
596 @type name: str
597 @keyword size: The number of elements required.
598 @type size: None or int
599 @keyword can_be_none: A flag specifying if the argument can be none.
600 @type can_be_none: bool
601 @keyword can_be_empty: A flag which if True allows the list to be empty.
602 @type can_be_empty: bool
603 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
604 @type raise_error: bool
605 @raise RelaxListError: If not a list (and the raise_error flag is set).
606 @raise RelaxListNumError: If not a list of numbers (and the raise_error flag is set).
607 @return: The answer to the question (if raise_error is not set).
608 @rtype: bool
609 """
610
611
612 fail = False
613
614
615 if can_be_none and arg == None:
616 return True
617
618
619 if not isinstance(arg, list) and not isinstance(arg, ndarray):
620 fail = True
621
622
623 else:
624
625 if size != None and len(arg) != size:
626 fail = True
627
628
629 if not can_be_empty and arg == []:
630 fail = True
631
632
633 for i in range(len(arg)):
634 if (not check_float(arg[i]) and not isinstance(arg[i], int)) or isinstance(arg, bool):
635 fail = True
636
637
638 if fail:
639 if not raise_error:
640 return False
641 if can_be_none and size != None:
642 raise RelaxNoneListNumError(name, arg, size)
643 elif can_be_none:
644 raise RelaxNoneListNumError(name, arg)
645 elif size != None:
646 raise RelaxListNumError(name, arg, size)
647 else:
648 raise RelaxListNumError(name, arg)
649
650
651 return True
652
653
654 -def is_num_or_num_tuple(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
655 """Test if the argument is a tuple of numbers.
656
657 @param arg: The argument.
658 @type arg: anything
659 @keyword name: The plain English name of the argument.
660 @type name: str
661 @keyword size: The number of elements required.
662 @type size: None or int
663 @keyword can_be_none: A flag specifying if the argument can be none.
664 @type can_be_none: bool
665 @keyword can_be_empty: A flag which if True allows the list to be empty.
666 @type can_be_empty: bool
667 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
668 @type raise_error: bool
669 @raise RelaxTupleError: If not a tuple (and the raise_error flag is set).
670 @raise RelaxTupleNumError: If not a tuple of numbers (and the raise_error flag is set).
671 @return: The answer to the question (if raise_error is not set).
672 @rtype: bool
673 """
674
675
676 fail = False
677 if size != None and not isinstance(size, list):
678 size = [size]
679
680
681 if can_be_none and arg == None:
682 return True
683
684
685 if not isinstance(arg, tuple):
686
687 try:
688 is_num(arg, name)
689 except:
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 check_float(arg[i]) and not isinstance(arg[i], int)) or isinstance(arg, bool):
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 try:
964 is_str(arg, name)
965 except:
966
967 try:
968 is_num(arg, name)
969 except:
970 fail = True
971
972
973 else:
974
975 if size != None and len(arg) != size:
976 fail = True
977
978
979 if not can_be_empty and arg == []:
980 fail = True
981
982
983 for i in range(len(arg)):
984
985 try:
986 is_str(arg[i], name)
987 except:
988
989 try:
990 is_num(arg[i], name)
991 except:
992 fail = True
993
994
995 if fail:
996 if not raise_error:
997 return False
998 if can_be_none and size != None:
999 raise RelaxNoneNumStrListNumStrError(name, arg, size)
1000 elif can_be_none:
1001 raise RelaxNoneNumStrListNumStrError(name, arg)
1002 elif size != None:
1003 raise RelaxNumStrListNumStrError(name, arg, size)
1004 else:
1005 raise RelaxNumStrListNumStrError(name, arg)
1006
1007
1008 return True
1009
1010
1011 -def is_str_or_num_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
1012 """Test if the argument is a string or a list of numbers.
1013
1014 @param arg: The argument.
1015 @type arg: anything
1016 @keyword name: The plain English name of the argument.
1017 @type name: str
1018 @keyword size: The number of elements required.
1019 @type size: None or int
1020 @keyword can_be_none: A flag specifying if the argument can be none.
1021 @type can_be_none: bool
1022 @keyword can_be_empty: A flag which if True allows the list to be empty.
1023 @type can_be_empty: bool
1024 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
1025 @type raise_error: bool
1026 @raise RelaxStrListNumError: If not a string or a list of strings (and the raise_error flag is set).
1027 @raise RelaxNoneStrListNumError: If not a string, a list of strings, or None (and the raise_error flag is set).
1028 @return: The answer to the question (if raise_error is not set).
1029 @rtype: bool
1030 """
1031
1032
1033 fail = False
1034
1035
1036 if can_be_none and arg == None:
1037 return True
1038
1039
1040 if not isinstance(arg, list):
1041
1042 try:
1043 is_str(arg, name)
1044 except:
1045 fail = True
1046
1047
1048 else:
1049
1050 if size != None and len(arg) != size:
1051 fail = True
1052
1053
1054 if not can_be_empty and arg == []:
1055 fail = True
1056
1057
1058 for i in range(len(arg)):
1059
1060 try:
1061 is_num(arg[i], name)
1062 except:
1063 fail = True
1064
1065
1066 if fail:
1067 if not raise_error:
1068 return False
1069 if can_be_none and size != None:
1070 raise RelaxNoneStrListNumError(name, arg, size)
1071 elif can_be_none:
1072 raise RelaxNoneStrListNumError(name, arg)
1073 elif size != None:
1074 raise RelaxStrListNumError(name, arg, size)
1075 else:
1076 raise RelaxStrListNumError(name, arg)
1077
1078
1079 return True
1080
1081
1082 -def is_str_or_str_list(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
1083 """Test if the argument is a string or a list of strings.
1084
1085 @param arg: The argument.
1086 @type arg: anything
1087 @keyword name: The plain English name of the argument.
1088 @type name: str
1089 @keyword size: The number of elements required.
1090 @type size: None or int
1091 @keyword can_be_none: A flag specifying if the argument can be none.
1092 @type can_be_none: bool
1093 @keyword can_be_empty: A flag which if True allows the list to be empty.
1094 @type can_be_empty: bool
1095 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
1096 @type raise_error: bool
1097 @raise RelaxStrListStrError: If not a string or a list of strings (and the raise_error flag is set).
1098 @raise RelaxNoneStrListStrError: If not a string, a list of strings, or None (and the raise_error flag is set).
1099 @return: The answer to the question (if raise_error is not set).
1100 @rtype: bool
1101 """
1102
1103
1104 fail = False
1105
1106
1107 if can_be_none and arg == None:
1108 return True
1109
1110
1111 if not isinstance(arg, list):
1112
1113 try:
1114 is_str(arg, name)
1115 except:
1116 fail = True
1117
1118
1119 else:
1120
1121 if size != None and len(arg) != size:
1122 fail = True
1123
1124
1125 if not can_be_empty and arg == []:
1126 fail = True
1127
1128
1129 for i in range(len(arg)):
1130
1131 try:
1132 is_str(arg[i], name)
1133 except:
1134 fail = True
1135
1136
1137 if fail:
1138 if not raise_error:
1139 return False
1140 if can_be_none and size != None:
1141 raise RelaxNoneStrListStrError(name, arg, size)
1142 elif can_be_none:
1143 raise RelaxNoneStrListStrError(name, arg)
1144 elif size != None:
1145 raise RelaxStrListStrError(name, arg, size)
1146 else:
1147 raise RelaxStrListStrError(name, arg)
1148
1149
1150 return True
1151
1152
1153 -def is_tuple(arg, name=None, size=None, can_be_none=False, can_be_empty=False, raise_error=True):
1154 """Test if the argument is a tuple.
1155
1156 @param arg: The argument.
1157 @type arg: anything
1158 @keyword name: The plain English name of the argument.
1159 @type name: str
1160 @keyword size: The number of elements required.
1161 @type size: None or int
1162 @keyword can_be_none: A flag specifying if the argument can be none.
1163 @type can_be_none: bool
1164 @keyword can_be_empty: A flag which if True allows the list to be empty.
1165 @type can_be_empty: bool
1166 @keyword raise_error: A flag which if True will cause RelaxErrors to be raised.
1167 @type raise_error: bool
1168 @raise RelaxTupleError: If not a tuple (and the raise_error flag is set).
1169 @raise RelaxNoneTupleError: If not a tuple or not None (and the raise_error flag is set).
1170 @return: The answer to the question (if raise_error is not set).
1171 @rtype: bool
1172 """
1173
1174
1175 fail = False
1176
1177
1178 if can_be_none and arg == None:
1179 return True
1180
1181
1182 if not isinstance(arg, tuple):
1183 fail = True
1184
1185
1186 else:
1187
1188 if size != None and len(arg) != size:
1189 fail = True
1190
1191
1192 if not can_be_empty and arg == []:
1193 fail = True
1194
1195
1196 if fail:
1197 if not raise_error:
1198 return False
1199 if can_be_none and size != None:
1200 raise RelaxNoneTupleError(name, arg, size)
1201 elif can_be_none:
1202 raise RelaxNoneTupleError(name, arg)
1203 elif size != None:
1204 raise RelaxTupleError(name, arg, size)
1205 else:
1206 raise RelaxTupleError(name, arg)
1207
1208
1209 return True
1210