[Tutor] please return flys in ointment

Dave Angel davea at davea.name
Sun Jul 7 04:09:43 CEST 2013


On 07/06/2013 09:18 PM, Jim Mooney wrote:
> On 6 July 2013 18:01, ALAN GAULD <alan.gauld at btinternet.com> wrote:
>>
>>
>> If its structured use a parser for preference. (HTML, XML or pyparser etc
>> for
>> proprietary type stuff
>>
>
> Ah, I see - don't bother with a parser unless you have a lot of structure.
>
> BTW, I extracted the main function from my program to a test program and it
> gets an index error after 6,000 or so tests of random integer strings. in
> case someone finds that, I'm already workin' on it ;')
>
> Jim
>
>

If the exception was on the line:

         first, second, third, last_two = (triplet[0], triplet[1],
triplet[2], triplet[1:])

then perhaps you want to apply this fix to the leftpad logic:


def make_triplets_from_input(numbers_str):
     leftpad = "0" * ((-len(numbers_str))%3)
     numbers_str = leftpad + numbers_str
     triplets = [numbers_str[x:x+3] for x in range(0,len(numbers_str),3)]
     return triplets

Testing with the following,

for j in xrange(100000000):
     i = random.randint(0, 10**12)
     triplets = make_triplets_from_input(str(i))
     print(j, i, numbers_to_name(triplets))

After some 800,000 values, I killed it, and put a conditional print 
instead, printing every ten-thousandth value.  (It still calls 
numbers_to_name(), obviously.)  It has done the full hundred million 
random values without crashing.

If you'd like, I'll post my full version, changed as little as possible 
from yours, but incorporating the suggestions I've tried to make here.

In another message, you had the following:


 > As a start, where I print Zero and exit, it seemed natural since
 > a user entering Zero is, I assume, asking for what they want,
 > and they get Zero, finissimo. But I guess I should be generous
 > and figure they mistyped, then loop for better input.
 > I think I already asked (or maybe not) if more than
 > one exit is a good idea, though.

You miss my point.  The  get_raw_input() function should permit a value 
of "0" but it should NOT just print a value and abort the program.  It 
breaks all reasonable factoring.  Conversely, the numbers_to_name() 
function SHOULD take a value of zero (which will actually be ["000"] ) 
and at that point, it's fine to special case.  Not by printing "Zero", 
but by returning "zero".



-- 
DaveA



More information about the Tutor mailing list