mirror of
https://github.com/kennethreitz/tablib.git
synced 2026-06-05 23:10:17 +00:00
Merge branch 'feature/paged' into develop
This commit is contained in:
+62
-18
@@ -14,15 +14,17 @@ import os
|
||||
from helpers import *
|
||||
from packages import simplejson as json
|
||||
from packages import xlwt
|
||||
import random
|
||||
|
||||
|
||||
try:
|
||||
import yaml
|
||||
except ImportError, why:
|
||||
from packages import yaml
|
||||
|
||||
|
||||
|
||||
__all__ = ['Dataset', 'source']
|
||||
|
||||
__all__ = ['Dataset', 'DataBook', 'source']
|
||||
|
||||
__name__ = 'tablib'
|
||||
__version__ = '0.0.4'
|
||||
@@ -43,8 +45,6 @@ class Dataset(object):
|
||||
self._data = None
|
||||
self._saved_file = None
|
||||
self._saved_format = None
|
||||
|
||||
|
||||
self._data = list(args)
|
||||
|
||||
try:
|
||||
@@ -57,11 +57,11 @@ class Dataset(object):
|
||||
except KeyError, why:
|
||||
self.title = None
|
||||
|
||||
|
||||
|
||||
def __len__(self):
|
||||
return self.height
|
||||
|
||||
|
||||
|
||||
def __getitem__(self, key):
|
||||
if is_string(key):
|
||||
if key in self.headers:
|
||||
@@ -81,7 +81,7 @@ class Dataset(object):
|
||||
def __delitem__(self, key):
|
||||
del self._data[key]
|
||||
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
if self.title:
|
||||
return '<%s dataset>' % (self.title.lower())
|
||||
@@ -131,13 +131,13 @@ class Dataset(object):
|
||||
except KeyError, why:
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
"""Returns JSON representation of Dataset."""
|
||||
return json.dumps(self._package())
|
||||
|
||||
|
||||
|
||||
@property
|
||||
def yaml(self):
|
||||
"""Returns YAML representation of Dataset."""
|
||||
@@ -152,7 +152,7 @@ class Dataset(object):
|
||||
|
||||
for row in self._package(dicts=False):
|
||||
_csv.writerow(row)
|
||||
|
||||
|
||||
return stream.getvalue()
|
||||
|
||||
|
||||
@@ -160,7 +160,7 @@ class Dataset(object):
|
||||
def xls(self):
|
||||
"""Returns XLS representation of Dataset."""
|
||||
stream = cStringIO.StringIO()
|
||||
|
||||
|
||||
wb = xlwt.Workbook()
|
||||
ws = wb.add_sheet(self.title if self.title else 'Tabbed Dataset')
|
||||
# for row in self._package(dicts=False):
|
||||
@@ -171,12 +171,13 @@ class Dataset(object):
|
||||
wb.save(stream)
|
||||
return stream.getvalue()
|
||||
|
||||
|
||||
|
||||
def append(self, row):
|
||||
"""Adds a row to the end of Dataset"""
|
||||
self._validate(row)
|
||||
self._data.append(tuple(row))
|
||||
|
||||
|
||||
def index(self, i, row):
|
||||
"""Inserts a row at given position in Dataset"""
|
||||
self._validate(row)
|
||||
@@ -187,32 +188,75 @@ class Dataset(object):
|
||||
# todo: accpept string if headers, or index nubmer
|
||||
pass
|
||||
|
||||
|
||||
def save(self, filename=None, format=None):
|
||||
"""Saves dataset"""
|
||||
if not format:
|
||||
format = filename.split('.')[-1].lower() # set format from filename
|
||||
|
||||
|
||||
if format not in FILE_EXTENSIONS:
|
||||
raise UnsupportedFormat
|
||||
|
||||
|
||||
|
||||
# note export format
|
||||
# open file, save the bitch
|
||||
|
||||
|
||||
|
||||
def export(self):
|
||||
"""Exports Dataset to given filename or file-object."""
|
||||
pass
|
||||
|
||||
|
||||
class DataBook(object):
|
||||
"""A book of Dataset objects.
|
||||
Currently, this exists only for XLS workbook support.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._datasets = []
|
||||
|
||||
|
||||
def add_book(self, dataset):
|
||||
"""Add given ."""
|
||||
if type(dataset) is Dataset:
|
||||
self._datasets.append(dataset)
|
||||
else:
|
||||
raise InvalidDatasetType
|
||||
|
||||
@property
|
||||
def size(self):
|
||||
"""The number of the Datasets within DataBook."""
|
||||
return len(self._datasets)
|
||||
|
||||
|
||||
@property
|
||||
def xls(self):
|
||||
"""Returns XLS representation of DataBook."""
|
||||
|
||||
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 row in self._package(dicts=False):
|
||||
for i, row in enumerate(dset._package(dicts=False)):
|
||||
for j, col in enumerate(row):
|
||||
ws.write(i, j, col)
|
||||
|
||||
wb.save(stream)
|
||||
return stream.getvalue()
|
||||
|
||||
|
||||
|
||||
class InvalidDatasetType(Exception):
|
||||
"Only Datasets can be added to a DataBook"
|
||||
|
||||
class InvalidDimensions(Exception):
|
||||
"Invalid size"
|
||||
|
||||
|
||||
class UnsupportedFormat(NotImplementedError):
|
||||
"Format is not supported"
|
||||
|
||||
|
||||
|
||||
def source(src=None, file=None, filename=None):
|
||||
"""docstring for import"""
|
||||
|
||||
+31
-8
@@ -10,17 +10,40 @@ data = [
|
||||
|
||||
data = tablib.Dataset(*data, headers=headers)
|
||||
|
||||
#print data[1]
|
||||
data.append(['kenneth' ,'reitz', 4.3])
|
||||
data.append(['Kenneth' ,'Reitz', 4.3])
|
||||
|
||||
#print '***WITH HEADERS***'
|
||||
|
||||
#print data.digest()
|
||||
#print 'First Names:\n',
|
||||
#print data['first_name']
|
||||
|
||||
#print '\nYAML:'
|
||||
#print data.yaml
|
||||
#
|
||||
#print 'JSON:'
|
||||
#print data.json
|
||||
#
|
||||
#print '\nCSV:'
|
||||
#print data.csv
|
||||
#
|
||||
#
|
||||
#print '***AND WITHOUT HEADERS***'
|
||||
#
|
||||
#data.headers = None
|
||||
#
|
||||
#print '\nYAML:'
|
||||
#print data.yaml
|
||||
#
|
||||
#print 'JSON:'
|
||||
#print data.json
|
||||
#
|
||||
#print '\nCSV:'
|
||||
#print data.csv
|
||||
|
||||
book = tablib.DataBook()
|
||||
book.add_book(data)
|
||||
book.add_book(data)
|
||||
|
||||
print book.xls
|
||||
|
||||
|
||||
data.headers = None
|
||||
print data.csv
|
||||
#print len(data.xls)
|
||||
print data.yaml
|
||||
print data.json
|
||||
Reference in New Issue
Block a user