def bruteforce(func, alphabet, length, method='upto', start=None, databag=None):
 if ((method == 'upto') and (length > 1)):
  iterator = product(alphabet, repeat=1)
  for i in xrange(2, (length + 1)):
   iterator = chain(iterator, product(alphabet, repeat=i))
 elif ((method == 'downfrom') and (length > 1)):
  iterator = product(alphabet, repeat=length)
  for i in xrange((length - 1), 1, (-1)):
   iterator = chain(iterator, product(alphabet, repeat=i))
 elif (method == 'fixed'):
  iterator = product(alphabet, repeat=length)
 else:
  raise TypeError('bruteforce(): unknown method')
 if (method == 'fixed'):
  total_iterations = (len(alphabet) ** length)
 else:
  total_iterations = (((len(alphabet) ** (length + 1)) / (len(alphabet) - 1)) - 1)
 if (start is not None):
  (i, N) = start
  if (i > N):
   raise ValueError('bruteforce(): invalid starting point')
  i -= 1
  chunk_size = (total_iterations / N)
  rest = (total_iterations % N)
  starting_point = 0
  for chunk in range(N):
   if (chunk >= i):
    break
   if (chunk <= rest):
    starting_point += (chunk_size + 1)
   else:
    starting_point += chunk_size
  if (rest >= i):
   chunk_size += 1
  total_iterations = chunk_size
 h = log.waitfor('Bruteforcing')
 cur_iteration = 0
 if (start != None):
  consume(i, iterator)
 for e in iterator:
  cur = ''.join(e)
  cur_iteration += 1
  if ((cur_iteration % 2000) == 0):
   progress = ((100.0 * cur_iteration) / total_iterations)
   h.status(('Trying "%s", %0.3f%%' % (cur, progress)))
   if databag:
    databag['current_item'] = cur
    databag['items_done'] = cur_iteration
    databag['items_total'] = total_iterations
  res = func(cur)
  if res:
   h.success(('Found key: "%s"' % cur))
   return cur
  if (start != None):
   consume((N - 1), iterator)
 h.failure('No matches found')

def bruteforce ( func , alphabet , length , method = 'upto' , start = None , databag = None ) : if ( ( method == 'upto' ) and ( length > 1 ) ) : iterator = product ( alphabet , repeat = 1 ) for i in xrange ( 2 , ( length + 1 ) ) : iterator = chain ( iterator , product ( alphabet , repeat = i ) ) elif ( ( method == 'downfrom' ) and ( length > 1 ) ) : iterator = product ( alphabet , repeat = length ) for i in xrange ( ( length - 1 ) , 1 , ( - 1 ) ) : iterator = chain ( iterator , product ( alphabet , repeat = i ) ) elif ( method == 'fixed' ) : iterator = product ( alphabet , repeat = length ) else : raise TypeError ( 'bruteforce(): unknown method' ) if ( method == 'fixed' ) : total_iterations = ( len ( alphabet ) ** length ) else : total_iterations = ( ( ( len ( alphabet ) ** ( length + 1 ) ) / ( len ( alphabet ) - 1 ) ) - 1 ) if ( start is not None ) : ( i , N ) = start if ( i > N ) : raise ValueError ( 'bruteforce(): invalid starting point' ) i -= 1 chunk_size = ( total_iterations / N ) rest = ( total_iterations % N ) starting_point = 0 for chunk in range ( N ) : if ( chunk >= i ) : break if ( chunk <= rest ) : starting_point += ( chunk_size + 1 ) else : starting_point += chunk_size if ( rest >= i ) : chunk_size += 1 total_iterations = chunk_size h = log . waitfor ( 'Bruteforcing' ) cur_iteration = 0 if ( start != None ) : consume ( i , iterator ) for e in iterator : cur = '' . join ( e ) cur_iteration += 1 if ( ( cur_iteration % 2000 ) == 0 ) : progress = ( ( 100.0 * cur_iteration ) / total_iterations ) h . status ( ( 'Trying "%s", %0.3f%%' % ( cur , progress ) ) ) if databag : databag [ 'current_item' ] = cur databag [ 'items_done' ] = cur_iteration databag [ 'items_total' ] = total_iterations res = func ( cur ) if res : h . success ( ( 'Found key: "%s"' % cur ) ) return cur if ( start != None ) : consume ( ( N - 1 ) , iterator ) h . failure ( 'No matches found' )
bruteforce candidate values to produce the desired index .