Debian Codegraph @

lefsebiom/ValidateData.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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__ = "[email protected]"
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
43 class ValidateData:
44
•45 # Tested 5
46 @staticmethod
47 def funcIsValidBoolean(parameterValue):
48 """
49 Validates a parameter as a valid boolean.
50
51 :param parameterValue: Value to be evaluated.
52 :type Unknown
53 :return Boolean: True indicates the parameter is a valid boolean.
54 :type Boolean
55 """
56
•57 # Check to make sure it is not null
58 if parameterValue == None:
59 return False
60
•61 # Check to make sure it is a string
62 if not type(parameterValue) is BooleanType:
63 return False
64 return True
65
•66 # Tested 6
67 @staticmethod
68 def funcIsTrue(parameterValue):
69 """
70 Validates a parameter as true.
71
72 :param parameterValue: Value to be evaluated.
73 :type Unknown
74 :return Boolean: True indicates the parameter is True.
75 :type Boolean
76 """
77
78 if(ValidateData.funcIsValidBoolean(parameterValue)):
79 if(parameterValue == True):
80 return True
81 return False
82
•83 # Tested 6
84 @staticmethod
85 def funcIsFalse(parameterValue):
86 """
87 Validates a parameter as false.
88
89 :param parameterValue: Value to be evaluated.
90 :type Unknown
91 :return Boolean: True indicates the parameter is False.
92 :type Boolean
93 """
94
95 if(ValidateData.funcIsValidBoolean(parameterValue)):
96 if(parameterValue == False):
97 return True
98 return False
99
•100 # Tested 5
101 @staticmethod
102 def funcIsValidInteger(parameterValue):
103 """
104 Validates a parameter as an integer.
105
106 :param parameterValue: Value to be evaluated.
107 :type Unknown
108 :return Boolean: True indicates the parameter is an integer.
109 :type Boolean
110 """
111
•112 # Check to make sure it is not null
113 if (parameterValue == None):
114 return False
115
•116 # Check to make sure it is an integer
117 if not type(parameterValue) is IntType:
118 return False
119
120 return True
121
•122 # Tested 5
123 @staticmethod
•124 def funcIsValidPositiveInteger(parameterValue, tempZero=False):
125 """
126 Validates a parameter as false.
127
128 :param parameterValue: Value to be evaluated.
129 :type Unknown
130 :param tempZero: Allows one to set what the value for zero should return.
131 :type Boolean The return value for zero.
132 :return Boolean: True indicates the parameter is a positive integer.
133 :type Boolean
134 """
135
•136 # Check to make sure it is not null
137 if not ValidateData.funcIsValidInteger(parameterValue):
138 return False
139
•140 # Check to see it is positive
141 if (parameterValue < 0):
142 return False
143
•144 # Check for zero value
145 if(parameterValue == 0):
146 return tempZero
147 return True
148
•149 # Tested 14
150 @staticmethod
151 def funcIsValidNumeric(parameterValue):
152 """
153 Validates a parameter as an integer.
154
155 :param parameterValue: Value to be evaluated.
156 :type Unknown
157 :return Boolean: True indicates the parameter is a numeric.
158 :type Boolean
159 """
160
•161 # Check to make sure it is not null
162 if (parameterValue == None):
163 return False
•164 # Check to make sure it is an integer
165 if((type(parameterValue) == IntType)or(type(parameterValue) == LongType)or(type(parameterValue) == FloatType)or(type(parameterValue) == ComplexType)or(str(type(parameterValue)) == "<type 'numpy.float64'>")):
166 if(not type(parameterValue) == BooleanType):
167 return True
168 return False
169
•170 # Tested 5
171 @staticmethod
172 def funcIsValidStringType(parameterValue):
173 """
174 Validates a parameter as a string. This allows the string to be blank or empty.
175
176 :param parameterValue: Value to be evaluated.
177 :type Unknown
178 :return Boolean: True indicates the parameter is a string type.
179 :type Boolean
180 """
181
•182 # Check to make sure it is not null
183 if parameterValue == None:
184 return False
185
•186 # Check to make sure it is a string
187 if not type(parameterValue) is StringType:
188 return False
189
190 return True
191
•192 # Tested 5
193 @staticmethod
194 def funcIsValidString(parameterValue):
195 """
196 Validates a parameter as a string. Does NOT allow string to be blank or empty.
197
198 :param parameterValue: Value to be evaluated.
199 :type Unknown
200 :return Boolean: True indicates the parameter is a string.
201 :type Boolean
202 """
203
•204 # Type check
205 if not ValidateData.funcIsValidStringType(parameterValue):
206 return False
207
•208 # Check to see it is not blank
209 if parameterValue.strip() == "":
210 return False
211 return True
212
213 @staticmethod
214 def funcIsValidStringInt(parameterValue):
215 """
216 Validates a parameter that is a string as a format which is an integer.
217
218 :param parameterValue: Value to be evaluated.
219 :type Unknown
220 """
221
•222 # Type string check
223 if not ValidateData.funcIsValidStringType(parameterValue):
224 return False
225
•226 # Check to see if the string can be converted to an integer
227 try:
228 int(parameterValue)
229 except:
230 return False
231 return True
232
233 @staticmethod
234 def funcIsValidStringFloat(parameterValue):
235 """
236 Validates a parameter that is a string as a format which is a numeric.
237
238 :param parameterValue: Value to be evaluated.
239 :type Unknown
240 """
241
•242 # Type string check
243 if not ValidateData.funcIsValidStringType(parameterValue):
244 return False
245
•246 # Check to see if the string can be converted to a double
247 try:
248 float(parameterValue)
249 except:
250 return False
251 return True
252
•253 # Tested 6
254 @staticmethod
255 def funcIsValidFormatString(parameterValue):
256 """
257 Validates a parameter as a valid format string.
258
259 :param parameterValue: Value to be evaluated.
260 :type Unknown
261 :return Boolean: True indicates the parameter is a valid value.
262 :type Boolean
263 """
264
265 lettersValid = False
266 if ValidateData.funcIsValidString(parameterValue):
267 validChars = "BbcdfHhIiLlPpsx0123456789"
268 for letter in parameterValue:
269 lettersValid = letter in validChars
270 if(not lettersValid):
271 break
272 return lettersValid
273
•274 # Tested 5
275 @staticmethod
276 def funcIsValidChar(parameterValue):
277 """
278 Validates a parameter as a valid character.
279
280 :param parameterValue: Value to be evaluated.
281 :type Unknown
282 :return Boolean: True indicates the parameter is a valid value.
283 :type Boolean
284 """
285
286 return ValidateData.funcIsValidString(parameterValue)
287
•288 # Tested 13
289 @staticmethod
290 def funcIsValidPositiveNumberChar(parameterValue):
291 """
292 Validates a parameter as a valid character representing a number.
293
294 :param parameterValue: Value to be evaluated.
295 :type Unknown
296 :return Boolean: True indicates the parameter is a valid value.
297 :type Boolean
298 """
299
•300 # Check to make sure is a valid string
301 if not ValidateData.funcIsValidString(parameterValue):
302 return False
303
•304 # Try to convert to decimal
305 try:
306 decimalConversion = decimal.Decimal(parameterValue)
307 if decimalConversion < 0:
308 return False
309 except:
310 return False
311 return True
312
•313 # Tested 9
314 @staticmethod
315 def funcIsValidFlagChar(parameterValue):
316 """
317 Validates a parameter as a valid character representing a boolean.
318
319 :param parameterValue: Value to be evaluated.
320 :type Unknown
321 :return Boolean: True indicates the parameter is a valid value.
322 :type Boolean
323 """
324
325 if parameterValue == '0' or parameterValue == "0" or parameterValue == '1' or parameterValue == "1":
326 return True
327 return False
328
•329 # Tested 15
330 @staticmethod
331 def funcIsValidBoundedIntegerChar(parameterValue, iValueOne, iValueTwo):
332 """
333 Validates a parameter as a valid characater that represents an integer inclusively bounded by two given values.
334
335 :param parameterValue: Value to be evaluated.
336 :type Unknown
337 :param iValueOne: One bound for the value.
338 :type Integer
339 :param iValueTwo: The other bound for the data.
340 :type Integer
341 :return Boolean: True indicates the parameter is a valid value.
342 :type Boolean
343 """
344
•345 # Check to make sure is a valid string
346 if not ValidateData.funcIsValidString(parameterValue):
347 return False
348
•349 # Check to make sure is a valid integer
350 if not ValidateData.funcIsValidInteger(iValueOne):
351 return False
352
•353 # Check to make sure is a valid integer
354 if not ValidateData.funcIsValidInteger(iValueTwo):
355 return False
356
•357 # Try to convert to decimal
358 try:
359 intConversion = int(parameterValue)
360 if(iValueOne < iValueTwo):
361 if ((intConversion >= iValueOne) and (intConversion <= iValueTwo)):
362 return True
363 return False
364 if(iValueTwo < iValueOne):
365 if ((intConversion >= iValueTwo) and (intConversion <= iValueOne)):
366 return True
367 return False
368 if(iValueOne == iValueTwo):
369 if (intConversion == iValueOne):
370 return True
371 return False
372 except:
373 return False
374
•375 # Tested 9
376 @staticmethod
377 def funcIsValidList(parameterValue):
378 """
379 Validates a parameter as a list.
380
381 :param parameterValue: Value to be evaluated.
382 :type Unknown
383 :return Boolean: True indicates the parameter is a list
384 :type Boolean
385 """
386
•387 # Check to make sure it is not null
388 if parameterValue == None:
389 return False
390
•391 # Check to make sure it is a list
392 if not type(parameterValue) is ListType:
393 return False
394
•395 # Check elements
396 listSize = len(parameterValue)
•397 for i in range(0, listSize):
398 if parameterValue[i] == None:
399 return False
400 if type(parameterValue[i]) is ListType:
401 if ValidateData.funcIsValidList(parameterValue[i]) == False:
402 return False
403 return True
404
•405 # Tested 9
406 @staticmethod
407 def funcIsValidTuple(parameterValue):
408 """
409 Validates a parameter as a tuple.
410
411 :param parameterValue: Value to be evaluated.
412 :type Unknown
413 :return Boolean: True indicates the parameter is a tuple
414 :type Boolean
415 """
416
•417 # Check to make sure it is not null
418 if parameterValue == None:
419 return False
420
•421 # Check to make sure it is a string
422 if not type(parameterValue) is TupleType:
423 return False
424
•425 # Check elements
426 tupleSize = len(parameterValue)
•427 for i in range(0, tupleSize):
428 if parameterValue[i] == None:
429 return False
430 if type(parameterValue[i]) is TupleType:
431 if ValidateData.funcIsValidTuple(parameterValue[i]) == False:
432 return False
433 return True
434
•435 # Tested 7
436 @staticmethod
437 def funcIsValidNumericList(parameterValue):
438 """
439 Validates a parameter as a list of numeric values.
440
441 :param parameterValue: Value to be evaluated.
442 :type Unknown
443 :return Boolean: True indicates the parameter is a list of numeric values.
444 :type Boolean
445 """
446
447 #Check is valid list
448 if(not ValidateData.funcIsValidList(parameterValue)):
449 return False
450
451 #Check elements
452 listSize = len(parameterValue)
453 for i in range(0,listSize):
454 if(not ValidateData.funcIsValidNumeric(parameterValue[i])):
455 return False
456 return True
457
458 #Tested 7
459 @staticmethod
460 def funcIsValidStringList(parameterValue):
461 """
462 Validates a parameter as a list of string values.
463
464 :param parameterValue: Value to be evaluated.
465 :type Unknown
466 :return Boolean: True indicates the parameter is a list of string values.
467 :type Boolean
468 """
469
470 #Check is valid list
471 if(not ValidateData.funcIsValidList(parameterValue)):
472 return False
473
474 #Check elements
475 listSize = len(parameterValue)
476 for i in range(0,listSize):
477 if(not ValidateData.funcIsValidString(parameterValue[i])):
478 return False
479 return True
480
481 #Tested 4
482 @staticmethod
483 def funcIsValidNPArray(parameterValue):
484 """
485 Validates a parameter as a numpy array.
486
487 :param parameterValue: Value to be evaluated.
488 :type Unknown
489 :return Boolean: True indicates the parameter is a numpy array.
490 :type Boolean
491 """
492
•493 # Check to make sure it is not null
494 if parameterValue == None:
495 return False
496
•497 # Check to make sure it is a structure array
498 if not str(type(parameterValue)) == "<type 'numpy.ndarray'>":
499 return False
500
501 return True
502
•503 # Tested 9
504 @staticmethod
505 def funcIsValidDictionary(parameterValue):
506 """
507 Validates a parameter as a dictionary.
508
509 :param parameterValue: Value to be evaluated.
510 :type Unknown
511 :return Boolean: True indicates the parameter is a dictionary.
512 :type Boolean
513 """
514
515 #Check to make sure it is not null
516 if parameterValue == None:
517 return False
518
519 #Check to make sure it is a string
520 if not type(parameterValue) is DictType:
521 return False
522
523 #Check key elements
524 keyList = list(parameterValue.keys())
525 keyListSize = len(keyList)
526 for i in range(0,keyListSize):
527 if keyList[i] == None:
528 return False
529 if type(keyList[i]) is ListType:
530 if validateData.funcIsValidList(keyList[i]) == False:
531 return False
532
533 #Check key elements
534 itemList = list(parameterValue.values())
535 itemListSize = len(itemList)
536
537 for i in range(0,itemListSize):
538 if itemList[i] == None:
539 return False
540 if type(itemList[i]) is ListType:
541 if ValidateData.funcIsValidList(itemList[i]) == False:
542 return False
543 return True
544
•545 # Tested 18
546 @staticmethod
547 def funcIsValidDNASequence(parameterValue):
548 """
549 Validates a parameter as a valid DNA sequence.
550
551 :param parameterValue: Value to be evaluated.
552 :type Unknown
553 :return Boolean: True indicates the parameter is a valid value.
554 :type Boolean
555 """
556
557 if ValidateData.funcIsValidString(parameterValue):
558 expression = re.compile(r'[^atcgATCG]')
559 if not None == expression.search(parameterValue):
560 return False
561 return True
562 return False
563
•564 # Tested 15
565 @staticmethod
566 def funcIsValidNucleotideBase(parameterValue):
567 """
568 Validates a parameter as a character which is a valid nucleotide representation.
569
570 :param parameterValue: Value to be evaluated.
571 :type Unknown
572 :return Boolean: True indicates the parameter is a valid value.
573 :type Boolean
574 """
575
576 if (ValidateData.funcIsValidDNASequence(parameterValue) or (parameterValue == 'u') or (parameterValue == "U")):
577 if (len(parameterValue) == 1):
578 return True
579 return False
580
•581 # Testing 4
582 @staticmethod
583 def funcIsValidFileName(parameterValue):
584 """
585 Validates a parameter as a valid file name.
586
587 :param parameterValue: Value to be evaluated.
588 :type Unknown
589 :return Boolean: True indicates the parameter is a valid file path.
590 :type Boolean
591 """
592
593 if parameterValue is None:
594 return False
595 elif(ValidateData.funcIsValidString(parameterValue)):
596 return os.path.exists(parameterValue)
597 return False
598
•599 # Tested 5
600 @staticmethod
601 def funcIsValidClass(parameterValue, strCorrectName):
602 """
603 Validates a parameter as a valid class (of a specifc type given by name).
604
605 :param parameterValue: Value to be evaluated.
606 :type Unknown
607 :param strCorrectName: Name of te class the parameter should be.
608 :type Unknown
609 :return Boolean: True indicates the parameter is a valid value.
610 :type Boolean
611 """
612
•613 if(parameterValue == None):
614 return False
615 if not ValidateData.funcIsValidString(strCorrectName):
616 return False
617 classType = type(parameterValue).__name__
618 if(classType == strCorrectName):
619 return True
620 if(classType == 'instance'):
•621 if(parameterValue.__class__.__name__ == strCorrectName):
622 return True
623 else:
624 return False
625 return False