Skip to content

Commit

Permalink
Avoid inserting dynamic values when row is already full
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep committed Dec 20, 2023
1 parent 7a34f4a commit ec9c861
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,13 @@ def insert(self, index, row, tags=()):
The default behaviour is to insert the given row to the :class:`Dataset`
object at the given index.
"""
"""

self._validate(row)
for pos, func in self._dynamic_columns.items():
row = list(row)
row.insert(pos, func(row))
if len(row) < self.width:
for pos, func in self._dynamic_columns.items():
row = list(row)
row.insert(pos, func(row))
self._data.insert(index, Row(row, tags=tags))

def rpush(self, row, tags=()):
Expand Down Expand Up @@ -560,7 +561,7 @@ def insert_col(self, index, col=None, header=None):
col = []

# Callable Columns...
if hasattr(col, '__call__'):
if callable(col):
self._dynamic_columns[self.width] = col
col = list(map(col, self._data))

Expand Down
2 changes: 2 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ def initials(row):
# Also acceptable when all dynamic column values are provided.
self.founders.append(('Other', 'Second', 84, 'Other', 'OS'))

self.assertEqual(self.founders[3], ('Some', 'One', 71, 'Some', 'SO'))
self.assertEqual(self.founders[4], ('Other', 'Second', 84, 'Other', 'OS'))
self.assertEqual(
self.founders['first_again'],
['John', 'George', 'Thomas', 'Some', 'Other']
Expand Down

0 comments on commit ec9c861

Please sign in to comment.