Package install :: Package MoSTBioDat :: Package DataBase :: Package ImportData :: Package Validator :: Module TypeValidator
[hide private]
[frames] | no frames]

Source Code for Module install.MoSTBioDat.DataBase.ImportData.Validator.TypeValidator

  1  #!/usr/bin/env python 
  2  ################################ 
  3  # TypeValidator.py             # 
  4  # MySQL/Python type validator  # 
  5  ################################ 
  6   
  7  ###################################################### 
  8  # Copyright (c) 2007-2008 Andrzej Bak                # 
  9  # ARC Seibersdorf & University of Silesia            # 
 10  # Author: Andrzej Bak <Andrzej.Bak@us.edu.pl>        # 
 11  # License: GNU General Public License, version: 3    # 
 12  # URL: http://chemoinformatyka.us.edu.pl/mostbiodat/ # 
 13  # Version: 1, 06.01.2010                             # 
 14  ###################################################### 
 15   
 16  try: 
 17      import sys 
 18      import os 
 19      from time import strptime 
 20  except ImportError,e: 
 21      print 'Error: %s' %e 
 22      sys.exit(1) 
 23  ############ Type class Validator  ################### 
24 -class TypeValidator(object):
25 """ 26 Type validator 27 INPUT: 28 invalue - str, input value 29 colobj - column object 30 OUTPUT: 31 boolean 32 """
33 - def __init__(self,invalue,colobj=None):
34 self.invalue=invalue#input value 35 self.colobj=colobj#column object 36 if not isinstance(self.invalue,str): 37 print 'Error: Input not in string format!' 38 sys.exit(1)
39
40 - def INT(self):
41 """ 42 Validate INT 43 INPUT: 44 class object 45 OUPTUT: 46 bolean 47 """ 48 deflength=11 49 length=self.colobj.getLength() 50 maxsigned=2147483647 51 minsigned=-2147483648 52 maxunsigned=4294967295 53 minunsigned=0 54 try: 55 intval=int(self.invalue) 56 except ValueError, e: 57 print 'Error: %s' %e 58 return False 59 else: 60 if length=='default': 61 length=deflength 62 63 if len(self.invalue)>length:#input value greater than define in DataBase 64 return False 65 66 if self.colobj.getUnsigned():#if integer below zero and flag unsigned is set 67 return intval in inRange(minunsigned,maxunsigned) 68 else: 69 return intval in inRange(minsigned,maxsigned)
70
71 - def MEDIUMINT(self):
72 """ 73 Validate MEDIUMINT 74 INPUT: 75 class object 76 OUPTUT: 77 boolean 78 """ 79 deflength=8 80 length=self.colobj.getLength() 81 maxsigned=8388607 82 minsigned=-8388606 83 maxunsigned=16777215 84 minunsigned=0 85 try: 86 intval=int(self.invalue) 87 except ValueError, e: 88 print 'Error: %s' %e 89 return False 90 else: 91 if length=='default': 92 length=deflength 93 94 if len(self.invalue)>length:#input value greater than define in DataBase 95 return False 96 97 if self.colobj.getUnsigned():#if integer below zero and flag unsigned is set 98 return intval in inRange(minunsigned,maxunsigned) 99 else: 100 return intval in inRange(minsigned,maxsigned)
101
102 - def SMALLINT(self):
103 """ 104 Validate SMALLINT 105 INPUT: 106 class object 107 OUPTUT: 108 boolean 109 """ 110 deflength=6 111 length=self.colobj.getLength() 112 maxsigned=32767 113 minsigned=-32768 114 maxunsigned=65535 115 minunsigned=0 116 try: 117 intval=int(self.invalue) 118 except ValueError, e: 119 print 'Error: %s' %e 120 return False 121 else: 122 if length=='default': 123 length=deflength 124 125 if len(self.invalue)>length:#input value greater than define in DataBase 126 return False 127 128 if self.colobj.getUnsigned():#if integer below zero and flag unsigned is set 129 return intval in inRange(minunsigned,maxunsigned) 130 else: 131 return intval in inRange(minsigned,maxsigned)
132
133 - def TINYINT(self):
134 """ 135 Validate TINYINT 136 INPUT: 137 class object 138 OUPTUT: 139 boolean 140 """ 141 deflength=5 142 length=self.colobj.getLength() 143 maxsigned=127 144 minsigned=-128 145 maxunsigned=255 146 minunsigned=0 147 try: 148 intval=int(self.invalue) 149 except ValueError, e: 150 print 'Error: %s' %e 151 return False 152 else: 153 if length=='default': 154 length=deflength 155 156 if len(self.invalue)>length:#input value greater than define in DataBase 157 return False 158 159 if self.colobj.getUnsigned():#if integer below zero and flag unsigned is set 160 return intval in inRange(minunsigned,maxunsigned) 161 else: 162 return intval in inRange(minsigned,maxsigned)
163
164 - def BIGINT(self):
165 """ 166 Validate BIGINT 167 INPUT: 168 class object 169 OUPTUT: 170 boolean 171 """ 172 deflength=20 173 length=self.colobj.getLength() 174 maxsigned=9223372036854775807 175 minsigned=-9223372036854775808 176 maxunsigned=18446744073709551615 177 minunsigned=0 178 try: 179 intval=long(self.invalue) 180 except ValueError, e: 181 print 'Error: %s' %e 182 return False 183 else: 184 if length=='default': 185 length=deflength 186 187 if len(self.invalue)>length:#input value greater than define in DataBase 188 return False 189 190 if self.colobj.getUnsigned():#if integer below zero and flag unsigned is set 191 return intval in inRange(minunsigned,maxunsigned) 192 else: 193 return intval in inRange(minsigned,maxsigned)
194
195 - def VARCHAR(self):
196 """ 197 Validate VARCHAR 198 INPUT: 199 class object 200 OUPTUT: 201 boolean 202 """ 203 deflength=255 204 length=self.colobj.getLength() 205 try: 206 intval=str(self.invalue) 207 except ValueError, e: 208 print 'Error: %s' %e 209 return False 210 else: 211 if length=='default': 212 length=deflength 213 214 if self.invalue.isspace():return False 215 216 if len(self.invalue)>length:#input value greater than define in DataBase 217 return False 218 else: 219 return True
220 221 CHAR=VARCHAR 222 TINYTEXT=VARCHAR 223 TINYBLOB=VARCHAR 224
225 - def TEXT(self):
226 """ 227 Validate TEXT 228 INPUT: 229 class object 230 OUPTUT: 231 boolean 232 """ 233 deflength=2**16-1 234 length=self.colobj.getLength() 235 try: 236 intval=str(self.invalue) 237 except ValueError, e: 238 print 'Error: %s' %e 239 return False 240 else: 241 if length=='default': 242 length=deflength 243 244 if self.invalue.isspace():return False 245 246 if len(self.invalue)>length:#input value greater than define in DataBase 247 return False 248 else: 249 return True
250 251 BLOB=TEXT 252
253 - def MEDIUMTEXT(self):
254 """ 255 Validate MEDIUMTEXT 256 INPUT: 257 class object 258 OUPTUT: 259 boolean: 260 """ 261 deflength=2**24-1 262 length=self.colobj.getLength() 263 try: 264 intval=str(self.invalue) 265 except ValueError, e: 266 print 'Error: %s' %e 267 return False 268 else: 269 if length=='default': 270 length=deflength 271 if self.invalue.isspace():return False 272 if len(self.invalue)>length:#input value greater than define in DataBase 273 return False 274 else: 275 return True
276 277 MEDIUMBLOB=MEDIUMTEXT 278
279 - def LONGTEXT(self):
280 """ 281 Validate LONGTEXT 282 INPUT: 283 class object 284 OUPTUT: 285 boolean 286 """ 287 deflength=2**32-1 288 length=self.colobj.getLength() 289 try: 290 intval=str(self.invalue) 291 except ValueError, e: 292 print 'Error: %s' %e 293 return False 294 else: 295 if length=='default': 296 length=deflength 297 if self.invalue.isspace():return False 298 if len(self.invalue)>length:#input value greater than define in DataBase 299 return False 300 else: 301 return True
302 303 LONGBLOB=MEDIUMTEXT 304
305 - def DATE(self):
306 """ 307 Validate DATE 308 INPUT: 309 class object 310 OUPTUT: 311 boolean 312 """ 313 fmt='%H:%M:%S' 314 try: 315 strptime(self.invalue,fmt) 316 except ValueError,e: 317 return False 318 else: 319 return True
320 321 TIME=DATE 322
323 - def TIMESTAMP(self):
324 """ 325 Validate DATE 326 INPUT: 327 class object 328 OUPTUT: 329 boolean 330 """ 331 fmt='%Y-%m-%d %H:%M:%S' 332 try: 333 strptime(self.invalue,fmt) 334 except ValueError,e: 335 return False 336 else: 337 return True
338
339 - def DATETIME(self):
340 """ 341 Validate DATETIME 342 INPUT: 343 class object 344 OUPTUT: 345 boolean 346 """ 347 fmt='%Y-%m-%d %H:%M:%S' 348 try: 349 strptime(self.invalue,fmt) 350 except ValueError,e: 351 return False 352 else: 353 return True
354
355 - def YEAR(self):
356 """ 357 Validate YEAR 358 INPUT: 359 class object 360 OUPTUT: 361 boolean 362 """ 363 fmt='%Y' 364 try: 365 strptime(self.invalue,fmt) 366 except ValueError,e: 367 return False 368 else: 369 return True
370
371 - def FLOAT(self):
372 """ 373 Validate FLOAT 374 INPUT: 375 class object 376 OUPTUT: 377 boolean 378 """ 379 deflength=16 380 defM=7 381 defD=7 382 383 length=self.colobj.getLength() 384 fpoint=self.colobj.getFpoint() 385 386 try: 387 floatval=float(self.invalue) 388 except ValueError, e: 389 print 'Error: %s' %e 390 return False 391 else: 392 if length=='default': 393 length=deflength 394 395 if fpoint=='default': 396 fpoint=defD 397 398 # if len(self.invalue)>length:#input value greater than define in DataBase 399 # return False 400 401 if self.colobj.getUnsigned(): 402 if floatval<0: 403 return False 404 return True
405 406 DOUBLE=FLOAT 407 REAL=FLOAT 408 DECIMAL=FLOAT 409 DEC=DECIMAL
410 ######### Class inRange ##############
411 -class inRange:
412 """ 413 Range class 414 INPUT: 415 low - low range value 416 high - high range value 417 OUTPUT: 418 boolean 419 """ 420
421 - def __init__(self, low, high):
422 self.low = low 423 self.high = high
424
425 - def __contains__(self, obj):
426 """ 427 INPUT: 428 class object 429 OUTPUT: 430 boolean 431 """ 432 return self.low < obj <self.high
433 434 ########### End of class ########################## 435 ############## MAIN ############################### 436 ############# example of usage #################### 437 if __name__=='__main__': 438 pass 439