Package Peach :: Package Generators :: Module data
[hide private]

Source Code for Module Peach.Generators.data

   1   
   2  ''' 
   3  Common data generators.  Includes common bad strings, numbers, etc. 
   4   
   5  @author: Michael Eddington 
   6  @version: $Id: Peach.Generators.data-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
   7  ''' 
   8   
   9  # 
  10  # Copyright (c) 2006-2008 Michael Eddington 
  11  # 
  12  # Permission is hereby granted, free of charge, to any person obtaining a copy  
  13  # of this software and associated documentation files (the "Software"), to deal 
  14  # in the Software without restriction, including without limitation the rights  
  15  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
  16  # copies of the Software, and to permit persons to whom the Software is  
  17  # furnished to do so, subject to the following conditions: 
  18  # 
  19  # The above copyright notice and this permission notice shall be included in     
  20  # all copies or substantial portions of the Software. 
  21  # 
  22  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
  23  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
  24  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
  25  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
  26  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
  27  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  28  # SOFTWARE. 
  29  # 
  30   
  31  # Authors: 
  32  #   Michael Eddington (mike@phed.org) 
  33   
  34  # $Id: Peach.Generators.data-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
  35   
  36  import static 
  37  import sys 
  38  import copy 
  39  import struct 
  40  import random 
  41  import array  
  42  import binascii 
  43   
  44  from Peach.generator                            import * 
  45  from Peach.Generators.dictionary        import * 
  46  from Peach.Generators.static            import * 
  47  from Peach.Generators.data                      import * 
  48  from Peach.Generators.repeater          import * 
  49  from Peach.Generators.block                     import * 
  50  from Peach.group                                        import * 
  51  import Peach.Transformers.type 
  52   
53 -class EndlessRandomStrings(Generator):
54 ''' 55 Generates an endless number of random strings 56 between the lengths of 0 and 1024. 57 ''' 58
59 - def __init__(self, group = None):
60 Generator.__init__(self) 61 self.setGroup(group)
62
63 - def next(self):
64 pass
65
66 - def getRawValue(self):
67 str = '' 68 for x in range(random.choice(range(0, 1024))): 69 str += struct.pack('B', random.randint(0, 255))[0] 70 71 return str
72
73 -class EndlessRandomWideStrings(Generator):
74 ''' 75 Generates an endless number of random strings 76 between the lengths of 0 and 1024. 77 ''' 78
79 - def __init__(self, group = None):
80 Generator.__init__(self) 81 self.setGroup(group)
82
83 - def next(self):
84 pass
85
86 - def getRawValue(self):
87 str = '' 88 89 if random.choice([True, False]): 90 for x in range(random.choice(range(0, 1024))): 91 str += struct.pack('B', random.randint(0, 255))[0] 92 93 if len(str) % 2 != 0: 94 str += '\0' 95 96 else: 97 for x in range(random.choice(range(0, 1024))): 98 str += struct.pack('BB', random.randint(0, 255), 0)[0] 99 100 return str
101
102 -class Bit(SimpleGenerator):
103 ''' 104 Generates 0 and 1 105 '''
106 - def __init__(self, group = None):
107 SimpleGenerator.__init__(self, group) 108 self._generator = List(None, [0, 1])
109 110
111 -class BadStrings(SimpleGenerator):
112 ''' 113 Generates variouse string tests. 114 115 Examples of data generated: 116 117 - Variations on format strings using '%n' 118 - Long string 119 - Empty string 120 - Extended ASCII 121 - Common bad ASCII (' " < >) 122 - All numbers 123 - All letters 124 - All spaces 125 - etc. 126 127 ''' 128 129 _strings = [ 130 'Peach', 131 'abcdefghijklmnopqrstuvwxyz', 132 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 133 '0123456789', 134 '', 135 '10', 136 '0.0', '1.0', 137 '0.1', '1.1.1', 138 '-2,147,483,648', 139 '-2,147,483,649', 140 '2,147,483,647', 141 '2,147,483,649', 142 '-2147483648', 143 '-2147483649', 144 '2147483647', 145 '2147483649', 146 '-129', 147 '129', 148 '255', 149 '256', 150 '-32769', 151 '-32,769', 152 '32767', 153 '32769', 154 '4,294,967,295', 155 '4294967299', 156 '-9,223,372,036,854,775,809', 157 '-9223372036854775809', 158 '9,223,372,036,854,775,809', 159 '9223372036854775809', 160 '18,446,744,073,709,551,615', 161 '18,446,744,073,709,551,619', 162 '18446744073709551619', 163 '2.305843009213693952', 164 '200000000000000000000.5', 165 '200000000000000000000000000000000000000000000.5', 166 '0xFF', 167 '0xFFFF', 168 '0xFFFFFF', 169 '0xFFFFFFFFFFFFFFFFFFFF', 170 'Yes', 171 'No', 172 '%n', 173 '%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n%n', 174 '%n'*1024, 175 '%x', 176 '%x%x%x%x%x%x%x%x', 177 '%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x', 178 '%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x', 179 '%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x', 180 """<>"/\'""", 181 """~`!@#$%^&*()_+=-{}|\][:"';<>?/.,""", 182 '\\"', 183 "\\'", 184 "%", 185 "a%", 186 "%a", 187 "COM1", 188 "COM2", 189 "AUX", 190 "COM1:", 191 "COM2:", 192 "AUX:", 193 "\\\\peach\foo\foo.txt", 194 "\\\\\\", 195 "..\\..\\..\\..\\..\\..\\..\\..\\", 196 "../../../../../", 197 "../", 198 "/../../../../../../", 199 "/../../..", 200 "\\..\\..\\..\\..\\..\\", 201 ";,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,;,", 202 ";,"*256, 203 ";,"*512, 204 ";,"*1024, 205 "A;A,"*256, 206 "A;A,"*512, 207 "A;A,"*1024, 208 ","*256, 209 ","*512, 210 ","*1024, 211 "|"*256, 212 "|"*512, 213 "|"*1024, 214 "||"*256, 215 "||"*512, 216 "||"*1024, 217 ":"*256, 218 ":"*512, 219 ":"*1024, 220 struct.pack('B', 0)[0], 221 struct.pack('B', 1)[0], struct.pack('B', 2)[0], 222 struct.pack('B', 3)[0], struct.pack('B', 4)[0], 223 struct.pack('B', 5)[0], struct.pack('B', 6)[0], 224 struct.pack('B', 7)[0], struct.pack('B', 8)[0], 225 struct.pack('B', 9)[0], struct.pack('B', 10)[0], 226 struct.pack('B', 11)[0], struct.pack('B', 12)[0], 227 struct.pack('B', 13)[0], struct.pack('B', 14)[0], 228 struct.pack('B', 15)[0], struct.pack('B', 16)[0], 229 struct.pack('B', 17)[0], struct.pack('B', 18)[0], 230 struct.pack('B', 19)[0], struct.pack('B', 20)[0], 231 struct.pack('B', 21)[0], struct.pack('B', 22)[0], 232 struct.pack('B', 23)[0], struct.pack('B', 24)[0], 233 struct.pack('B', 25)[0], struct.pack('B', 26)[0], 234 struct.pack('B', 27)[0], struct.pack('B', 28)[0], 235 struct.pack('B', 29)[0], struct.pack('B', 30)[0], 236 struct.pack('B', 31)[0], struct.pack('B', 32)[0], 237 struct.pack('B', 33)[0], struct.pack('B', 34)[0], 238 struct.pack('B', 35)[0], struct.pack('B', 36)[0], 239 struct.pack('B', 37)[0], struct.pack('B', 38)[0], 240 struct.pack('B', 39)[0], struct.pack('B', 40)[0], 241 struct.pack('B', 41)[0], struct.pack('B', 42)[0], 242 struct.pack('B', 43)[0], struct.pack('B', 44)[0], 243 struct.pack('B', 45)[0], struct.pack('B', 46)[0], 244 struct.pack('B', 47)[0], struct.pack('B', 48)[0], 245 struct.pack('B', 49)[0], struct.pack('B', 50)[0], 246 struct.pack('B', 51)[0], struct.pack('B', 52)[0], 247 struct.pack('B', 53)[0], struct.pack('B', 54)[0], 248 struct.pack('B', 55)[0], struct.pack('B', 56)[0], 249 struct.pack('B', 57)[0], struct.pack('B', 58)[0], 250 struct.pack('B', 59)[0], struct.pack('B', 60)[0], 251 struct.pack('B', 61)[0], struct.pack('B', 62)[0], 252 struct.pack('B', 63)[0], struct.pack('B', 64)[0], 253 struct.pack('B', 65)[0], struct.pack('B', 66)[0], 254 struct.pack('B', 67)[0], struct.pack('B', 68)[0], 255 struct.pack('B', 69)[0], struct.pack('B', 70)[0], 256 struct.pack('B', 71)[0], struct.pack('B', 72)[0], 257 struct.pack('B', 73)[0], struct.pack('B', 74)[0], 258 struct.pack('B', 75)[0], struct.pack('B', 76)[0], 259 struct.pack('B', 77)[0], struct.pack('B', 78)[0], 260 struct.pack('B', 79)[0], struct.pack('B', 80)[0], 261 struct.pack('B', 81)[0], struct.pack('B', 82)[0], 262 struct.pack('B', 83)[0], struct.pack('B', 84)[0], 263 struct.pack('B', 85)[0], struct.pack('B', 86)[0], 264 struct.pack('B', 87)[0], struct.pack('B', 88)[0], 265 struct.pack('B', 89)[0], struct.pack('B', 90)[0], 266 struct.pack('B', 91)[0], struct.pack('B', 92)[0], 267 struct.pack('B', 93)[0], struct.pack('B', 94)[0], 268 struct.pack('B', 95)[0], struct.pack('B', 96)[0], 269 struct.pack('B', 97)[0], struct.pack('B', 98)[0], 270 struct.pack('B', 99)[0], struct.pack('B', 100)[0], 271 struct.pack('B', 101)[0], struct.pack('B', 102)[0], 272 struct.pack('B', 103)[0], struct.pack('B', 104)[0], 273 struct.pack('B', 105)[0], struct.pack('B', 106)[0], 274 struct.pack('B', 107)[0], struct.pack('B', 108)[0], 275 struct.pack('B', 109)[0], struct.pack('B', 110)[0], 276 struct.pack('B', 111)[0], struct.pack('B', 112)[0], 277 struct.pack('B', 113)[0], struct.pack('B', 114)[0], 278 struct.pack('B', 115)[0], struct.pack('B', 116)[0], 279 struct.pack('B', 117)[0], struct.pack('B', 118)[0], 280 struct.pack('B', 119)[0], struct.pack('B', 120)[0], 281 struct.pack('B', 121)[0], struct.pack('B', 122)[0], 282 struct.pack('B', 123)[0], struct.pack('B', 124)[0], 283 struct.pack('B', 125)[0], struct.pack('B', 126)[0], 284 struct.pack('B', 127)[0], struct.pack('B', 128)[0], 285 struct.pack('B', 129)[0], struct.pack('B', 130)[0], 286 struct.pack('B', 131)[0], struct.pack('B', 132)[0], 287 struct.pack('B', 133)[0], struct.pack('B', 134)[0], 288 struct.pack('B', 135)[0], struct.pack('B', 136)[0], 289 struct.pack('B', 137)[0], struct.pack('B', 138)[0], 290 struct.pack('B', 139)[0], struct.pack('B', 140)[0], 291 struct.pack('B', 141)[0], struct.pack('B', 142)[0], 292 struct.pack('B', 143)[0], struct.pack('B', 144)[0], 293 struct.pack('B', 145)[0], struct.pack('B', 146)[0], 294 struct.pack('B', 147)[0], struct.pack('B', 148)[0], 295 struct.pack('B', 149)[0], struct.pack('B', 150)[0], 296 struct.pack('B', 151)[0], struct.pack('B', 152)[0], 297 struct.pack('B', 153)[0], struct.pack('B', 154)[0], 298 struct.pack('B', 155)[0], struct.pack('B', 156)[0], 299 struct.pack('B', 157)[0], struct.pack('B', 158)[0], 300 struct.pack('B', 159)[0], struct.pack('B', 160)[0], 301 struct.pack('B', 161)[0], struct.pack('B', 162)[0], 302 struct.pack('B', 163)[0], struct.pack('B', 164)[0], 303 struct.pack('B', 165)[0], struct.pack('B', 166)[0], 304 struct.pack('B', 167)[0], struct.pack('B', 168)[0], 305 struct.pack('B', 169)[0], struct.pack('B', 170)[0], 306 struct.pack('B', 171)[0], struct.pack('B', 172)[0], 307 struct.pack('B', 173)[0], struct.pack('B', 174)[0], 308 struct.pack('B', 175)[0], struct.pack('B', 176)[0], 309 struct.pack('B', 177)[0], struct.pack('B', 178)[0], 310 struct.pack('B', 179)[0], struct.pack('B', 180)[0], 311 struct.pack('B', 181)[0], struct.pack('B', 182)[0], 312 struct.pack('B', 183)[0], struct.pack('B', 184)[0], 313 struct.pack('B', 185)[0], struct.pack('B', 186)[0], 314 struct.pack('B', 187)[0], struct.pack('B', 188)[0], 315 struct.pack('B', 189)[0], struct.pack('B', 190)[0], 316 struct.pack('B', 191)[0], struct.pack('B', 192)[0], 317 struct.pack('B', 193)[0], struct.pack('B', 194)[0], 318 struct.pack('B', 195)[0], struct.pack('B', 196)[0], 319 struct.pack('B', 197)[0], struct.pack('B', 198)[0], 320 struct.pack('B', 199)[0], struct.pack('B', 200)[0], 321 struct.pack('B', 201)[0], struct.pack('B', 202)[0], 322 struct.pack('B', 203)[0], struct.pack('B', 204)[0], 323 struct.pack('B', 205)[0], struct.pack('B', 206)[0], 324 struct.pack('B', 207)[0], struct.pack('B', 208)[0], 325 struct.pack('B', 209)[0], struct.pack('B', 210)[0], 326 struct.pack('B', 211)[0], struct.pack('B', 212)[0], 327 struct.pack('B', 213)[0], struct.pack('B', 214)[0], 328 struct.pack('B', 215)[0], struct.pack('B', 216)[0], 329 struct.pack('B', 217)[0], struct.pack('B', 218)[0], 330 struct.pack('B', 219)[0], struct.pack('B', 220)[0], 331 struct.pack('B', 221)[0], struct.pack('B', 222)[0], 332 struct.pack('B', 223)[0], struct.pack('B', 224)[0], 333 struct.pack('B', 225)[0], struct.pack('B', 226)[0], 334 struct.pack('B', 227)[0], struct.pack('B', 228)[0], 335 struct.pack('B', 229)[0], struct.pack('B', 230)[0], 336 struct.pack('B', 231)[0], struct.pack('B', 232)[0], 337 struct.pack('B', 233)[0], struct.pack('B', 234)[0], 338 struct.pack('B', 235)[0], struct.pack('B', 236)[0], 339 struct.pack('B', 237)[0], struct.pack('B', 238)[0], 340 struct.pack('B', 239)[0], struct.pack('B', 240)[0], 341 struct.pack('B', 241)[0], struct.pack('B', 242)[0], 342 struct.pack('B', 243)[0], struct.pack('B', 244)[0], 343 struct.pack('B', 245)[0], struct.pack('B', 246)[0], 344 struct.pack('B', 247)[0], struct.pack('B', 248)[0], 345 struct.pack('B', 249)[0], struct.pack('B', 250)[0], 346 struct.pack('B', 251)[0], struct.pack('B', 252)[0], 347 struct.pack('B', 253)[0], struct.pack('B', 254)[0], 348 struct.pack('B', 255)[0], 349 ] 350
351 - def __init__(self, group = None):
352 SimpleGenerator.__init__(self, group) 353 354 self._randStrings = [] 355 356 for i in range(100): 357 str = '' 358 for x in range(random.choice(range(10, 100))): 359 str += struct.pack('B', random.randint(1, 255))[0] 360 361 self._randStrings.append(str) 362 363 self._generator = GeneratorList(None, [ 364 List(None, self._strings), 365 List(None, self._randStrings), 366 Repeater(None, Static("A"), 10, 200), 367 Repeater(None, Static("A"), 127, 100), 368 Repeater(None, Static("A"), 1024, 10), 369 Repeater(None, Static("\x41\0"), 10, 200), 370 Repeater(None, Static("\x41\0"), 127, 100), 371 Repeater(None, Static("\x41\0"), 1024, 10), 372 373 Block2([ 374 Static('\0\0'), 375 Static('A'*7000) 376 ]), 377 378 Block2([ 379 Static('%00%00'), 380 Static('A'*7000) 381 ]), 382 383 BadNumbers(), 384 ])
385
386 - def unittest():
387 g = BadString(None) 388 389 if g.getValue() != 'abcdefghijklmnopqrstuvwxyz': 390 raise Exception("BadString unittest failed #1") 391 g.next() 392 393 if g.getValue() != 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': 394 raise Exception("BadString unittest failed #2") 395 396 print "BadString okay\n"
397 unittest = staticmethod(unittest)
398
399 -class BadString(BadStrings):
400 ''' 401 Depricated, use BadStrings instead. 402 ''' 403 pass
404
405 -class BadTime(SimpleGenerator):
406 ''' 407 Test cases for HTTP-Date type 408 '''
409 - def __init__(self, group = None):
410 SimpleGenerator.__init__(self, group) 411 412 groupSeq = [Group(), Group(), Group()] 413 414 self._generator = GeneratorList2(None, [ 415 groupSeq[0], 416 groupSeq[1], 417 groupSeq[2], 418 ],[ 419 420 Block([ 421 GeneratorList(groupSeq[0], [ 422 Static('08'), 423 BadString(), 424 BadNumbers(), 425 Static('08') 426 ]), 427 Static(':01:01') 428 ]), 429 430 Block([ 431 Static('08:'), 432 GeneratorList(groupSeq[1], [ 433 Static('08'), 434 BadString(), 435 BadNumbers(), 436 Static('08') 437 ]), 438 Static(':01') 439 ]), 440 441 Block([ 442 Static('08:01'), 443 GeneratorList(groupSeq[2], [ 444 Static('08'), 445 BadString(), 446 BadNumbers(), 447 Static('08') 448 ]) 449 ]) 450 ])
451 452
453 -class BadDate(SimpleGenerator):
454 ''' 455 [BETA] Generates alot of funky date's. This Generator is still missing 456 alot of test cases. 457 458 - Invalid month, year, day 459 - Mixed up stuff 460 - Crazy odd date formats 461 ''' 462 463 _strings = [ 464 '1/1/1', 465 '0/0/0', 466 '0-0-0', 467 '00-00-00', 468 '-1/-1/-1', 469 'XX/XX/XX', 470 '-1-1-1-1-1-1-1-1-1-1-1-', 471 'Jun 39th 1999', 472 'June -1th 1999', 473 474 # ANSI Date formats 475 '2000', 476 '1997', 477 '0000', 478 '0001', 479 '9999', 480 481 '0000-00', 482 '0000-01', 483 '0000-99', 484 '0000-13', 485 '0001-00', 486 '0001-01', 487 '0001-99', 488 '0001-13', 489 '9999-00', 490 '9999-01', 491 '9999-99', 492 '9999-13', 493 494 '0000-00-00', 495 '0000-01-00', 496 '0000-99-00', 497 '0000-13-00', 498 '0001-00-00', 499 '0001-01-00', 500 '0001-99-00', 501 '0001-13-00', 502 '9999-00-00', 503 '9999-01-00', 504 '9999-99-00', 505 '9999-13-00', 506 '0000-00-01', 507 '0000-01-01', 508 '0000-99-01', 509 '0000-13-01', 510 '0001-00-01', 511 '0001-01-01', 512 '0001-99-01', 513 '0001-13-01', 514 '9999-00-01', 515 '9999-01-01', 516 '9999-99-01', 517 '9999-13-01', 518 '0000-00-99', 519 '0000-01-99', 520 '0000-99-99', 521 '0000-13-99', 522 '0001-00-99', 523 '0001-01-99', 524 '0001-99-99', 525 '0001-13-99', 526 '9999-00-99', 527 '9999-01-99', 528 '9999-99-99', 529 '9999-13-99', 530 ] 531
532 - def __init__(self, group = None):
533 SimpleGenerator.__init__(self, group) 534 self._generator = List(None, self._strings)
535
536 - def unittest():
537 g = BadDate(None) 538 539 if g.getValue() != '1/1/1': 540 raise Exception("BadDate unittest failed #1") 541 g.next() 542 543 if g.getValue() != '0/0/0': 544 raise Exception("BadDate unittest failed #2") 545 546 print "BadDate okay\n"
547 unittest = staticmethod(unittest)
548 549
550 -class NumberLimiter(Generator):
551 ''' 552 Wraps another generator that produces numbers and limits the produced 553 number to a range. If the number produced is outside of this range 554 we will skip it. 555 ''' 556
557 - def __init__(self, group, generator, min, max):
558 ''' 559 Min and max can be used to limit the produced numbers. 560 561 @type group: Group 562 @param group: Group to use 563 @type generator: Generator 564 @param generator: Generatored number to limit 565 @param min: Number 566 @type min: Minimum allowed number 567 @param max: Number 568 @type max: Maximum allowed number 569 ''' 570 571 Generator.__init__(self) 572 self.setGroup(group) 573 574 self._generator = generator 575 self._min = min 576 self._max = max 577 self._lastGood = str(self._min)
578
579 - def _checkAndSkip(self):
580 val = int(self._generator.getValue()) 581 582 while val < self._min or val > self._max: 583 #print "_checkAndSkip: Skipping: ", val 584 self._generator.next() 585 val = int(self._generator.getValue())
586
587 - def next(self):
588 self._generator.next() 589 self._checkAndSkip()
590
591 - def reset(self):
592 self._generator.reset() 593 self._checkAndSkip()
594
595 - def getRawValue(self):
596 val = int(self._generator.getValue()) 597 598 if val < self._min or val > self._max: 599 return self._lastGood 600 601 self._lastGood = str(val) 602 return self._lastGood
603 604
605 -class StringVariance(Generator):
606 ''' 607 Generate a range of string sizes from len(str) - variance to len(str) + variance. 608 ''' 609 610
611 - def __init__(self, group, string, variance, min = None, max = None):
612 ''' 613 Min and max can be used to limit the produced numbers. 614 615 @type group: Group 616 @param group: Group to use 617 @type string: String or Generator 618 @param string: String to vary length of 619 @type variance: + and - change to give length range 620 @param min: Number 621 @type min: Minimum allowed length 622 @param max: Number 623 @type max: Maximum allowed length 624 ''' 625 626 Generator.__init__(self) 627 self.setGroup(group) 628 629 self._number = None 630 self._variance = None 631 self._min = None 632 self._max = None 633 self._current = None 634 635 # Can't create negative strings :) 636 if variance > len(string) and min == None: 637 min = 0 638 639 if min != None and min < 0: 640 raise Exception("Negative string min length???") 641 642 self._string = string 643 self._stringLength = len(string) 644 self._numberVariance = NumberVariance(None, len(string), variance, min, max) 645 self._minAllowed = min 646 self._maxAllowed = max 647 self._current = string[:self._numberVariance.getValue()]
648
649 - def getRawValue(self):
650 return self._current
651
652 - def next(self):
653 self._numberVariance.next() 654 655 # make current value 656 length = self._numberVariance.getValue() 657 if length < self._stringLength: 658 self._current = self._string[:length] 659 else: 660 multiplier = (length/self._stringLength) + 1 661 #print "Multiplier: ",multiplier 662 #print "Target length: ",length 663 val = self._string * multiplier 664 self._current = val[:length]
665
666 - def reset(self):
667 self._numberVariance.reset()
668 669
670 -class NumberVariance(Generator):
671 ''' 672 Generate a range of numbers from (number - variance) to (number + variance). 673 674 Example: 675 676 >>> gen = NumberVariance(None, 10, 5) 677 >>> print gen.getValue() 678 5 679 >>> gen.next() 680 >>> gen.getValue() 681 6 682 >>> gen.next() 683 >>> gen.getValue() 684 7 685 >>> gen.next() 686 >>> gen.getValue() 687 8 688 >>> gen.next() 689 >>> gen.getValue() 690 9 691 >>> gen.next() 692 >>> gen.getValue() 693 10 694 >>> gen.next() 695 >>> gen.getValue() 696 11 697 >>> gen.next() 698 >>> gen.getValue() 699 12 700 >>> gen.next() 701 >>> gen.getValue() 702 13 703 >>> gen.next() 704 >>> gen.getValue() 705 14 706 >>> gen.next() 707 >>> gen.getValue() 708 15 709 ''' 710
711 - def __init__(self, group, number, variance, min = None, max = None):
712 ''' 713 Min and max can be used to limit the produced numbers. 714 715 When using a generator's value will be gotten on the first call to 716 our .getRawValue/getValue methods that occur after a reset(). 717 718 @type group: Group 719 @param group: Group to use 720 @type number: Number or Generator 721 @param number: Number to change 722 @type variance: + and - change to give range 723 @param min: Number 724 @type min: Minimum allowed number 725 @param max: Number 726 @type max: Maximum allowed number 727 ''' 728 729 Generator.__init__(self) 730 self.setGroup(group) 731 732 if str(type(number)) != "<type 'int'>" and str(type(number)) != "<type 'long'>": 733 self._generator = number 734 self._number = None 735 self._isGenerator = True 736 else: 737 self._generator = None 738 self._number = number 739 self._isGenerator = False 740 741 # Max range of values 742 self._variance = long(variance) 743 self._totalVariance = (self._variance * 2) + 1 744 745 # Min and Max value to be generated 746 self._minAllowed = min 747 self._maxAllowed = max 748 749 # Current index into range of values 750 self._current = 0 751 self._currentRange = None 752 753 # Calculate this upfront as well to make sure 754 # our iteration count is correct! 755 if self._isGenerator: 756 num = long(self._generator.getValue()) 757 else: 758 num = long(self._number) 759 760 if (num - self._variance) < (num + self._variance): 761 min = num - self._variance 762 max = num + self._variance 763 else: 764 max = num - self._variance 765 min = num + self._variance 766 767 if self._minAllowed != None and min < self._minAllowed: 768 min = self._minAllowed 769 770 if self._maxAllowed != None and max > self._maxAllowed: 771 max = self._maxAllowed 772 773 self._currentRange = range(min, max)
774
775 - def next(self):
776 self._current += 1 777 if self._current > self._totalVariance: 778 raise GeneratorCompleted("NumberVariance 1") 779 780 if self._currentRange != None and self._current >= len(self._currentRange): 781 raise GeneratorCompleted("NumberVariance 2")
782
783 - def getRawValue(self):
784 # Always get the value from the generator. In the case of 785 # a BlockSize generator this can change when we are recursing 786 787 if self._isGenerator: 788 num = long(self._generator.getValue()) 789 else: 790 num = long(self._number) 791 792 if (num - self._variance) < (num + self._variance): 793 min = num - self._variance 794 max = num + self._variance 795 else: 796 max = num - self._variance 797 min = num + self._variance 798 799 if self._minAllowed != None and min < self._minAllowed: 800 min = self._minAllowed 801 802 if self._maxAllowed != None and max > self._maxAllowed: 803 max = self._maxAllowed 804 805 self._currentRange = range(min, max) 806 807 try: 808 #print "NumberVariance.getRawValue(): [%d-%d:%d:%d] Returning %d" % (min, max, self._current, len(self._currentRange), self._currentRange[self._current]) 809 return str(self._currentRange[self._current]) 810 except: 811 #print "NumberVariance.getRawValue(): Returning %d" % self._currentRange[-1] 812 return str(self._currentRange[-1])
813 814
815 - def reset(self):
816 self._current = 0
817
818 - def unittest():
819 gen = NumberVariance(None, 10, 5) 820 for cnt in range(5, 15): 821 if cnt != gen.getValue(): 822 raise Exception("NumberVariance broken %d != %d" % (cnt, gen.getValue())) 823 824 print "NumberVariance OK!"
825 826 unittest = staticmethod(unittest)
827 828
829 -class NumbersVariance(SimpleGenerator):
830 ''' 831 Performs a L{NumberVariance} on a list of numbers. This is a specialized 832 version of L{NumberVariance} that takes an array of numbers to perform a 833 variance on instead of just a single number. 834 835 Example: 836 837 >>> gen = NumbersVariance(None, [1,10], 1) 838