summaryrefslogtreecommitdiff
path: root/tablib
diff options
context:
space:
mode:
authorLuca Beltrame <einar@heavensinferno.net>2010-11-17 22:51:43 +0100
committerLuca Beltrame <einar@heavensinferno.net>2010-11-17 22:51:43 +0100
commitd8136ab6138104583aee6d304eb2d91f809a6ea0 (patch)
tree4bba1a53f68a5394b27e606b2e9382675baf40bc /tablib
parent10ce000d31f57c70753b797342c139e2f37a1872 (diff)
downloadtablib-d8136ab6138104583aee6d304eb2d91f809a6ea0.tar.gz
Whitespace
Diffstat (limited to 'tablib')
-rw-r--r--tablib/core.py130
1 files changed, 65 insertions, 65 deletions
diff --git a/tablib/core.py b/tablib/core.py
index 23026a2..3a80e60 100644
--- a/tablib/core.py
+++ b/tablib/core.py
@@ -77,40 +77,40 @@ class Row(object):
return False
elif isinstance(tag, basestring):
return (tag in self.tags)
- else:
+ else:
return True if len(set(tag) & set(self.tags)) else False
class Dataset(object):
- """The :class:`Dataset` object is the heart of Tablib. It provides all core
+ """The :class:`Dataset` object is the heart of Tablib. It provides all core
functionality.
-
+
Usually you create a :class:`Dataset` instance in your main module, and append
rows and columns as you collect data. ::
-
+
data = tablib.Dataset()
data.headers = ('name', 'age')
-
+
for (name, age) in some_collector():
data.append((name, age))
-
+
You can also set rows and headers upon instantiation. This is useful if dealing
with dozens or hundres of :class:`Dataset` objects. ::
-
+
headers = ('first_name', 'last_name')
data = [('John', 'Adams'), ('George', 'Washington')]
-
+
data = tablib.Dataset(*data, headers=headers)
-
-
+
+
:param \*args: (optional) list of rows to populate Dataset
:param headers: (optional) list strings for Dataset header row
.. admonition:: Format Attributes Definition
- If you look at the code, the various output/import formats are not
- defined within the :class:`Dataset` object. To add support for a new format, see
+ If you look at the code, the various output/import formats are not
+ defined within the :class:`Dataset` object. To add support for a new format, see
:ref:`Adding New Formats <newformats>`.
"""
@@ -118,7 +118,7 @@ class Dataset(object):
def __init__(self, *args, **kwargs):
self._data = list(Row(arg) for arg in args)
self.__headers = None
-
+
# ('title', index) tuples
self._separators = []
@@ -134,7 +134,7 @@ class Dataset(object):
self._register_formats()
-
+
def __len__(self):
return self.height
@@ -153,7 +153,7 @@ class Dataset(object):
else:
return [result.tuple for result in _results]
-
+
def __setitem__(self, key, value):
self._validate(value)
self._data[key] = Row(value)
@@ -163,10 +163,10 @@ class Dataset(object):
if isinstance(key, basestring):
if key in self.headers:
-
+
pos = self.headers.index(key)
del self.headers[pos]
-
+
for i, row in enumerate(self._data):
del row[pos]
@@ -183,7 +183,7 @@ class Dataset(object):
except AttributeError:
return '<dataset object>'
-
+
@classmethod
def _register_formats(cls):
"""Adds format properties."""
@@ -193,7 +193,7 @@ class Dataset(object):
setattr(cls, fmt.title, property(fmt.export_set, fmt.import_set))
except AttributeError:
setattr(cls, fmt.title, property(fmt.export_set))
-
+
except AttributeError:
pass
@@ -234,21 +234,21 @@ class Dataset(object):
def _clean_col(self, col):
"""Prepares the given column for insert/append."""
-
+
col = list(col)
-
+
if self.headers:
header = [col.pop(0)]
else:
header = []
-
+
if len(col) == 1 and callable(col[0]):
col = map(col[0], self._data)
col = tuple(header + col)
-
+
return col
-
+
@property
def height(self):
"""The number of rows currently in the :class:`Dataset`.
@@ -262,7 +262,7 @@ class Dataset(object):
"""The number of columns currently in the :class:`Dataset`.
Cannot be directly modified.
"""
-
+
try:
return len(self._data[0])
except IndexError:
@@ -275,7 +275,7 @@ class Dataset(object):
@property
def headers(self):
"""An *optional* list of strings to be used for header rows and attribute names.
-
+
This must be set manually. The given list length must equal :class:`Dataset.width`.
"""
@@ -297,9 +297,9 @@ class Dataset(object):
@property
def dict(self):
- """A JSON representation of the :class:`Dataset` object. If headers have been
- set, a JSON list of objects will be returned. If no headers have
- been set, a JSON list of lists (rows) will be returned instead.
+ """A JSON representation of the :class:`Dataset` object. If headers have been
+ set, a JSON list of objects will be returned. If no headers have
+ been set, a JSON list of lists (rows) will be returned instead.
A dataset object can also be imported by setting the `Dataset.json` attribute: ::
@@ -309,10 +309,10 @@ class Dataset(object):
"""
return self._package()
-
+
@dict.setter
def dict(self, pickle):
- """A native Python representation of the Dataset object. If headers have been
+ """A native Python representation of the Dataset object. If headers have been
set, a list of Python dictionaries will be returned. If no headers have been
set, a list of tuples (rows) will be returned instead.
@@ -320,7 +320,7 @@ class Dataset(object):
data = tablib.Dataset()
data.dict = [{'age': 90, 'first_name': 'Kenneth', 'last_name': 'Reitz'}]
-
+
"""
if not len(pickle):
return
@@ -330,7 +330,7 @@ class Dataset(object):
self.wipe()
for row in pickle:
self.append(Row(row))
-
+
# if list of objects
elif isinstance(pickle[0], dict):
self.wipe()
@@ -353,11 +353,11 @@ class Dataset(object):
"""
pass
-
+
@property
def csv():
- """A CSV representation of the :class:`Dataset` object. The top row will contain
- headers, if they have been set. Otherwise, the top row will contain
+ """A CSV representation of the :class:`Dataset` object. The top row will contain
+ headers, if they have been set. Otherwise, the top row will contain
the first row of the dataset.
A dataset object can also be imported by setting the :class:`Dataset.csv` attribute. ::
@@ -371,8 +371,8 @@ class Dataset(object):
@property
def tsv():
- """A TSV representation of the :class:`Dataset` object. The top row will contain
- headers, if they have been set. Otherwise, the top row will contain
+ """A TSV representation of the :class:`Dataset` object. The top row will contain
+ headers, if they have been set. Otherwise, the top row will contain
the first row of the dataset.
A dataset object can also be imported by setting the :class:`Dataset.tsv` attribute. ::
@@ -385,9 +385,9 @@ class Dataset(object):
@property
def yaml():
- """A YAML representation of the :class:`Dataset` object. If headers have been
- set, a YAML list of objects will be returned. If no headers have
- been set, a YAML list of lists (rows) will be returned instead.
+ """A YAML representation of the :class:`Dataset` object. If headers have been
+ set, a YAML list of objects will be returned. If no headers have
+ been set, a YAML list of lists (rows) will be returned instead.
A dataset object can also be imported by setting the :class:`Dataset.json` attribute: ::
@@ -398,12 +398,12 @@ class Dataset(object):
"""
pass
-
+
@property
def json():
- """A JSON representation of the :class:`Dataset` object. If headers have been
- set, a JSON list of objects will be returned. If no headers have
- been set, a JSON list of lists (rows) will be returned instead.
+ """A JSON representation of the :class:`Dataset` object. If headers have been
+ set, a JSON list of objects will be returned. If no headers have
+ been set, a JSON list of lists (rows) will be returned instead.
A dataset object can also be imported by setting the :class:`Dataset.json` attribute: ::
@@ -444,18 +444,18 @@ class Dataset(object):
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.
-
- Rows and columns inserted must be the correct size (height or width).
-
+ """Inserts a row or column to the :class:`Dataset` at the given index.
+
+ Rows and columns inserted must be the correct size (height or width).
+
The default behaviour is to insert the given row to the :class:`Dataset`
object at the given index. If the ``col`` parameter is given, however,
a new column will be insert to the :class:`Dataset` object instead.
You can also insert a column of a single callable object, which will
- add a new column with the return values of the callable each as an
+ add a new column with the return values of the callable each as an
item in the column. ::
-
+
data.append(col=random.randint)
See :ref:`dyncols` for an in-depth example.
@@ -469,7 +469,7 @@ class Dataset(object):
If inserting a row, you can add :ref:`tags <tags>` to the row you are inserting.
This gives you the ability to :class:`filter <Dataset.filter>` your
:class:`Dataset` later.
-
+
"""
if row:
self._validate(row)
@@ -477,7 +477,7 @@ class Dataset(object):
elif col:
col = list(col)
- # Callable Columns...
+ # Callable Columns...
if len(col) == 1 and callable(col[0]):
col = map(col[0], self._data)
@@ -489,7 +489,7 @@ class Dataset(object):
if not header:
raise HeadersNeeded()
self.headers.insert(index, header)
-
+
if self.height and self.width:
for i, row in enumerate(self._data):
@@ -501,11 +501,11 @@ class Dataset(object):
def filter(self, tag):
"""Returns a new instance of the :class:`Dataset`, excluding any rows
- that do not contain the given :ref:`tags <tags>`.
+ that do not contain the given :ref:`tags <tags>`.
"""
_dset = copy(self)
_dset._data = [row for row in _dset._data if row.has_tag(tag)]
-
+
return _dset
def transpose(self):
@@ -521,7 +521,7 @@ class Dataset(object):
# The first element of the headers stays in the headers,
# it is our "hinge" on which we rotate the data
new_headers = [self.headers[0]] + self[self.headers[0]]
-
+
_dset.headers = new_headers
for column in self.headers:
@@ -562,7 +562,7 @@ class Databook(object):
"""Removes all :class:`Dataset` objects from the :class:`Databook`."""
self._datasets = []
-
+
@classmethod
def _register_formats(cls):
"""Adds format properties."""
@@ -572,7 +572,7 @@ class Databook(object):
setattr(cls, fmt.title, property(fmt.export_book, fmt.import_book))
except AttributeError:
setattr(cls, fmt.title, property(fmt.export_book))
-
+
except AttributeError:
pass
@@ -583,7 +583,7 @@ class Databook(object):
self._datasets.append(dataset)
else:
raise InvalidDatasetType
-
+
def _package(self):
"""Packages :class:`Databook` for delivery."""
@@ -607,12 +607,12 @@ def detect(stream):
for fmt in formats.available:
try:
if fmt.detect(stream):
- return (fmt, stream)
+ return (fmt, stream)
except AttributeError:
- pass
+ pass
return (None, stream)
-
-
+
+
def import_set(stream):
"""Return dataset of given stream."""
(format, stream) = detect(stream)
@@ -621,7 +621,7 @@ def import_set(stream):
data = Dataset()
format.import_set(data, stream)
return data
-
+
except AttributeError, e:
return None