#NEWLINE# def auto_ranges( lower , upper , max_items ) : #NEWLINE# #INDENT# if ( lower == upper ) : #NEWLINE# #INDENT# return [ ( lower , upper ) ] #NEWLINE# #UNINDENT# assert ( lower < upper ) #NEWLINE# input_type = type ( lower ) #NEWLINE# if ( input_type is float ) : #NEWLINE# #INDENT# lower = str ( lower ) #NEWLINE# upper = str ( upper ) #NEWLINE# #UNINDENT# lower_d = Decimal ( lower ) #NEWLINE# upper_d = Decimal ( upper ) #NEWLINE# step = ( ( upper_d - lower_d ) / max_items ) #NEWLINE# st = step . as_tuple ( ) #NEWLINE# exponent_offset = len ( st . digits ) #NEWLINE# if ( ( exponent_offset + st . exponent ) > 0 ) : #NEWLINE# #INDENT# exponent_offset = ( - st . exponent ) #NEWLINE# #UNINDENT# zeros = ( [ 0 ] * ( ( len ( st . digits ) - 1 ) - exponent_offset ) ) #NEWLINE# candidate_steps = [ Decimal ( DecimalTuple ( sign = st . sign , digits = ( [ d ] + zeros ) , exponent = ( st . exponent + exponent_offset ) ) ) for d in ( 1 , 2 , 5 ) ] #NEWLINE# candidate_steps . append ( Decimal ( DecimalTuple ( sign = st . sign , digits = ( [ 1 , 0 ] + zeros ) , exponent = ( st . exponent + exponent_offset ) ) ) ) #NEWLINE# for c_step in candidate_steps : #NEWLINE# #INDENT# lower_r = ( round_dec_down ( ( lower_d / c_step ) ) * c_step ) #NEWLINE# upper_r = ( round_dec_up ( ( upper_d / c_step ) ) * c_step ) #NEWLINE# num_steps = int ( round_dec ( ( ( upper_r - lower_r ) / c_step ) ) ) #NEWLINE# if ( num_steps <= max_items ) : #NEWLINE# #INDENT# ranges = [ ] #NEWLINE# for i in xrange ( num_steps ) : #NEWLINE# #INDENT# lower_i = input_type ( ( lower_r + ( c_step * i ) ) ) #NEWLINE# upper_i = input_type ( ( lower_r + ( c_step * ( i + 1 ) ) ) ) #NEWLINE# if ( i == ( num_steps - 1 ) ) : #NEWLINE# #INDENT# upper_i = input_type ( upper_r ) #NEWLINE# #UNINDENT# ranges . append ( ( lower_i , upper_i ) ) #NEWLINE# #UNINDENT# return ranges #NEWLINE# #UNINDENT# #UNINDENT# assert False , "Can't find a candidate set of ranges, logic error"