diff options
| author | Kenneth Reitz <me@kennethreitz.com> | 2011-03-23 00:32:56 -0400 |
|---|---|---|
| committer | Kenneth Reitz <me@kennethreitz.com> | 2011-03-23 00:32:56 -0400 |
| commit | 3803a7a21b68633b0e54c6c01824647973ffa95d (patch) | |
| tree | ee713960164472e46ce45a6c4063e801b55cd013 /tablib | |
| parent | 8b5b29fc90ca4e222b2f2e9c0ceb6038b53fb0d6 (diff) | |
| download | tablib-3803a7a21b68633b0e54c6c01824647973ffa95d.tar.gz | |
formatter execution in place upon export
Diffstat (limited to 'tablib')
| -rw-r--r-- | tablib/core.py | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/tablib/core.py b/tablib/core.py index 0915ee3..6c17bc3 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -240,13 +240,26 @@ class Dataset(object): def _package(self, dicts=True): """Packages Dataset into lists of dictionaries for transmission.""" + _data = list(self._data) + + # Execute formatters + if self._formatters: + for row_i, row in enumerate(_data): + for col, callback in self._formatters: + if col is None: + for j, c in enumerate(row): + _data[row_i][j] = callback(c) + else: + _data[row_i][col] = callback(row[col]) + + if self.headers: if dicts: - data = [OrderedDict(zip(self.headers, data_row)) for data_row in self ._data] + data = [OrderedDict(zip(self.headers, data_row)) for data_row in _data] else: - data = [list(self.headers)] + list(self._data) + data = [list(self.headers)] + list(_data) else: - data = [list(row) for row in self._data] + data = [list(row) for row in _data] return data @@ -474,7 +487,7 @@ class Dataset(object): self.insert_separator(index, text) - def add_formatter(col, handler): + def add_formatter(self, col, handler): """Adds a :ref:`formatter` to the :class:`Dataset`. .. versionadded:: 0.9.5 |
