seperator => separator

This commit is contained in:
Kenneth Reitz
2011-05-11 17:58:31 -04:00
parent 7ae7d3ff46
commit a2b62669b7
2 changed files with 45 additions and 45 deletions
+27 -27
View File
@@ -30,11 +30,11 @@ A :class:`Dataset <tablib.Dataset>` is nothing more than what its name implies
Creating your own instance of the :class:`tablib.Dataset` object is simple. ::
data = tablib.Dataset()
You can now start filling this :class:`Dataset <tablib.Dataset>` object with data.
.. admonition:: Example Context
From here on out, if you see ``data``, assume that it's a fresh :class:`Dataset <tablib.Dataset>` object.
@@ -52,7 +52,7 @@ Let's say you want to collect a simple list of names. ::
for name in names:
# split name appropriately
fname, lname = name.split()
# add names to Dataset
data.append([fname, lname])
@@ -76,19 +76,19 @@ Now our data looks a little different. ::
>>> data.dict
[{'Last Name': 'Reitz', 'First Name': 'Kenneth'}, {'Last Name': 'Monke', 'First Name': 'Bessie'}]
--------------
Adding Columns
Adding Columns
--------------
Now that we have a basic :class:`Dataset` in place, let's add a column of **ages** to it. ::
data.append(col=[22, 20], header='Age')
Let's view the data now. ::
>>> data.dict
@@ -106,8 +106,8 @@ Tablib's killer feature is the ability to export your :class:`Dataset` objects i
**Comma-Separated Values** ::
>>> data.csv
Last Name,First Name,Age
Reitz,Kenneth,22
Last Name,First Name,Age
Reitz,Kenneth,22
Monke,Bessie,20
**JavaScript Object Notation** ::
@@ -121,7 +121,7 @@ Tablib's killer feature is the ability to export your :class:`Dataset` objects i
>>> data.yaml
- {Age: 22, First Name: Kenneth, Last Name: Reitz}
- {Age: 20, First Name: Bessie, Last Name: Monke}
**Microsoft Excel** ::
@@ -190,11 +190,11 @@ Thanks to Josh Ourisman, Tablib now supports adding dynamic columns. A dynamic c
Let's add a dynamic column to our :class:`Dataset` object. In this example, we have a function that generates a random grade for our students. ::
import random
def random_grade(row):
"""Returns a random integer for entry."""
return (random.randint(60,100)/100.0)
data.append(col=[random_grade], header='Grade')
Let's have a look at our data. ::
@@ -209,7 +209,7 @@ Let's remove that column. ::
>>> del data['Grade']
When you add a dynamic column, the first argument that is passed in to the given callable is the current data row. You can use this to perform calculations against your data row.
When you add a dynamic column, the first argument that is passed in to the given callable is the current data row. You can use this to perform calculations against your data row.
For example, we can use the data available in the row to guess the gender of a student. ::
@@ -217,9 +217,9 @@ For example, we can use the data available in the row to guess the gender of a s
"""Calculates gender of given student data row."""
m_names = ('Kenneth', 'Mike', 'Yuri')
f_names = ('Bessie', 'Samantha', 'Heather')
name = row[0]
if name in m_names:
return 'Male'
elif name in f_names:
@@ -243,8 +243,8 @@ Filtering Datasets with Tags
.. versionadded:: 0.9.0
When constructing a :class:`Dataset` object, you can add tags to rows by specifying the ``tags`` parameter.
This allows you to filter your :class:`Dataset` later. This can be useful so separate rows of data based on
When constructing a :class:`Dataset` object, you can add tags to rows by specifying the ``tags`` parameter.
This allows you to filter your :class:`Dataset` later. This can be useful so separate rows of data based on
arbitrary criteria (*e.g.* origin) that you don't want to include in your :class:`Dataset`.
Let's tag some students. ::
@@ -266,10 +266,10 @@ It's that simple. The original :class:`Dataset` is untouched.
Excel Workbook With Multiple Sheets
------------------------------------
------------------------------------
When dealing with a large number of :class:`Datasets <Dataset>` in spreadsheet format, it's quite common to group multiple spreadsheets into a single Excel file, known as a Workbook. Tablib makes it extremely easy to build workbooks with the handy, :class:`Databook` class.
Let's say we have 3 different :class:`Datasets <Dataset>`. All we have to do is add then to a :class:`Databook` object... ::
@@ -287,15 +287,15 @@ The resulting **students.xls** file will contain a separate spreadsheet for each
Make sure to open the output file in binary mode.
.. _seperators:
.. _separators:
----------
Seperators
Separators
----------
.. versionadded:: 0.8.2
When, it's often useful to create a blank row containing information on the upcoming data. So,
When, it's often useful to create a blank row containing information on the upcoming data. So,
@@ -305,24 +305,24 @@ When, it's often useful to create a blank row containing information on the upco
('11/24/09', 'Math 101 Mid-term Exam', 56.),
('05/24/10', 'Math 101 Final Exam', 62.)
]
suzie_tests = [
('11/24/09', 'Math 101 Mid-term Exam', 56.),
('05/24/10', 'Math 101 Final Exam', 62.)
]
# Create new dataset
tests = tablib.Dataset()
tests.headers = ['Date', 'Test Name', 'Grade']
# Daniel's Tests
tests.append_seperator('Daniel\'s Scores')
tests.append_separator('Daniel\'s Scores')
for test_row in daniel_tests:
tests.append(test_row)
# Susie's Tests
tests.append_seperator('Susie\'s Scores')
tests.append_separator('Susie\'s Scores')
for test_row in suzie_tests:
tests.append(test_row)
@@ -331,7 +331,7 @@ When, it's often useful to create a blank row containing information on the upco
with open('grades.xls', 'wb') as f:
f.write(tests.xls)
The resulting **tests.xls** will have the following layout:
The resulting **tests.xls** will have the following layout:
Daniel's Scores:
@@ -347,7 +347,7 @@ The resulting **tests.xls** will have the following layout:
.. admonition:: Format Support
At this time, only :class:`Excel <Dataset.xls>` output supports separators.
----
Now, go check out the :ref:`API Documentation <api>` or begin :ref:`Tablib Development <development>`.
+18 -18
View File
@@ -94,7 +94,7 @@ class Row(object):
return (tag in self.tags)
else:
return bool(len(set(tag) & set(self.tags)))
@@ -138,7 +138,7 @@ class Dataset(object):
# ('title', index) tuples
self._separators = []
# (column, callback) tuples
self._formatters = []
@@ -242,12 +242,12 @@ class Dataset(object):
"""Packages Dataset into lists of dictionaries for transmission."""
_data = list(self._data)
# Execute formatters
if self._formatters:
for row_i, row in enumerate(_data):
for col, callback in self._formatters:
try:
try:
if col is None:
for j, c in enumerate(row):
_data[row_i][j] = callback(c)
@@ -255,7 +255,7 @@ class Dataset(object):
_data[row_i][col] = callback(row[col])
except IndexError:
raise InvalidDatasetIndex
if self.headers:
if dicts:
@@ -331,8 +331,8 @@ class Dataset(object):
headers = property(_get_headers, _set_headers)
def _get_dict(self):
"""A native Python representation of the :class:`Dataset` object. If headers have
been set, a list of Python dictionaries will be returned. If no headers have been set,
"""A native Python representation of the :class:`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.
A dataset object can also be imported by setting the `Dataset.dict` attribute: ::
@@ -379,7 +379,7 @@ class Dataset(object):
@property
def xls():
"""An Excel Spreadsheet representation of the :class:`Dataset` object, with :ref:`seperators`. Cannot be set.
"""An Excel Spreadsheet representation of the :class:`Dataset` object, with :ref:`separators`. Cannot be set.
.. admonition:: Binary Warning
@@ -480,7 +480,7 @@ class Dataset(object):
def append_separator(self, text='-'):
"""Adds a :ref:`seperator <seperators>` to the :class:`Dataset`."""
"""Adds a :ref:`separator <separators>` to the :class:`Dataset`."""
# change offsets if headers are or aren't defined
if not self.headers:
@@ -493,26 +493,26 @@ class Dataset(object):
def add_formatter(self, 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
:param handler: reference to callback function to execute
against each cell value.
"""
if isinstance(col, str):
if col in self.headers:
col = self.headers.index(col) # get 'key' index from each data
else:
raise KeyError
if not col > self.width:
self._formatters.append((col, handler))
else:
raise InvalidDatasetIndex
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.
@@ -584,10 +584,10 @@ class Dataset(object):
def sort(self, col, reverse=False):
"""Sort a :class:`Dataset` by a specific column, given string (for
header) or integer (for column index). The order can be reversed by
setting ``reverse`` to ``True``.
setting ``reverse`` to ``True``.
Returns a new :class:`Dataset` instance where columns have been
sorted."""
if isinstance(col, str):
if not self.headers:
@@ -805,7 +805,7 @@ class InvalidDatasetType(Exception):
class InvalidDimensions(Exception):
"Invalid size"
class InvalidDatasetIndex(Exception):
"Outside of Dataset size"