Mercurial > hg > numerics
changeset 21:fef3f407113f
spec rows
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 03 Oct 2014 17:29:42 -0700 |
parents | 178dc9a94387 |
children | 61ef3985ec5b |
files | numerics/data.py |
diffstat | 1 files changed, 42 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/numerics/data.py Mon Sep 29 16:19:35 2014 -0700 +++ b/numerics/data.py Fri Oct 03 17:29:42 2014 -0700 @@ -4,10 +4,46 @@ data models """ -__all__ = ['Dataset'] -string = (str, unicode) +from collections import OrderedDict + +__all__ = ['Rows', 'Columns'] + + +class ColumnNumberException(Exception): + """ + wrong number of columns: {given} given; {expected} expected + """ + def __init__(self, given, expected): + self.given = given + self.expected = expected + Exception.__init__(self.__doc__.format(**self.__dict__)) + + +class Rows(object): + """ + row-based data + """ + + array = OrderedDict -class Dataset(object): - """ - rows of data - """ + def __init__(self, columns, *rows): + """ + columns -- column labels + """ + self.column_names = columns + self.rows = [] + + for row in rows: + self += row + + def __iadd__(self, row): + """add a labeled row""" + if len(row) != len(self.columns_names): + raise ColumnNumberException(len(row), len(self.columns_names)) + self.rows.append(self.array(zip(self.columns_names, row))) + + def __len__(self): + return len(self.rows) + + def __getitem__(self, item): + return self.rows[item]