Back to index

moin  1.9.0~rc2
dataset.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Datasets
00004 
00005     Datasets are used by the DataBrowserWidget, and with the
00006     statistics code.
00007 
00008     @copyright: 2002 Juergen Hermann <jh@web.de>
00009     @license: GNU GPL, see COPYING for details.
00010 """
00011 
00012 class Column:
00013     """ Meta-data for a column.
00014     """
00015 
00016     _SLOTS = [
00017         ('label', ''),
00018         ('sortable', 0),
00019         ('hidden', 0),
00020         ('align', ''),
00021         ('autofilter', 0),
00022         ]
00023 
00024     def __init__(self, name, **kw):
00025         """ Init a column
00026 
00027         keys in kw that not in slots are ignored.
00028 
00029         @param name: column name, unicode or ascii
00030         @param kw: column meta data, unicode or ascii
00031         """
00032         self.name = name
00033         for slot, defval in self._SLOTS:
00034             setattr(self, slot, kw.get(slot, defval))
00035 
00036 
00037 class Dataset:
00038     """ Base dataset.
00039 
00040     Holds a 2-dimensional data set (m rows of n columns) and associated
00041     meta-data (column titles, etc.).
00042 
00043     Note: Dataset rows and column must contain only ascii or Unicode values!
00044     """
00045 
00046     def __init__(self, data_id=None):
00047         self.columns = []
00048         self.data = []
00049         self._pos = 0
00050         self.data_id = data_id
00051 
00052     def __len__(self):
00053         return len(self.data)
00054 
00055     def reset(self):
00056         """ Reset iterator to start.
00057         """
00058         self._pos = 0
00059 
00060     def next(self):
00061         """ Return next row as a tuple, ordered by columns.
00062         """
00063         if self._pos >= len(self):
00064             return None
00065 
00066         row = self.data[self._pos]
00067         self._pos += 1
00068         return row
00069 
00070     def addRow(self, row):
00071         """ Add a row to the dataset.
00072         """
00073         self.data.append(row)
00074 
00075 
00076 class TupleDataset(Dataset):
00077     """ A dataset that stores tuples.
00078     """
00079 
00080 
00081 class DictDataset(Dataset):
00082     """ A dataset that stores dicts as the rows.
00083     """
00084 
00085     def next(self):
00086         row = Dataset.next(self)
00087         return tuple([row[col.name] for col in self.columns])
00088 
00089 
00090 class DbDataset(Dataset):
00091     pass
00092