summaryrefslogtreecommitdiff
path: root/tablib
diff options
context:
space:
mode:
authorKenneth Reitz <me@kennethreitz.com>2011-03-23 00:20:39 -0400
committerKenneth Reitz <me@kennethreitz.com>2011-03-23 00:20:39 -0400
commit8b5b29fc90ca4e222b2f2e9c0ceb6038b53fb0d6 (patch)
tree953acf505f517da266fb80040ec58fc5fc5552e8 /tablib
parente8ba7654263c1f3cddfe63dea3d282a739c9778f (diff)
downloadtablib-8b5b29fc90ca4e222b2f2e9c0ceb6038b53fb0d6.tar.gz
added Dataset.add_formatter
Diffstat (limited to 'tablib')
-rw-r--r--tablib/core.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/tablib/core.py b/tablib/core.py
index 548de55..0915ee3 100644
--- a/tablib/core.py
+++ b/tablib/core.py
@@ -137,6 +137,9 @@ class Dataset(object):
# ('title', index) tuples
self._separators = []
+
+ # (column, callback) tuples
+ self._formatters = []
try:
self.headers = kwargs['headers']
@@ -386,6 +389,7 @@ class Dataset(object):
"""
pass
+
@property
def tsv():
"""A TSV representation of the :class:`Dataset` object. The top row will contain
@@ -470,6 +474,26 @@ class Dataset(object):
self.insert_separator(index, text)
+ def add_formatter(col, handler):
+ """Adds a :ref:`formatter` to the :class:`Dataset`.
+
+ .. versionadded:: 0.9.5
+ :param col: column to. Accepts index int or header str.
+ :param handler: reference to callback function to execute
+ against each cell value.
+ """
+
+ if isinstance(col, basestring):
+ if col in self.headers:
+ col = self.headers.index(key) # get 'key' index from each data
+ else:
+ raise KeyError
+
+ self._formatters.append((col, handler))
+
+ return True
+
+
def insert(self, index, row=None, col=None, header=None, tags=list()):
"""Inserts a row or column to the :class:`Dataset` at the given index.
@@ -659,12 +683,14 @@ class Dataset(object):
return _dset
+
def wipe(self):
"""Removes all content and headers from the :class:`Dataset` object."""
self._data = list()
self.__headers = None
+
class Databook(object):
"""A book of :class:`Dataset` objects.
"""