Am 03.03.2010 18:38, schrieb Tracubik:
Il Wed, 03 Mar 2010 09:39:54 +0100, Peter Otten ha scritto:

def loop():
     count = 0
     m = 0
     lookup = {1: 1, 2: 10, 3: 100}
     for iterations in range(20): # off by one
         # ...
         print "%2d %1d %3d" % (iterations, count, m) # ...
         if generic_condition():
             count += 1
         # ...
         m = lookup.get(count, m)
         if count == 4:
             break

if __name__ == "__main__":
     loop()
     print "That's all, folks"

Something must be wrong with me today because I find the Pascal code
/more/ readable..

i was think the same, Pascal seem to generate a great more readable code.
I'm a newbie, so my opinion is probably wrong, but i still think that
don't have CASE OF and REPEAT UNTIL code block return in difficult-to-
read code.

That is probably a side-effect of literal code translation.

No one would write something like you did when he writes in python the first place.


>> Tracubik wrote:
>>
>>> hi, i've to convert from Pascal this code:
>>
>> program loop;
>>
>> function generic_condition: boolean;
>> begin
>>     generic_condition := random>  0.7
>> end;
>>
>> procedure loop;
>> var
>>     iterations, count, m: integer;
>> begin
>>     iterations := 0;
>>     count := 0;
>>     m := 0;
>>     repeat
>>        iterations := iterations+1;
>>        (*...*)
>>        writeln(iterations:2, count:2, m:4);
>>        (*...*)
>>        if generic_condition then
>>            inc(count);
>>        (*...*)
>>        case count of
>>        1: m := 1;
>>        2: m := 10;
>>        3: m := 100
>>        end
>>     until (count = 4) or (iterations = 20)
>> end;
>>
>> begin
>>     loop;
>>     writeln("That's all, folks")
>> end.

Hmmm lets see...
We have somewhat obscure and complicated logic.

If we cannot get rid of it, lets hide it:

class Loop:
    def __init__(self, maxiterations=0, maxcount=1, m=0):
        assert generic_condition.__call__
        self.maxiterations = maxiterations
        self.maxcount = maxcount
        self.m=m
        self.count=0
        self.iterations=0

    def __iter__(self):
        while True:
            yield self.next()

    def next(self):
        self.iterations+=1
        if self.iterations > self.maxiterations:
            raise StopIteration
        if generic_condition():
            self.count += 1
            if self.count >= self.maxcount:
                raise StopIteration
            self.m = (None,1,10,100)[self.count]
        return self, self.m

##### So we have:

#from complicatedlogic import Loop
from random import random

def generic_condition():
    return random() > 0.7

if __name__ == '__main__':
    for loop, m in Loop(maxiterations=20, maxcount=4):
        print("%2d %1d %3d" % (loop.iterations, loop.count, m))
    print("That's all, folks")


better? worse? I honestly do not know.

Note that while this is valid py3 and runs as intended, there might be some off-by-one or something left.

Also, this is of course not production level code ;)

Regards,
Michael
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to