mirror of
https://github.com/kennethreitz/tablib.git
synced 2026-06-05 23:10:17 +00:00
Added data.append(col=[]) support.
This commit is contained in:
+50
-12
@@ -15,6 +15,7 @@ import simplejson as json
|
||||
import xlwt
|
||||
import yaml
|
||||
|
||||
import showme
|
||||
from helpers import *
|
||||
|
||||
|
||||
@@ -80,10 +81,15 @@ class Dataset(object):
|
||||
return '<dataset object>'
|
||||
|
||||
|
||||
def _validate(self, row=None, safety=False):
|
||||
def _validate(self, row=None, col=None, safety=False):
|
||||
"""Assures size of every row in dataset is of proper proportions."""
|
||||
if row:
|
||||
is_valid = (len(row) == self.width) if self.width else True
|
||||
elif col:
|
||||
if self.headers:
|
||||
is_valid = (len(col) - 1) == self.height
|
||||
else:
|
||||
is_valid = (len(col) == self.height) if self.height else True
|
||||
else:
|
||||
is_valid = all((len(x)== self.width for x in self._data))
|
||||
|
||||
@@ -130,17 +136,20 @@ class Dataset(object):
|
||||
"""Headers property."""
|
||||
return self.__headers
|
||||
|
||||
|
||||
@headers.setter
|
||||
def headers(self, collection):
|
||||
"""Validating headers setter."""
|
||||
self._validate(collection)
|
||||
self.__headers = collection
|
||||
|
||||
|
||||
|
||||
@property
|
||||
def dict(self):
|
||||
"""Returns python dict of Dataset."""
|
||||
return self._package()
|
||||
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
"""Returns JSON representation of Dataset."""
|
||||
@@ -179,17 +188,37 @@ class Dataset(object):
|
||||
wb.save(stream)
|
||||
return stream.getvalue()
|
||||
|
||||
|
||||
def append(self, row):
|
||||
# @showme.trace
|
||||
def append(self, row=None, col=None):
|
||||
"""Adds a row to the end of Dataset"""
|
||||
self._validate(row)
|
||||
self._data.append(tuple(row))
|
||||
if row:
|
||||
self._validate(row)
|
||||
self._data.append(tuple(row))
|
||||
elif col:
|
||||
self._validate(col=col)
|
||||
|
||||
if self.headers:
|
||||
# pop the first item off, add to headers
|
||||
self.headers.append(col[0])
|
||||
col = col[1:]
|
||||
|
||||
if self.height and self.width:
|
||||
|
||||
for i, row in enumerate(self._data):
|
||||
_row = list(row)
|
||||
_row.append(col[i])
|
||||
self._data[i] = tuple(_row)
|
||||
else:
|
||||
self._data = [tuple([row]) for row in col]
|
||||
|
||||
|
||||
def index(self, i, row):
|
||||
def insert(self, i, row=None, col=None):
|
||||
"""Inserts a row at given position in Dataset"""
|
||||
self._validate(row)
|
||||
self._data.insert(i, tuple(row))
|
||||
if row:
|
||||
self._validate(row)
|
||||
self._data.insert(i, tuple(row))
|
||||
elif col:
|
||||
pass
|
||||
|
||||
|
||||
class DataBook(object):
|
||||
@@ -200,12 +229,14 @@ class DataBook(object):
|
||||
def __init__(self, sets=[]):
|
||||
self._datasets = sets
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
try:
|
||||
return '<%s databook>' % (self.title.lower())
|
||||
except AttributeError:
|
||||
return '<databook object>'
|
||||
|
||||
|
||||
def add_sheet(self, dataset):
|
||||
"""Add given dataset ."""
|
||||
if type(dataset) is Dataset:
|
||||
@@ -213,6 +244,7 @@ class DataBook(object):
|
||||
else:
|
||||
raise InvalidDatasetType
|
||||
|
||||
|
||||
def _package(self):
|
||||
collector = []
|
||||
for dset in self._datasets:
|
||||
@@ -222,6 +254,7 @@ class DataBook(object):
|
||||
))
|
||||
return collector
|
||||
|
||||
|
||||
@property
|
||||
def size(self):
|
||||
"""The number of the Datasets within DataBook."""
|
||||
@@ -235,8 +268,8 @@ class DataBook(object):
|
||||
stream = cStringIO.StringIO()
|
||||
wb = xlwt.Workbook()
|
||||
|
||||
for dset in self._datasets:
|
||||
ws = wb.add_sheet(dset.title if dset.title else 'Tabbed Dataset %s' % (int(random.random() * 100000000)))
|
||||
for i, dset in enumerate(self._datasets):
|
||||
ws = wb.add_sheet(dset.title if dset.title else 'Sheet%s' % (i))
|
||||
|
||||
#for row in self._package(dicts=False):
|
||||
for i, row in enumerate(dset._package(dicts=False)):
|
||||
@@ -246,24 +279,29 @@ class DataBook(object):
|
||||
wb.save(stream)
|
||||
return stream.getvalue()
|
||||
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
"""Returns JSON representation of Databook."""
|
||||
|
||||
return json.dumps(self._package())
|
||||
|
||||
|
||||
@property
|
||||
def yaml(self):
|
||||
"""Returns YAML representation of Databook."""
|
||||
|
||||
return yaml.dump(self._package())
|
||||
|
||||
|
||||
|
||||
|
||||
class InvalidDatasetType(Exception):
|
||||
"Only Datasets can be added to a DataBook"
|
||||
|
||||
|
||||
class InvalidDimensions(Exception):
|
||||
"Invalid size"
|
||||
|
||||
|
||||
class UnsupportedFormat(NotImplementedError):
|
||||
"Format is not supported"
|
||||
|
||||
Reference in New Issue
Block a user