Skip to content

Commit

Permalink
Accept generator in writerow
Browse files Browse the repository at this point in the history
A generator has not `__len__` attribute, disallowing supplying one in
`Sheet.writerow()`.

This PR makes the for-loop count the number of cells, so the method does
not need to know the number of cells in advance.
  • Loading branch information
Kijewski committed Jan 11, 2016
1 parent 0e1d454 commit bab53a1
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions odswriter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,7 @@ def __init__(self, dom, name="Sheet 1", cols=None):

def writerow(self, cells):
row = self.dom.createElement("table:table-row")
content_cells = len(cells)

if self.cols is not None:
padding_cells = self.cols - content_cells
if content_cells > self.cols:
raise Exception("More cells than cols.")
cells += [None]*padding_cells
content_cells = 0

for cell_data in cells:
cell = self.dom.createElement("table:table-cell")
Expand Down Expand Up @@ -165,6 +159,16 @@ def writerow(self, cells):

row.appendChild(cell)

content_cells += 1

if self.cols is not None:
if content_cells > self.cols:
raise Exception("More cells than cols.")

for _ in range(content_cells, self.cols):
cell = self.dom.createElement("table:table-cell")
row.appendChild(cell)

self.table.appendChild(row)

def writerows(self, rows):
Expand Down

0 comments on commit bab53a1

Please sign in to comment.