mirror of
https://github.com/kennethreitz/tablib.git
synced 2026-06-05 15:00:19 +00:00
Compare commits
59 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6407afba3e | |||
| 7aada68952 | |||
| 5ba92b0f6b | |||
| 9f26c23eb5 | |||
| 8136f4b09e | |||
| 7e7ad73ddd | |||
| f889910629 | |||
| 969d9d957d | |||
| 86d84b555d | |||
| 66867527d2 | |||
| 7505d8d985 | |||
| d5515c17b8 | |||
| 07ac723971 | |||
| 5d7843ea59 | |||
| b5f0cf9d37 | |||
| a73bbe1645 | |||
| f1bdf43aab | |||
| 7623bfe7b0 | |||
| 59ccc0b422 | |||
| 99154aa6d6 | |||
| 65836d5ace | |||
| 4117503ed5 | |||
| dfa26a7d53 | |||
| 4f035caf1b | |||
| a9c7a5067d | |||
| 80cb42e8dd | |||
| 8d7e5732cd | |||
| 942dd3dadf | |||
| b1d282744c | |||
| 4c0c879d65 | |||
| cab63e02c8 | |||
| 63d025888a | |||
| 5a993ac281 | |||
| 666dd1d2c7 | |||
| ac1666e3ae | |||
| 5b7e817db2 | |||
| f9c168e4bc | |||
| 82f3d84c7d | |||
| 121cf46aec | |||
| 4bb4a05bcb | |||
| e52b8dd329 | |||
| 93fb89b8b6 | |||
| c01b66a16a | |||
| c3fa29a166 | |||
| 8d6a52aaf5 | |||
| 703b1da04c | |||
| 0e6bd079cc | |||
| 579dbf0cc0 | |||
| fbabb430ca | |||
| b8f923f8c5 | |||
| fbe6fe1612 | |||
| 17e90e71e5 | |||
| dc21825f34 | |||
| 7364995eaa | |||
| 3407170b99 | |||
| dd13744c92 | |||
| 31e4c39762 | |||
| 4fc70957ac | |||
| fbcc3b60af |
@@ -10,4 +10,4 @@ Development Lead
|
||||
Patches and Suggestions
|
||||
```````````````````````
|
||||
|
||||
- A Lucky Someone
|
||||
- Luke Lee
|
||||
+19
@@ -1,6 +1,25 @@
|
||||
History
|
||||
=======
|
||||
|
||||
0.8.1 (2010-09-28)
|
||||
------------------
|
||||
* Packaging Fix
|
||||
|
||||
|
||||
0.8.0 (2010-09-25)
|
||||
------------------
|
||||
* New format plugin system!
|
||||
* Imports! ELEGANT Imports!
|
||||
* Tests. Lots of tests.
|
||||
|
||||
|
||||
0.7.1 (2010-09-20)
|
||||
------------------
|
||||
|
||||
* Reverting methods back to properties.
|
||||
* Windows bug compenated in documentation.
|
||||
|
||||
|
||||
0.7.0 (2010-09-20)
|
||||
------------------
|
||||
|
||||
|
||||
+59
-15
@@ -15,15 +15,27 @@ Tablib is a format-agnostic tabular dataset library, written in Python.
|
||||
|
||||
Output formats supported:
|
||||
|
||||
- Excel
|
||||
- JSON
|
||||
- YAML
|
||||
- CSV
|
||||
- Excel (Sets + Books)
|
||||
- JSON (Sets + Books)
|
||||
- YAML (Sets + Books)
|
||||
- CSV (Sets)
|
||||
|
||||
At this time, Tablib supports the **export** of it's powerful Dataset object instances into any of the above formats. Import is underway.
|
||||
Import formats supported:
|
||||
|
||||
- JSON (Sets + Books)
|
||||
- YAML (Sets + Books)
|
||||
- CSV (Sets)
|
||||
|
||||
Note that tablib *purposefully* excludes XML support. It always will.
|
||||
|
||||
Overview
|
||||
--------
|
||||
|
||||
`tablib.Dataset()`
|
||||
A Dataset is a table of tabular data. It may or may not have a header row. They can be build and maniuplated as raw Python datatypes (Lists of tuples|dictonaries). Datasets can be imported from JSON, YAML, and CSV; they can be exported to Excel (XLS), JSON, YAML, and CSV.
|
||||
|
||||
`tablib.Databook()`
|
||||
A Databook is a set of Datasets. The most common form of a Databook is an Excel file with multiple spreadsheets. Databooks can be imported from JSON and YAML; they can be exported to Excel (XLS), JSON, and YAML.
|
||||
|
||||
Usage
|
||||
-----
|
||||
@@ -64,13 +76,16 @@ Easily delete rows: ::
|
||||
|
||||
>>> del data[1]
|
||||
|
||||
Exports
|
||||
-------
|
||||
|
||||
Drumroll please...........
|
||||
|
||||
JSON!
|
||||
+++++
|
||||
::
|
||||
|
||||
>>> print data.json()
|
||||
>>> print data.json
|
||||
[
|
||||
{
|
||||
"last_name": "Adams",
|
||||
@@ -89,7 +104,7 @@ YAML!
|
||||
+++++
|
||||
::
|
||||
|
||||
>>> print data.yaml()
|
||||
>>> print data.yaml
|
||||
- {age: 90, first_name: John, last_name: Adams}
|
||||
- {age: 83, first_name: Henry, last_name: Ford}
|
||||
|
||||
@@ -97,7 +112,7 @@ CSV...
|
||||
++++++
|
||||
::
|
||||
|
||||
>>> print data.csv()
|
||||
>>> print data.csv
|
||||
first_name,last_name,age
|
||||
John,Adams,90
|
||||
Henry,Ford,83
|
||||
@@ -106,11 +121,44 @@ EXCEL!
|
||||
++++++
|
||||
::
|
||||
|
||||
>>> data.xls('people.xls')
|
||||
>>> open('people.xls', 'wb').write(data.xls)
|
||||
|
||||
It's that easy.
|
||||
|
||||
Imports!
|
||||
--------
|
||||
|
||||
JSON
|
||||
++++
|
||||
|
||||
::
|
||||
|
||||
>>> data.json = '[{"last_name": "Adams","age": 90,"first_name": "John"}]'
|
||||
>>> print data[0]
|
||||
('John', 'Adams', 90)
|
||||
|
||||
|
||||
YAML
|
||||
++++
|
||||
::
|
||||
|
||||
>>> data.yaml = '- {age: 90, first_name: John, last_name: Adams}'
|
||||
>>> print data[0]
|
||||
('John', 'Adams', 90)
|
||||
|
||||
CSV
|
||||
+++
|
||||
::
|
||||
|
||||
>>> data.yaml = 'age, first_name, last_name\n90, John, Adams'
|
||||
>>> print data[0]
|
||||
('John', 'Adams', 90)
|
||||
|
||||
|
||||
>>> print data.yaml
|
||||
- {age: 90, first_name: John, last_name: Adams}
|
||||
|
||||
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
@@ -131,14 +179,10 @@ If you'd like to contribute, simply fork `the repository`_, commit your changes
|
||||
|
||||
Roadmap
|
||||
-------
|
||||
- Add ability to add/remove full columns
|
||||
- Import datasets from CSV, JSON, YAML
|
||||
- Release CLI Interface
|
||||
- Auto-detect import format
|
||||
- Add possible other exports (SQL?)
|
||||
- Possibly plugin-ify format architecture
|
||||
- Ability to assign types to rows (set, regex=, &c.)
|
||||
- Plugin support
|
||||
|
||||
.. _`the repository`: http://github.com/kennethreitz/tablib
|
||||
.. _AUTHORS: http://github.com/kennethreitz/tablib/blob/master/AUTHORS
|
||||
.. _AUTHORS: http://github.com/kennethreitz/tablib/blob/master/AUTHORS
|
||||
|
||||
@@ -11,21 +11,20 @@ def publish():
|
||||
"""Publish to PyPi"""
|
||||
os.system("python setup.py sdist upload")
|
||||
|
||||
|
||||
if sys.argv[-1] == "publish":
|
||||
publish()
|
||||
sys.exit()
|
||||
|
||||
setup(
|
||||
name='tablib',
|
||||
version='0.7.0',
|
||||
version='0.8.1',
|
||||
description='Format agnostic tabular data library (XLS, JSON, YAML, CSV)',
|
||||
long_description=open('README.rst').read() + '\n\n' +
|
||||
open('HISTORY.rst').read(),
|
||||
author='Kenneth Reitz',
|
||||
author_email='me@kennethreitz.com',
|
||||
url='http://github.com/kennethreitz/tablib',
|
||||
packages=['tablib'],
|
||||
packages=['tablib', 'tablib.formats'],
|
||||
install_requires=['xlwt', 'simplejson', 'PyYAML'],
|
||||
license='MIT',
|
||||
classifiers=(
|
||||
|
||||
+8
-1
@@ -1 +1,8 @@
|
||||
from core import *
|
||||
""" Tablib.
|
||||
"""
|
||||
|
||||
from tablib.core import (
|
||||
Databook, Dataset, InvalidDatasetType,
|
||||
InvalidDimensions, UnsupportedFormat
|
||||
)
|
||||
|
||||
|
||||
+70
-106
@@ -1,28 +1,14 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# _____ ______ ______ _________
|
||||
# __ /_______ ____ /_ ___ /_ _____ ______ /
|
||||
# _ __/_ __ `/__ __ \__ __ \_ _ \_ __ /
|
||||
# / /_ / /_/ / _ /_/ /_ /_/ // __// /_/ /
|
||||
# \__/ \__,_/ /_.___/ /_.___/ \___/ \__,_/
|
||||
""" Tablib - Core Library.
|
||||
"""
|
||||
|
||||
from tablib.formats import FORMATS as formats
|
||||
|
||||
|
||||
import csv
|
||||
import StringIO
|
||||
import random
|
||||
|
||||
import simplejson as json
|
||||
import xlwt
|
||||
import yaml
|
||||
|
||||
from helpers import *
|
||||
|
||||
|
||||
# __all__ = ['Dataset', 'DataBook']
|
||||
|
||||
__name__ = 'tablib'
|
||||
__version__ = '0.7.0'
|
||||
__build__ = 0x000700
|
||||
__title__ = 'tablib'
|
||||
__version__ = '0.8.1'
|
||||
__build__ = 0x000801
|
||||
__author__ = 'Kenneth Reitz'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright 2010 Kenneth Reitz'
|
||||
@@ -32,22 +18,20 @@ class Dataset(object):
|
||||
"""Epic Tabular-Dataset object. """
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self._data = None
|
||||
self._saved_file = None
|
||||
self._saved_format = None
|
||||
self._data = list(args)
|
||||
self.__headers = None
|
||||
|
||||
try:
|
||||
self.headers = kwargs['headers']
|
||||
except KeyError, why:
|
||||
except KeyError:
|
||||
self.headers = None
|
||||
|
||||
try:
|
||||
self.title = kwargs['title']
|
||||
except KeyError, why:
|
||||
except KeyError:
|
||||
self.title = None
|
||||
|
||||
self._register_formats()
|
||||
|
||||
def __len__(self):
|
||||
return self.height
|
||||
@@ -79,6 +63,20 @@ class Dataset(object):
|
||||
except AttributeError:
|
||||
return '<dataset object>'
|
||||
|
||||
|
||||
@classmethod
|
||||
def _register_formats(cls):
|
||||
"""Adds format properties."""
|
||||
for fmt in formats:
|
||||
try:
|
||||
try:
|
||||
setattr(cls, fmt.title, property(fmt.export_set, fmt.import_set))
|
||||
except AttributeError:
|
||||
setattr(cls, fmt.title, property(fmt.export_set))
|
||||
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
||||
def _validate(self, row=None, col=None, safety=False):
|
||||
"""Assures size of every row in dataset is of proper proportions."""
|
||||
@@ -113,6 +111,7 @@ class Dataset(object):
|
||||
|
||||
return data
|
||||
|
||||
|
||||
@property
|
||||
def height(self):
|
||||
"""Returns the height of the Dataset."""
|
||||
@@ -124,12 +123,13 @@ class Dataset(object):
|
||||
"""Returns the width of the Dataset."""
|
||||
try:
|
||||
return len(self._data[0])
|
||||
except IndexError, why:
|
||||
except IndexError:
|
||||
try:
|
||||
return len(self.headers)
|
||||
except TypeError, e:
|
||||
except TypeError:
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
@property
|
||||
def headers(self):
|
||||
"""Headers property."""
|
||||
@@ -143,7 +143,7 @@ class Dataset(object):
|
||||
if collection:
|
||||
try:
|
||||
self.__headers = list(collection)
|
||||
except TypeError, why:
|
||||
except TypeError:
|
||||
raise TypeError
|
||||
else:
|
||||
self.__headers = None
|
||||
@@ -154,45 +154,21 @@ class Dataset(object):
|
||||
"""Returns python dict of Dataset."""
|
||||
return self._package()
|
||||
|
||||
|
||||
def json(self):
|
||||
"""Returns JSON representation of Dataset."""
|
||||
return json.dumps(self.dict)
|
||||
|
||||
|
||||
def yaml(self):
|
||||
"""Returns YAML representation of Dataset."""
|
||||
return yaml.dump(self.dict)
|
||||
|
||||
|
||||
def csv(self):
|
||||
"""Returns CSV representation of Dataset."""
|
||||
stream = StringIO.StringIO()
|
||||
_csv = csv.writer(stream)
|
||||
|
||||
for row in self._package(dicts=False):
|
||||
_csv.writerow(row)
|
||||
|
||||
return stream.getvalue()
|
||||
|
||||
|
||||
def xls(self, path=None):
|
||||
"""Returns XLS representation of Dataset."""
|
||||
|
||||
wb = xlwt.Workbook(encoding='utf8')
|
||||
ws = wb.add_sheet(self.title if self.title else 'Tabbed Dataset')
|
||||
|
||||
for i, row in enumerate(self._package(dicts=False)):
|
||||
for j, col in enumerate(row):
|
||||
ws.write(i, j, col)
|
||||
|
||||
if path:
|
||||
wb.save(path)
|
||||
return True
|
||||
|
||||
@dict.setter
|
||||
def dict(self, pickle):
|
||||
"""Returns python dict of Dataset."""
|
||||
if not len(pickle):
|
||||
return
|
||||
if isinstance(pickle[0], list):
|
||||
for row in pickle:
|
||||
self.append(row)
|
||||
elif isinstance(pickle[0], dict):
|
||||
self.headers = pickle[0].keys()
|
||||
for row in pickle:
|
||||
self.append(row.values())
|
||||
else:
|
||||
stream = StringIO.StringIO()
|
||||
wb.save(stream)
|
||||
return stream.getvalue()
|
||||
raise UnsupportedFormat
|
||||
|
||||
|
||||
def append(self, row=None, col=None):
|
||||
@@ -225,6 +201,12 @@ class Dataset(object):
|
||||
self._data.insert(i, tuple(row))
|
||||
elif col:
|
||||
pass
|
||||
|
||||
|
||||
def wipe(self):
|
||||
"""Erases all data from Dataset."""
|
||||
self._data = list()
|
||||
self.__headers = None
|
||||
|
||||
|
||||
class Databook(object):
|
||||
@@ -234,6 +216,7 @@ class Databook(object):
|
||||
|
||||
def __init__(self, sets=[]):
|
||||
self._datasets = sets
|
||||
self._register_formats()
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
@@ -242,9 +225,26 @@ class Databook(object):
|
||||
except AttributeError:
|
||||
return '<databook object>'
|
||||
|
||||
def wipe(self):
|
||||
"""Wipe book clean."""
|
||||
self._datasets = []
|
||||
|
||||
@classmethod
|
||||
def _register_formats(cls):
|
||||
"""Adds format properties."""
|
||||
for fmt in formats:
|
||||
try:
|
||||
try:
|
||||
setattr(cls, fmt.title, property(fmt.export_book, fmt.import_book))
|
||||
except AttributeError:
|
||||
setattr(cls, fmt.title, property(fmt.export_book))
|
||||
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
||||
def add_sheet(self, dataset):
|
||||
"""Add given dataset ."""
|
||||
"""Adds given dataset."""
|
||||
if type(dataset) is Dataset:
|
||||
self._datasets.append(dataset)
|
||||
else:
|
||||
@@ -252,6 +252,7 @@ class Databook(object):
|
||||
|
||||
|
||||
def _package(self):
|
||||
"""Packages Databook for delivery."""
|
||||
collector = []
|
||||
for dset in self._datasets:
|
||||
collector.append(dict(
|
||||
@@ -267,43 +268,6 @@ class Databook(object):
|
||||
return len(self._datasets)
|
||||
|
||||
|
||||
def xls(self, path=None):
|
||||
"""Returns XLS representation of DataBook."""
|
||||
|
||||
|
||||
wb = xlwt.Workbook(encoding='utf8')
|
||||
|
||||
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)):
|
||||
for j, col in enumerate(row):
|
||||
ws.write(i, j, col)
|
||||
|
||||
|
||||
|
||||
if path:
|
||||
wb.save(path)
|
||||
return True
|
||||
else:
|
||||
stream = cStringIO.StringIO()
|
||||
wb.save(stream)
|
||||
return stream.getvalue()
|
||||
|
||||
|
||||
def json(self):
|
||||
"""Returns JSON representation of Databook."""
|
||||
|
||||
return json.dumps(self._package())
|
||||
|
||||
|
||||
def yaml(self):
|
||||
"""Returns YAML representation of Databook."""
|
||||
|
||||
return yaml.dump(self._package())
|
||||
|
||||
|
||||
|
||||
class InvalidDatasetType(Exception):
|
||||
"Only Datasets can be added to a DataBook"
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
""" Tablib - formats
|
||||
"""
|
||||
|
||||
import _csv as csv
|
||||
import _json as json
|
||||
import _xls as xls
|
||||
import _yaml as yaml
|
||||
|
||||
FORMATS = (csv, json, xls, yaml)
|
||||
@@ -0,0 +1,42 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
""" Tablib - CSV Support.
|
||||
"""
|
||||
|
||||
import cStringIO
|
||||
import csv
|
||||
import os
|
||||
|
||||
import simplejson as json
|
||||
|
||||
import tablib
|
||||
|
||||
|
||||
title = 'csv'
|
||||
extentions = ('csv',)
|
||||
|
||||
|
||||
|
||||
def export_set(dataset):
|
||||
"""Returns CSV representation of Dataset."""
|
||||
stream = cStringIO.StringIO()
|
||||
_csv = csv.writer(stream)
|
||||
|
||||
for row in dataset._package(dicts=False):
|
||||
_csv.writerow(row)
|
||||
|
||||
return stream.getvalue()
|
||||
|
||||
|
||||
def import_set(dset, in_stream, headers=True):
|
||||
"""Returns dataset from CSV stream."""
|
||||
|
||||
dset.wipe()
|
||||
|
||||
rows = csv.reader(in_stream.split())
|
||||
for i, row in enumerate(rows):
|
||||
|
||||
if (i == 0) and (headers):
|
||||
dset.headers = row
|
||||
else:
|
||||
dset.append(row)
|
||||
@@ -0,0 +1,38 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
""" Tablib - JSON Support
|
||||
"""
|
||||
|
||||
import simplejson as json
|
||||
import tablib.core
|
||||
|
||||
title = 'json'
|
||||
extentions = ('json', 'jsn')
|
||||
|
||||
|
||||
def export_set(dataset):
|
||||
"""Returns JSON representation of Dataset."""
|
||||
return json.dumps(dataset.dict)
|
||||
|
||||
|
||||
def export_book(databook):
|
||||
"""Returns JSON representation of Databook."""
|
||||
return json.dumps(databook._package())
|
||||
|
||||
|
||||
def import_set(dset, in_stream):
|
||||
"""Returns dataset from JSON stream."""
|
||||
|
||||
dset.wipe()
|
||||
dset.dict = json.loads(in_stream)
|
||||
|
||||
|
||||
def import_book(dbook, in_stream):
|
||||
"""Returns databook from JSON stream."""
|
||||
|
||||
dbook.wipe()
|
||||
for sheet in json.loads(in_stream):
|
||||
data = tablib.core.Dataset()
|
||||
data.title = sheet['title']
|
||||
data.dict = sheet['data']
|
||||
dbook.add_sheet(data)
|
||||
@@ -0,0 +1,45 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
""" Tablib - XLS Support.
|
||||
"""
|
||||
|
||||
import xlwt
|
||||
import cStringIO
|
||||
|
||||
|
||||
title = 'xls'
|
||||
extentions = ('xls',)
|
||||
|
||||
|
||||
def export_set(dataset):
|
||||
"""Returns XLS representation of Dataset."""
|
||||
|
||||
wb = xlwt.Workbook(encoding='utf8')
|
||||
ws = wb.add_sheet(dataset.title if dataset.title else 'Tabbed Dataset')
|
||||
|
||||
for i, row in enumerate(dataset._package(dicts=False)):
|
||||
for j, col in enumerate(row):
|
||||
ws.write(i, j, col)
|
||||
|
||||
stream = cStringIO.StringIO()
|
||||
wb.save(stream)
|
||||
return stream.getvalue()
|
||||
|
||||
|
||||
def export_book(databook):
|
||||
"""Returns XLS representation of DataBook."""
|
||||
|
||||
wb = xlwt.Workbook(encoding='utf8')
|
||||
|
||||
for i, dset in enumerate(databook._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)):
|
||||
for j, col in enumerate(row):
|
||||
ws.write(i, j, col)
|
||||
|
||||
|
||||
stream = cStringIO.StringIO()
|
||||
wb.save(stream)
|
||||
return stream.getvalue()
|
||||
@@ -0,0 +1,42 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
""" Tablib - YAML Support.
|
||||
"""
|
||||
|
||||
import yaml
|
||||
import tablib
|
||||
|
||||
|
||||
|
||||
title = 'yaml'
|
||||
extentions = ('yaml', 'yml')
|
||||
|
||||
|
||||
|
||||
def export_set(dataset):
|
||||
"""Returns YAML representation of Dataset."""
|
||||
return yaml.dump(dataset.dict)
|
||||
|
||||
|
||||
def export_book(databook):
|
||||
"""Returns YAML representation of Databook."""
|
||||
return yaml.dump(databook._package())
|
||||
|
||||
|
||||
def import_set(dset, in_stream):
|
||||
"""Returns dataset from YAML stream."""
|
||||
|
||||
dset.wipe()
|
||||
dset.dict = yaml.load(in_stream)
|
||||
|
||||
|
||||
def import_book(dbook, in_stream):
|
||||
"""Returns databook from YAML stream."""
|
||||
|
||||
dbook.wipe()
|
||||
|
||||
for sheet in yaml.load(in_stream):
|
||||
data = tablib.core.Dataset()
|
||||
data.title = sheet['title']
|
||||
data.dict = sheet['data']
|
||||
dbook.add_sheet(data)
|
||||
@@ -1,5 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
""" Tablib - General Helpers.
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
@@ -12,6 +15,19 @@ class Struct(object):
|
||||
def __getitem__(self, key):
|
||||
return getattr(self, key, None)
|
||||
|
||||
def dictionary(self):
|
||||
"""Returns dictionary representation of object."""
|
||||
return self.__dict__
|
||||
|
||||
def items(self):
|
||||
"""Returns items within object."""
|
||||
return self.__dict__.items()
|
||||
|
||||
def keys(self):
|
||||
"""Returns keys within object."""
|
||||
return self.__dict__.keys()
|
||||
|
||||
|
||||
|
||||
def piped():
|
||||
"""Returns piped input via stdin, else False."""
|
||||
|
||||
+103
-6
@@ -13,8 +13,10 @@ class TablibTestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Create simple data set with headers."""
|
||||
global data
|
||||
|
||||
global data, book
|
||||
data = tablib.Dataset()
|
||||
book = tablib.Databook()
|
||||
|
||||
self.headers = ('first_name', 'last_name', 'gpa')
|
||||
self.john = ('John', 'Adams', 90)
|
||||
@@ -167,7 +169,7 @@ class TablibTestCase(unittest.TestCase):
|
||||
csv += str(col) + ','
|
||||
csv = csv.strip(',') + '\r\n'
|
||||
|
||||
self.assertEqual(csv, self.founders.csv())
|
||||
self.assertEqual(csv, self.founders.csv)
|
||||
|
||||
|
||||
def test_unicode_append(self):
|
||||
@@ -176,11 +178,106 @@ class TablibTestCase(unittest.TestCase):
|
||||
new_row = ('å', 'é')
|
||||
data.append(new_row)
|
||||
|
||||
data.json()
|
||||
data.yaml()
|
||||
data.csv()
|
||||
data.xls()
|
||||
data.json
|
||||
data.yaml
|
||||
data.csv
|
||||
data.xls
|
||||
|
||||
|
||||
def test_book_export_no_exceptions(self):
|
||||
"""Test that varoius exports don't error out."""
|
||||
|
||||
book = tablib.Databook()
|
||||
book.add_sheet(data)
|
||||
|
||||
book.json
|
||||
book.yaml
|
||||
book.xls
|
||||
|
||||
|
||||
def test_json_import_set(self):
|
||||
"""Generate and import JSON set serialization."""
|
||||
data.append(self.john)
|
||||
data.append(self.george)
|
||||
data.headers = self.headers
|
||||
|
||||
_json = data.json
|
||||
|
||||
data.json = _json
|
||||
|
||||
self.assertEqual(_json, data.json)
|
||||
|
||||
|
||||
def test_json_import_book(self):
|
||||
"""Generate and import JSON book serialization."""
|
||||
data.append(self.john)
|
||||
data.append(self.george)
|
||||
data.headers = self.headers
|
||||
|
||||
book.add_sheet(data)
|
||||
_json = book.json
|
||||
|
||||
book.json = _json
|
||||
|
||||
self.assertEqual(_json, book.json)
|
||||
|
||||
|
||||
def test_yaml_import_set(self):
|
||||
"""Generate and import YAML set serialization."""
|
||||
data.append(self.john)
|
||||
data.append(self.george)
|
||||
data.headers = self.headers
|
||||
|
||||
_yaml = data.yaml
|
||||
|
||||
data.yaml = _yaml
|
||||
|
||||
self.assertEqual(_yaml, data.yaml)
|
||||
|
||||
|
||||
def test_yaml_import_book(self):
|
||||
"""Generate and import YAML book serialization."""
|
||||
data.append(self.john)
|
||||
data.append(self.george)
|
||||
data.headers = self.headers
|
||||
|
||||
book.add_sheet(data)
|
||||
_yaml = book.yaml
|
||||
|
||||
book.yaml = _yaml
|
||||
|
||||
self.assertEqual(_yaml, book.yaml)
|
||||
|
||||
|
||||
def test_csv_import_set(self):
|
||||
"""Generate and import CSV set serialization."""
|
||||
data.append(self.john)
|
||||
data.append(self.george)
|
||||
data.headers = self.headers
|
||||
|
||||
_csv = data.csv
|
||||
|
||||
data.csv = _csv
|
||||
|
||||
self.assertEqual(_csv, data.csv)
|
||||
|
||||
|
||||
def test_wipe(self):
|
||||
"""Purge a dataset."""
|
||||
|
||||
new_row = (1, 2, 3)
|
||||
data.append(new_row)
|
||||
|
||||
# Verify width/data
|
||||
self.assertTrue(data.width == len(new_row))
|
||||
self.assertTrue(data[0] == new_row)
|
||||
|
||||
data.wipe()
|
||||
new_row = (1, 2, 3, 4)
|
||||
data.append(new_row)
|
||||
self.assertTrue(data.width == len(new_row))
|
||||
self.assertTrue(data[0] == new_row)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user