Re: [Tutor] don't repeat yourself; question about code optimization CORRECTION

2007-07-29 Thread Bob Gailer
[EMAIL PROTECTED] wrote:
>
>
> On 7/23/07, *Bob Gailer* <[EMAIL PROTECTED] 
> > wrote:
>
> A correction to the code at the end. The test of
> self.total_num_of_items
> should precede the pop(0)
>
>
>
> Bob, I spent today studying what you've been telling me and I put the 
> finishing touches to make your code pass my battery of tests.  It 
> still is repetitive, i.e., table.append(tuple(row)), but I've found 
> guidance in what Alan had to say about code readability and easy 
> maintenance, and am no longer going to press the matter:
>
> class Table_Creator(object):
> def __init__(self, total_num_of_items, max_num_of_items_per_row):
> self.given_items = range(total_num_of_items)
> self.max_num_of_items_per_row = max_num_of_items_per_row
>
> def create_grid(self):
> table = []
> while True:
> row = []
> while len(row) < self.max_num_of_items_per_row:
> if not self.given_items:
> if row:
> table.append(tuple(row))
> return table
> row.append(self.given_items.pop(0))
> table.append(tuple(row))
A bit more juggling leads to more simplification:

def create_grid(self):
table = []
while self.given_items:
row = []
while len(row) < self.max_num_of_items_per_row and 
self.given_items:
row.append(self.given_items.pop(0))
table.append(tuple(row))
return table

Testing self.given_items twice is unavoidable.

-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] don't repeat yourself; question about code optimization CORRECTION

2007-07-29 Thread Eric Brunson
[EMAIL PROTECTED] wrote:
>
>
> On 7/23/07, *Bob Gailer* <[EMAIL PROTECTED] 
> > wrote:
>
> A correction to the code at the end. The test of
> self.total_num_of_items
> should precede the pop(0)
>
>
>
> Bob, I spent today studying what you've been telling me and I put the 
> finishing touches to make your code pass my battery of tests.  It 
> still is repetitive, i.e., table.append(tuple(row)),

I see you're also repeating the use of "=" quite a bit, as well as 
multiple uses of the word "if".  Maybe you could work on that.

Sorry for the sarcasm, but there's a big different between repeating an 
assignment and a type coersion versus a multi line block of code that 
could be converted to a function.  :-)

> but I've found guidance in what Alan had to say about code readability 
> and easy maintenance, and am no longer going to press the matter:
>
> class Table_Creator(object):
> def __init__(self, total_num_of_items, max_num_of_items_per_row):
> self.given_items = range(total_num_of_items)
> self.max_num_of_items_per_row = max_num_of_items_per_row
>
> def create_grid(self):
> table = []
> while True:
> row = []
> while len(row) < self.max_num_of_items_per_row:
> if not self.given_items:
> if row:
> table.append(tuple(row))
> return table
> row.append(self.given_items.pop(0))
> table.append(tuple(row))
>  
>
>
> 
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] don't repeat yourself; question about code optimization CORRECTION

2007-07-29 Thread tpc247
On 7/23/07, Bob Gailer <[EMAIL PROTECTED]> wrote:
>
> A correction to the code at the end. The test of self.total_num_of_items
> should precede the pop(0)



Bob, I spent today studying what you've been telling me and I put the
finishing touches to make your code pass my battery of tests.  It still is
repetitive, i.e., table.append(tuple(row)), but I've found guidance in what
Alan had to say about code readability and easy maintenance, and am no
longer going to press the matter:

class Table_Creator(object):
def __init__(self, total_num_of_items, max_num_of_items_per_row):
self.given_items = range(total_num_of_items)
self.max_num_of_items_per_row = max_num_of_items_per_row

def create_grid(self):
table = []
while True:
row = []
while len(row) < self.max_num_of_items_per_row:
if not self.given_items:
if row:
table.append(tuple(row))
return table
row.append(self.given_items.pop(0))
table.append(tuple(row))
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] don't repeat yourself; question about code optimization CORRECTION

2007-07-23 Thread Bob Gailer
A correction to the code at the end. The test of self.total_num_of_items 
should precede the pop(0)

[EMAIL PROTECTED] wrote:
>
>
> On 7/20/07, *Bob Gailer* <[EMAIL PROTECTED] 
> > wrote:
>
> Take advantage of slicing:
>def create_grid(self):
>table = []
>for i in range(0, len( self.total_num_of_items),
> self.max_num_of_items_per_row):
>  table.append(tuple(self.total_num_of_items[i : i +
> self.max_num_of_items_per_row ]))
>return table
>
>
> simply amazing.  Thank you.
One of the many "shifts" one makes in adjusting to "new" language
features. I made such a shift in 1974 when I switched from FORTRAN and
PL/I to APL.
>
> OK - to address your original question:
>
> def create_grid(self):
> table = []
> while self.total_num_of_items:
> row = []
> count = 0
> while count < self.max_num_of_items_per_row and
> self.total_num_of_items:
> row.append(self.total_num_of_items.pop(0))
> count += 1
> table.append(tuple(row))
> return table
>
>
> At first I  regarded you with quiet awe
We gurus bask in attention.
> , but then the nitpick in me saw the two "while 
> self.total_num_of_item" statements, and I was less pleased.
Oh all right, that costs us one more statement. Independently we can get
rid of count:

def create_grid(self):
table = []
while True:
row = []
while len(row) < self.max_num_of_items_per_row:
if not self.total_num_of_items:
return table
row.append(self.total_num_of_items.pop(0))
table.append(tuple(row))


-- 
Bob Gailer
510-978-4454 Oakland, CA
919-636-4239 Chapel Hill, NC



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor