Mercurial > repos > sagun98 > micropita_v2
comparison src/breadcrumbs/src/ValidateData.py @ 0:0de566f21448 draft default tip
v2
author | sagun98 |
---|---|
date | Thu, 03 Jun 2021 18:13:32 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0de566f21448 |
---|---|
1 """ | |
2 Author: Timothy Tickle | |
3 Description: Validate Data containing methods for testing variables. | |
4 """ | |
5 | |
6 ##################################################################################### | |
7 #Copyright (C) <2012> | |
8 # | |
9 #Permission is hereby granted, free of charge, to any person obtaining a copy of | |
10 #this software and associated documentation files (the "Software"), to deal in the | |
11 #Software without restriction, including without limitation the rights to use, copy, | |
12 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
13 #and to permit persons to whom the Software is furnished to do so, subject to | |
14 #the following conditions: | |
15 # | |
16 #The above copyright notice and this permission notice shall be included in all copies | |
17 #or substantial portions of the Software. | |
18 # | |
19 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
20 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
21 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
22 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
23 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
24 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
25 ##################################################################################### | |
26 | |
27 __author__ = "Timothy Tickle" | |
28 __copyright__ = "Copyright 2012" | |
29 __credits__ = ["Timothy Tickle"] | |
30 __license__ = "MIT" | |
31 __maintainer__ = "Timothy Tickle" | |
32 __email__ = "ttickle@sph.harvard.edu" | |
33 __status__ = "Development" | |
34 | |
35 #Import local code | |
36 from types import * | |
37 import decimal | |
38 import os | |
39 import re | |
40 import string | |
41 | |
42 class ValidateData: | |
43 | |
44 #Tested 5 | |
45 @staticmethod | |
46 def funcIsValidBoolean(parameterValue): | |
47 """ | |
48 Validates a parameter as a valid boolean. | |
49 | |
50 :param parameterValue: Value to be evaluated. | |
51 :type Unknown | |
52 :return Boolean: True indicates the parameter is a valid boolean. | |
53 :type Boolean | |
54 """ | |
55 | |
56 #Check to make sure it is not null | |
57 if parameterValue == None: | |
58 return False | |
59 | |
60 #Check to make sure it is a string | |
61 if not type(parameterValue) is BooleanType: | |
62 return False | |
63 return True | |
64 | |
65 #Tested 6 | |
66 @staticmethod | |
67 def funcIsTrue(parameterValue): | |
68 """ | |
69 Validates a parameter as true. | |
70 | |
71 :param parameterValue: Value to be evaluated. | |
72 :type Unknown | |
73 :return Boolean: True indicates the parameter is True. | |
74 :type Boolean | |
75 """ | |
76 | |
77 if(ValidateData.funcIsValidBoolean(parameterValue)): | |
78 if(parameterValue == True): | |
79 return True | |
80 return False | |
81 | |
82 #Tested 6 | |
83 @staticmethod | |
84 def funcIsFalse(parameterValue): | |
85 """ | |
86 Validates a parameter as false. | |
87 | |
88 :param parameterValue: Value to be evaluated. | |
89 :type Unknown | |
90 :return Boolean: True indicates the parameter is False. | |
91 :type Boolean | |
92 """ | |
93 | |
94 if(ValidateData.funcIsValidBoolean(parameterValue)): | |
95 if(parameterValue == False): | |
96 return True | |
97 return False | |
98 | |
99 #Tested 5 | |
100 @staticmethod | |
101 def funcIsValidInteger(parameterValue): | |
102 """ | |
103 Validates a parameter as an integer. | |
104 | |
105 :param parameterValue: Value to be evaluated. | |
106 :type Unknown | |
107 :return Boolean: True indicates the parameter is an integer. | |
108 :type Boolean | |
109 """ | |
110 | |
111 #Check to make sure it is not null | |
112 if (parameterValue == None): | |
113 return False | |
114 | |
115 #Check to make sure it is an integer | |
116 if not type(parameterValue) is IntType: | |
117 return False | |
118 | |
119 return True | |
120 | |
121 #Tested 5 | |
122 @staticmethod | |
123 def funcIsValidPositiveInteger(parameterValue, tempZero = False): | |
124 """ | |
125 Validates a parameter as false. | |
126 | |
127 :param parameterValue: Value to be evaluated. | |
128 :type Unknown | |
129 :param tempZero: Allows one to set what the value for zero should return. | |
130 :type Boolean The return value for zero. | |
131 :return Boolean: True indicates the parameter is a positive integer. | |
132 :type Boolean | |
133 """ | |
134 | |
135 #Check to make sure it is not null | |
136 if not ValidateData.funcIsValidInteger(parameterValue): | |
137 return False | |
138 | |
139 #Check to see it is positive | |
140 if (parameterValue < 0): | |
141 return False | |
142 | |
143 #Check for zero value | |
144 if(parameterValue == 0): | |
145 return tempZero | |
146 return True | |
147 | |
148 #Tested 14 | |
149 @staticmethod | |
150 def funcIsValidNumeric(parameterValue): | |
151 """ | |
152 Validates a parameter as an integer. | |
153 | |
154 :param parameterValue: Value to be evaluated. | |
155 :type Unknown | |
156 :return Boolean: True indicates the parameter is a numeric. | |
157 :type Boolean | |
158 """ | |
159 | |
160 #Check to make sure it is not null | |
161 if (parameterValue == None): | |
162 return False | |
163 #Check to make sure it is an integer | |
164 if((type(parameterValue) == IntType)or(type(parameterValue) == LongType)or(type(parameterValue) == FloatType)or(type(parameterValue) == ComplexType)or(str(type(parameterValue)) == "<type 'numpy.float64'>")): | |
165 if(not type(parameterValue) == BooleanType): | |
166 return True | |
167 return False | |
168 | |
169 #Tested 5 | |
170 @staticmethod | |
171 def funcIsValidStringType(parameterValue): | |
172 """ | |
173 Validates a parameter as a string. This allows the string to be blank or empty. | |
174 | |
175 :param parameterValue: Value to be evaluated. | |
176 :type Unknown | |
177 :return Boolean: True indicates the parameter is a string type. | |
178 :type Boolean | |
179 """ | |
180 | |
181 #Check to make sure it is not null | |
182 if parameterValue == None: | |
183 return False | |
184 | |
185 #Check to make sure it is a string | |
186 if not type(parameterValue) is StringType: | |
187 return False | |
188 | |
189 return True | |
190 | |
191 #Tested 5 | |
192 @staticmethod | |
193 def funcIsValidString(parameterValue): | |
194 """ | |
195 Validates a parameter as a string. Does NOT allow string to be blank or empty. | |
196 | |
197 :param parameterValue: Value to be evaluated. | |
198 :type Unknown | |
199 :return Boolean: True indicates the parameter is a string. | |
200 :type Boolean | |
201 """ | |
202 | |
203 #Type check | |
204 if not ValidateData.funcIsValidStringType(parameterValue): | |
205 return False | |
206 | |
207 #Check to see it is not blank | |
208 if parameterValue.strip() == "": | |
209 return False | |
210 return True | |
211 | |
212 @staticmethod | |
213 def funcIsValidStringInt(parameterValue): | |
214 """ | |
215 Validates a parameter that is a string as a format which is an integer. | |
216 | |
217 :param parameterValue: Value to be evaluated. | |
218 :type Unknown | |
219 """ | |
220 | |
221 #Type string check | |
222 if not ValidateData.funcIsValidStringType(parameterValue): | |
223 return False | |
224 | |
225 #Check to see if the string can be converted to an integer | |
226 try: | |
227 int(parameterValue) | |
228 except: | |
229 return False | |
230 return True | |
231 | |
232 @staticmethod | |
233 def funcIsValidStringFloat(parameterValue): | |
234 """ | |
235 Validates a parameter that is a string as a format which is a numeric. | |
236 | |
237 :param parameterValue: Value to be evaluated. | |
238 :type Unknown | |
239 """ | |
240 | |
241 #Type string check | |
242 if not ValidateData.funcIsValidStringType(parameterValue): | |
243 return False | |
244 | |
245 #Check to see if the string can be converted to a double | |
246 try: | |
247 float(parameterValue) | |
248 except: | |
249 return False | |
250 return True | |
251 | |
252 #Tested 6 | |
253 @staticmethod | |
254 def funcIsValidFormatString(parameterValue): | |
255 """ | |
256 Validates a parameter as a valid format string. | |
257 | |
258 :param parameterValue: Value to be evaluated. | |
259 :type Unknown | |
260 :return Boolean: True indicates the parameter is a valid value. | |
261 :type Boolean | |
262 """ | |
263 | |
264 lettersValid = False | |
265 if ValidateData.funcIsValidString(parameterValue): | |
266 validChars = "BbcdfHhIiLlPpsx0123456789" | |
267 for letter in parameterValue: | |
268 lettersValid = letter in validChars | |
269 if(not lettersValid): | |
270 break | |
271 return lettersValid | |
272 | |
273 #Tested 5 | |
274 @staticmethod | |
275 def funcIsValidChar(parameterValue): | |
276 """ | |
277 Validates a parameter as a valid character. | |
278 | |
279 :param parameterValue: Value to be evaluated. | |
280 :type Unknown | |
281 :return Boolean: True indicates the parameter is a valid value. | |
282 :type Boolean | |
283 """ | |
284 | |
285 return ValidateData.funcIsValidString(parameterValue) | |
286 | |
287 #Tested 13 | |
288 @staticmethod | |
289 def funcIsValidPositiveNumberChar(parameterValue): | |
290 """ | |
291 Validates a parameter as a valid character representing a number. | |
292 | |
293 :param parameterValue: Value to be evaluated. | |
294 :type Unknown | |
295 :return Boolean: True indicates the parameter is a valid value. | |
296 :type Boolean | |
297 """ | |
298 | |
299 #Check to make sure is a valid string | |
300 if not ValidateData.funcIsValidString(parameterValue): | |
301 return False | |
302 | |
303 #Try to convert to decimal | |
304 try: | |
305 decimalConversion = decimal.Decimal(parameterValue) | |
306 if decimalConversion < 0: | |
307 return False | |
308 except: | |
309 return False | |
310 return True | |
311 | |
312 #Tested 9 | |
313 @staticmethod | |
314 def funcIsValidFlagChar(parameterValue): | |
315 """ | |
316 Validates a parameter as a valid character representing a boolean. | |
317 | |
318 :param parameterValue: Value to be evaluated. | |
319 :type Unknown | |
320 :return Boolean: True indicates the parameter is a valid value. | |
321 :type Boolean | |
322 """ | |
323 | |
324 if parameterValue == '0' or parameterValue == "0" or parameterValue == '1' or parameterValue == "1": | |
325 return True | |
326 return False | |
327 | |
328 #Tested 15 | |
329 @staticmethod | |
330 def funcIsValidBoundedIntegerChar(parameterValue, iValueOne, iValueTwo): | |
331 """ | |
332 Validates a parameter as a valid characater that represents an integer inclusively bounded by two given values. | |
333 | |
334 :param parameterValue: Value to be evaluated. | |
335 :type Unknown | |
336 :param iValueOne: One bound for the value. | |
337 :type Integer | |
338 :param iValueTwo: The other bound for the data. | |
339 :type Integer | |
340 :return Boolean: True indicates the parameter is a valid value. | |
341 :type Boolean | |
342 """ | |
343 | |
344 #Check to make sure is a valid string | |
345 if not ValidateData.funcIsValidString(parameterValue): | |
346 return False | |
347 | |
348 #Check to make sure is a valid integer | |
349 if not ValidateData.funcIsValidInteger(iValueOne): | |
350 return False | |
351 | |
352 #Check to make sure is a valid integer | |
353 if not ValidateData.funcIsValidInteger(iValueTwo): | |
354 return False | |
355 | |
356 #Try to convert to decimal | |
357 try: | |
358 intConversion = int(parameterValue) | |
359 if(iValueOne < iValueTwo): | |
360 if ((intConversion >= iValueOne) and (intConversion <= iValueTwo)): | |
361 return True | |
362 return False | |
363 if(iValueTwo < iValueOne): | |
364 if ((intConversion >= iValueTwo) and (intConversion <= iValueOne)): | |
365 return True | |
366 return False | |
367 if(iValueOne == iValueTwo): | |
368 if (intConversion == iValueOne): | |
369 return True | |
370 return False | |
371 except: | |
372 return False | |
373 | |
374 #Tested 9 | |
375 @staticmethod | |
376 def funcIsValidList(parameterValue): | |
377 """ | |
378 Validates a parameter as a list. | |
379 | |
380 :param parameterValue: Value to be evaluated. | |
381 :type Unknown | |
382 :return Boolean: True indicates the parameter is a list | |
383 :type Boolean | |
384 """ | |
385 | |
386 #Check to make sure it is not null | |
387 if parameterValue == None: | |
388 return False | |
389 | |
390 #Check to make sure it is a list | |
391 if not type(parameterValue) is ListType: | |
392 return False | |
393 | |
394 #Check elements | |
395 listSize = len(parameterValue) | |
396 for i in range(0,listSize): | |
397 if parameterValue[i] == None: | |
398 return False | |
399 if type(parameterValue[i]) is ListType: | |
400 if ValidateData.funcIsValidList(parameterValue[i]) == False: | |
401 return False | |
402 return True | |
403 | |
404 #Tested 9 | |
405 @staticmethod | |
406 def funcIsValidTuple(parameterValue): | |
407 """ | |
408 Validates a parameter as a tuple. | |
409 | |
410 :param parameterValue: Value to be evaluated. | |
411 :type Unknown | |
412 :return Boolean: True indicates the parameter is a tuple | |
413 :type Boolean | |
414 """ | |
415 | |
416 #Check to make sure it is not null | |
417 if parameterValue == None: | |
418 return False | |
419 | |
420 #Check to make sure it is a string | |
421 if not type(parameterValue) is TupleType: | |
422 return False | |
423 | |
424 #Check elements | |
425 tupleSize = len(parameterValue) | |
426 for i in range(0,tupleSize): | |
427 if parameterValue[i] == None: | |
428 return False | |
429 if type(parameterValue[i]) is TupleType: | |
430 if ValidateData.funcIsValidTuple(parameterValue[i]) == False: | |
431 return False | |
432 return True | |
433 | |
434 #Tested 7 | |
435 @staticmethod | |
436 def funcIsValidNumericList(parameterValue): | |
437 """ | |
438 Validates a parameter as a list of numeric values. | |
439 | |
440 :param parameterValue: Value to be evaluated. | |
441 :type Unknown | |
442 :return Boolean: True indicates the parameter is a list of numeric values. | |
443 :type Boolean | |
444 """ | |
445 | |
446 #Check is valid list | |
447 if(not ValidateData.funcIsValidList(parameterValue)): | |
448 return False | |
449 | |
450 #Check elements | |
451 listSize = len(parameterValue) | |
452 for i in xrange(0,listSize): | |
453 if(not ValidateData.funcIsValidNumeric(parameterValue[i])): | |
454 return False | |
455 return True | |
456 | |
457 #Tested 7 | |
458 @staticmethod | |
459 def funcIsValidStringList(parameterValue): | |
460 """ | |
461 Validates a parameter as a list of string values. | |
462 | |
463 :param parameterValue: Value to be evaluated. | |
464 :type Unknown | |
465 :return Boolean: True indicates the parameter is a list of string values. | |
466 :type Boolean | |
467 """ | |
468 | |
469 #Check is valid list | |
470 if(not ValidateData.funcIsValidList(parameterValue)): | |
471 return False | |
472 | |
473 #Check elements | |
474 listSize = len(parameterValue) | |
475 for i in xrange(0,listSize): | |
476 if(not ValidateData.funcIsValidString(parameterValue[i])): | |
477 return False | |
478 return True | |
479 | |
480 #Tested 4 | |
481 @staticmethod | |
482 def funcIsValidNPArray(parameterValue): | |
483 """ | |
484 Validates a parameter as a numpy array. | |
485 | |
486 :param parameterValue: Value to be evaluated. | |
487 :type Unknown | |
488 :return Boolean: True indicates the parameter is a numpy array. | |
489 :type Boolean | |
490 """ | |
491 | |
492 #Check to make sure it is not null | |
493 if parameterValue == None: | |
494 return False | |
495 | |
496 #Check to make sure it is a structure array | |
497 if not str(type(parameterValue)) == "<type 'numpy.ndarray'>": | |
498 return False | |
499 | |
500 return True | |
501 | |
502 #Tested 9 | |
503 @staticmethod | |
504 def funcIsValidDictionary(parameterValue): | |
505 """ | |
506 Validates a parameter as a dictionary. | |
507 | |
508 :param parameterValue: Value to be evaluated. | |
509 :type Unknown | |
510 :return Boolean: True indicates the parameter is a dictionary. | |
511 :type Boolean | |
512 """ | |
513 | |
514 #Check to make sure it is not null | |
515 if parameterValue == None: | |
516 return False | |
517 | |
518 #Check to make sure it is a string | |
519 if not type(parameterValue) is DictType: | |
520 return False | |
521 | |
522 #Check key elements | |
523 keyList = parameterValue.keys() | |
524 keyListSize = len(keyList) | |
525 for i in range(0,keyListSize): | |
526 if keyList[i] == None: | |
527 return False | |
528 if type(keyList[i]) is ListType: | |
529 if validateData.funcIsValidList(keyList[i]) == False: | |
530 return False | |
531 | |
532 #Check key elements | |
533 itemList = parameterValue.values() | |
534 itemListSize = len(itemList) | |
535 | |
536 for i in range(0,itemListSize): | |
537 if itemList[i] == None: | |
538 return False | |
539 if type(itemList[i]) is ListType: | |
540 if ValidateData.funcIsValidList(itemList[i]) == False: | |
541 return False | |
542 return True | |
543 | |
544 #Tested 18 | |
545 @staticmethod | |
546 def funcIsValidDNASequence(parameterValue): | |
547 """ | |
548 Validates a parameter as a valid DNA sequence. | |
549 | |
550 :param parameterValue: Value to be evaluated. | |
551 :type Unknown | |
552 :return Boolean: True indicates the parameter is a valid value. | |
553 :type Boolean | |
554 """ | |
555 | |
556 if ValidateData.funcIsValidString(parameterValue): | |
557 expression = re.compile(r'[^atcgATCG]') | |
558 if not None == expression.search(parameterValue): | |
559 return False | |
560 return True | |
561 return False | |
562 | |
563 #Tested 15 | |
564 @staticmethod | |
565 def funcIsValidNucleotideBase(parameterValue): | |
566 """ | |
567 Validates a parameter as a character which is a valid nucleotide representation. | |
568 | |
569 :param parameterValue: Value to be evaluated. | |
570 :type Unknown | |
571 :return Boolean: True indicates the parameter is a valid value. | |
572 :type Boolean | |
573 """ | |
574 | |
575 if (ValidateData.funcIsValidDNASequence(parameterValue) or (parameterValue == 'u') or (parameterValue == "U")): | |
576 if (len(parameterValue) == 1): | |
577 return True | |
578 return False | |
579 | |
580 #Testing 4 | |
581 @staticmethod | |
582 def funcIsValidFileName(parameterValue): | |
583 """ | |
584 Validates a parameter as a valid file name. | |
585 | |
586 :param parameterValue: Value to be evaluated. | |
587 :type Unknown | |
588 :return Boolean: True indicates the parameter is a valid file path. | |
589 :type Boolean | |
590 """ | |
591 | |
592 if parameterValue is None: | |
593 return False | |
594 elif(ValidateData.funcIsValidString(parameterValue)): | |
595 return os.path.exists(parameterValue) | |
596 return False | |
597 | |
598 #Tested 5 | |
599 @staticmethod | |
600 def funcIsValidClass(parameterValue, strCorrectName): | |
601 """ | |
602 Validates a parameter as a valid class (of a specifc type given by name). | |
603 | |
604 :param parameterValue: Value to be evaluated. | |
605 :type Unknown | |
606 :param strCorrectName: Name of te class the parameter should be. | |
607 :type Unknown | |
608 :return Boolean: True indicates the parameter is a valid value. | |
609 :type Boolean | |
610 """ | |
611 | |
612 if(parameterValue==None): | |
613 return False | |
614 if not ValidateData.funcIsValidString(strCorrectName): | |
615 return False | |
616 classType = type(parameterValue).__name__ | |
617 if(classType == strCorrectName): | |
618 return True | |
619 if(classType == 'instance'): | |
620 if(parameterValue.__class__.__name__==strCorrectName): | |
621 return True | |
622 else: | |
623 return False | |
624 return False |