| 1 |
""" |
| 2 |
Author: Timothy Tickle |
| 3 |
Description: Validate Data containing methods for testing variables. |
| 4 |
""" |
| 5 |
|
| 6 |
|
| •7 |
|
| 8 |
|
| •9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| •16 |
|
| 17 |
|
| 18 |
|
| •19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 36 |
from types import * |
| 37 |
import decimal |
| 38 |
import os |
| 39 |
import re |
| 40 |
import string |
| 41 |
|
| •42 |
|
| 43 |
class ValidateData: |
| 44 |
|
| •45 |
|
| 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 |
|
| 58 |
if parameterValue == None: |
| 59 |
return False |
| 60 |
|
| •61 |
|
| 62 |
if not type(parameterValue) is BooleanType: |
| 63 |
return False |
| 64 |
return True |
| 65 |
|
| •66 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 113 |
if (parameterValue == None): |
| 114 |
return False |
| 115 |
|
| •116 |
|
| 117 |
if not type(parameterValue) is IntType: |
| 118 |
return False |
| 119 |
|
| 120 |
return True |
| 121 |
|
| •122 |
|
| 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 |
|
| 137 |
if not ValidateData.funcIsValidInteger(parameterValue): |
| 138 |
return False |
| 139 |
|
| •140 |
|
| 141 |
if (parameterValue < 0): |
| 142 |
return False |
| 143 |
|
| •144 |
|
| 145 |
if(parameterValue == 0): |
| 146 |
return tempZero |
| 147 |
return True |
| 148 |
|
| •149 |
|
| 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 |
|
| 162 |
if (parameterValue == None): |
| 163 |
return False |
| •164 |
|
| 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 |
|
| 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 |
|
| 183 |
if parameterValue == None: |
| 184 |
return False |
| 185 |
|
| •186 |
|
| 187 |
if not type(parameterValue) is StringType: |
| 188 |
return False |
| 189 |
|
| 190 |
return True |
| 191 |
|
| •192 |
|
| 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 |
|
| 205 |
if not ValidateData.funcIsValidStringType(parameterValue): |
| 206 |
return False |
| 207 |
|
| •208 |
|
| 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 |
|
| 223 |
if not ValidateData.funcIsValidStringType(parameterValue): |
| 224 |
return False |
| 225 |
|
| •226 |
|
| 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 |
|
| 243 |
if not ValidateData.funcIsValidStringType(parameterValue): |
| 244 |
return False |
| 245 |
|
| •246 |
|
| 247 |
try: |
| 248 |
float(parameterValue) |
| 249 |
except: |
| 250 |
return False |
| 251 |
return True |
| 252 |
|
| •253 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 301 |
if not ValidateData.funcIsValidString(parameterValue): |
| 302 |
return False |
| 303 |
|
| •304 |
|
| 305 |
try: |
| 306 |
decimalConversion = decimal.Decimal(parameterValue) |
| 307 |
if decimalConversion < 0: |
| 308 |
return False |
| 309 |
except: |
| 310 |
return False |
| 311 |
return True |
| 312 |
|
| •313 |
|
| 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 |
|
| 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 |
|
| 346 |
if not ValidateData.funcIsValidString(parameterValue): |
| 347 |
return False |
| 348 |
|
| •349 |
|
| 350 |
if not ValidateData.funcIsValidInteger(iValueOne): |
| 351 |
return False |
| 352 |
|
| •353 |
|
| 354 |
if not ValidateData.funcIsValidInteger(iValueTwo): |
| 355 |
return False |
| 356 |
|
| •357 |
|
| 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 |
|
| 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 |
|
| 388 |
if parameterValue == None: |
| 389 |
return False |
| 390 |
|
| •391 |
|
| 392 |
if not type(parameterValue) is ListType: |
| 393 |
return False |
| 394 |
|
| •395 |
|
| 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 |
|
| 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 |
|
| 418 |
if parameterValue == None: |
| 419 |
return False |
| 420 |
|
| •421 |
|
| 422 |
if not type(parameterValue) is TupleType: |
| 423 |
return False |
| 424 |
|
| •425 |
|
| 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 |
|
| 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 |
|
| 448 |
if(not ValidateData.funcIsValidList(parameterValue)): |
| 449 |
return False |
| 450 |
|
| 451 |
|
| 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 |
|
| 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 |
|
| 471 |
if(not ValidateData.funcIsValidList(parameterValue)): |
| 472 |
return False |
| 473 |
|
| 474 |
|
| 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 |
|
| 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 |
|
| 494 |
if parameterValue == None: |
| 495 |
return False |
| 496 |
|
| •497 |
|
| 498 |
if not str(type(parameterValue)) == "<type 'numpy.ndarray'>": |
| 499 |
return False |
| 500 |
|
| 501 |
return True |
| 502 |
|
| •503 |
|
| 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 |
|
| 516 |
if parameterValue == None: |
| 517 |
return False |
| 518 |
|
| 519 |
|
| 520 |
if not type(parameterValue) is DictType: |
| 521 |
return False |
| 522 |
|
| 523 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |