Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Here is a list of past and present much-appreciated contributors:
Bruno Soares
Claude Paroz
Daniel Santos
Deepak Ganesh
Egor Osokin
Erik Youngren
Hugo van Kemenade
Expand Down
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## 3.9.0 and newer

### Bugfixes

- Resolve dynamic column callables supplied as row placeholders when appending
rows (#576).

See GitHub Releases:

- https://github.com/jazzband/tablib/releases
Expand Down
8 changes: 7 additions & 1 deletion src/tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,16 @@ def insert(self, index, row, tags=()):
:class:`Dataset` later.
"""

row = list(row)
if len(row) == self.width:
for pos, func in sorted(self._dynamic_columns.items()):
if pos < len(row) and row[pos] is func:
source_row = row[:pos] + row[pos + 1:]
row[pos] = func(source_row)

self._validate(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))

Expand Down
9 changes: 6 additions & 3 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,19 @@ def initials(row):
self.founders.append(('Some', 'One', 71))
# Also acceptable when all dynamic column values are provided.
self.founders.append(('Other', 'Second', 84, 'Other', 'OS'))
# Callable placeholders for dynamic columns are resolved when adding rows.
self.founders.append(('Brad', 'Montgomery', 70, new_col, initials))

self.assertEqual(self.founders[3], ('Some', 'One', 71, 'Some', 'SO'))
self.assertEqual(self.founders[4], ('Other', 'Second', 84, 'Other', 'OS'))
self.assertEqual(self.founders[5], ('Brad', 'Montgomery', 70, 'Brad', 'BM'))
self.assertEqual(
self.founders['first_again'],
['John', 'George', 'Thomas', 'Some', 'Other']
['John', 'George', 'Thomas', 'Some', 'Other', 'Brad']
)
self.assertEqual(
self.founders['initials'],
['JA', 'GW', 'TJ', 'SO', 'OS']
['JA', 'GW', 'TJ', 'SO', 'OS', 'BM']
)

# However only partial dynamic values provided is not accepted.
Expand All @@ -226,7 +229,7 @@ def initials(row):
self.founders.append(('After', 'Deletion', 75))
self.assertEqual(
self.founders['initials'],
['JA', 'GW', 'TJ', 'SO', 'OS', 'AD']
['JA', 'GW', 'TJ', 'SO', 'OS', 'BM', 'AD']
)

def test_header_slicing(self):
Expand Down