Changeset 2876
- Timestamp:
- 12/06/13 17:11:11 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/python/selector.py
r2875 r2876 361 361 pattern = '' 362 362 if idx >= 0: 363 if idx < 10: 364 num_digits = 1 365 else: 366 num_digits = int(math.log10(idx)) + 1 367 numerics = [] 368 modulo = 10 369 divider = 1 370 for i in xrange(num_digits): 371 numerics.append(int((idx % modulo) / divider)) 372 modulo *= 10 373 divider *= 10 363 numerics = map(int,list(str(idx))) 364 #numerics.reverse() 365 num_digits = len(numerics) 374 366 #print 'numerics=',numerics 375 367 if num_digits == 1: … … 379 371 pattern = '[0-%s]'%(numerics[0]) 380 372 elif num_digits == 2: 381 pattern_list = ['[0-9]'] 382 pattern_list.append('[1-%s][0-9]'%(numerics[1]-1)) 383 if numerics[0] == 0: 384 pattern_list.append('%s%s'%(numerics[1],numerics[0])) 385 else: 386 pattern_list.append('%s[0-%s]'%(numerics[1],numerics[0])) 387 pattern = '(%s)'%('|'.join(pattern_list)) 373 pattern = '(%s)'%('|'.join( 374 list(self.__gen_two_digit_pattern(numerics)))) 388 375 elif num_digits == 3: 389 pattern_list = ['[0-9]','[1-9][0-9]'] 390 if numerics[2] == 2: 391 pattern_list.append('1[0-9][0-9]') 392 elif numerics[2] > 2: 393 pattern_list.append('[1-%s][0-9][0-9]'%(numerics[2]-1)) 394 if numerics[1] == 0: 395 if numerics[0] == 0: 396 pattern_list.append('%s00'%(numerics[2])) 397 else: 398 pattern_list.append('%s0[0-%s]'%(numerics[2],numerics[0])) 399 else: 400 pattern_list.append('%s[0-%s][0-9]'%(numerics[2],numerics[1]-1)) 401 if numerics[0] == 0: 402 pattern_list.append('%s%s%s'%(numerics[2],numerics[1],numerics[0])) 403 else: 404 pattern_list.append('%s%s[0-%s]'%(numerics[2],numerics[1],numerics[0])) 405 pattern = '(%s)'%('|'.join(pattern_list)) 376 pattern = '(%s)'%('|'.join( 377 list(self.__gen_three_digit_pattern(numerics)))) 406 378 else: 407 379 raise RuntimeError('ID > 999 is not supported') 408 pattern = ''409 380 else: 410 381 raise RuntimeError('ID must be >= 0') 411 382 return pattern 383 384 def __gen_two_digit_pattern(self, numerics): 385 assert len(numerics) == 2 386 yield '[0-9]' 387 if numerics[0] == 2: 388 yield '1[0-9]' 389 elif numerics[0] > 2: 390 yield '[1-%s][0-9]'%(numerics[0]-1) 391 if numerics[1] == 0: 392 yield '%s%s'%(numerics[0],numerics[1]) 393 else: 394 yield '%s[0-%s]'%(numerics[0],numerics[1]) 395 396 def __gen_three_digit_pattern(self, numerics): 397 assert len(numerics) == 3 398 yield '[0-9]' 399 yield '[1-9][0-9]' 400 if numerics[0] == 2: 401 yield '1[0-9][0-9]' 402 elif numerics[0] > 2: 403 yield '[1-%s][0-9][0-9]'%(numerics[0]-1) 404 if numerics[1] == 0: 405 if numerics[2] == 0: 406 yield '%s00'%(numerics[0]) 407 else: 408 yield '%s0[0-%s]'%(numerics[0],numerics[2]) 409 else: 410 if numerics[1] > 1: 411 yield '%s[0-%s][0-9]'%(numerics[0],numerics[1]-1) 412 elif numerics[1] == 1: 413 yield '%s0[0-9]'%(numerics[0]) 414 if numerics[0] == 0: 415 yield '%s%s%s'%(numerics[0],numerics[1],numerics[2]) 416 else: 417 yield '%s%s[0-%s]'%(numerics[0],numerics[1],numerics[2])
Note:
See TracChangeset
for help on using the changeset viewer.