Mercurial > hg > numerics
view numerics/data.py @ 22:61ef3985ec5b
stub columns; do we need it
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 03 Oct 2014 17:48:39 -0700 |
parents | fef3f407113f |
children | 87615a38190c |
line wrap: on
line source
# -*- coding: utf-8 -*- """ data models """ 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 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] class Columns(object): """ column-oriented data """ def __init__(self, *columns): self.columns = OrderedDict() # this should be ordered