Mercurial > hg > numerics
annotate numerics/data.py @ 50:e39643d18d82
note to self
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 19 Jan 2015 12:45:06 -0800 |
parents | 5caa67643162 |
children | e7e49b46832b |
rev | line source |
---|---|
19 | 1 # -*- coding: utf-8 -*- |
2 | |
3 """ | |
4 data models | |
5 """ | |
6 | |
21 | 7 from collections import OrderedDict |
8 | |
48
36e47061187f
stub a transposition function
Jeff Hammel <k0scist@gmail.com>
parents:
23
diff
changeset
|
9 __all__ = ['ColumnNumberException', 'ColumnLengthException', 'Rows', 'Columns'] |
21 | 10 |
11 | |
12 class ColumnNumberException(Exception): | |
13 """ | |
14 wrong number of columns: {given} given; {expected} expected | |
15 """ | |
16 def __init__(self, given, expected): | |
17 self.given = given | |
18 self.expected = expected | |
23 | 19 Exception.__init__(self.__doc__.format(**self.__dict__).strip()) |
20 | |
21 | |
48
36e47061187f
stub a transposition function
Jeff Hammel <k0scist@gmail.com>
parents:
23
diff
changeset
|
22 def transpose(array): |
36e47061187f
stub a transposition function
Jeff Hammel <k0scist@gmail.com>
parents:
23
diff
changeset
|
23 """makes rows into columns or vice versa""" |
36e47061187f
stub a transposition function
Jeff Hammel <k0scist@gmail.com>
parents:
23
diff
changeset
|
24 |
36e47061187f
stub a transposition function
Jeff Hammel <k0scist@gmail.com>
parents:
23
diff
changeset
|
25 if not array: |
36e47061187f
stub a transposition function
Jeff Hammel <k0scist@gmail.com>
parents:
23
diff
changeset
|
26 return array # nothing to do |
36e47061187f
stub a transposition function
Jeff Hammel <k0scist@gmail.com>
parents:
23
diff
changeset
|
27 |
36e47061187f
stub a transposition function
Jeff Hammel <k0scist@gmail.com>
parents:
23
diff
changeset
|
28 n_cols = len(array[0]) |
49 | 29 retval = [[]] * n_cols |
50 | 30 return zip(*array) |
31 # XXX this is a nothing function; we shouldn't use this | |
48
36e47061187f
stub a transposition function
Jeff Hammel <k0scist@gmail.com>
parents:
23
diff
changeset
|
32 |
23 | 33 class ColumnLengthException(ColumnNumberException): |
34 """ | |
35 wrong length of column: {given} given; {expected} expected | |
36 """ | |
37 # XXX should share ABC, not inherit from ColumnNumberException | |
21 | 38 |
39 | |
40 class Rows(object): | |
41 """ | |
42 row-based data | |
43 """ | |
44 | |
45 array = OrderedDict | |
19 | 46 |
21 | 47 def __init__(self, columns, *rows): |
48 """ | |
49 columns -- column labels | |
50 """ | |
51 self.column_names = columns | |
52 self.rows = [] | |
53 | |
54 for row in rows: | |
55 self += row | |
56 | |
57 def __iadd__(self, row): | |
58 """add a labeled row""" | |
59 if len(row) != len(self.columns_names): | |
60 raise ColumnNumberException(len(row), len(self.columns_names)) | |
61 self.rows.append(self.array(zip(self.columns_names, row))) | |
62 | |
63 def __len__(self): | |
64 return len(self.rows) | |
65 | |
66 def __getitem__(self, item): | |
67 return self.rows[item] | |
22 | 68 |
69 | |
70 class Columns(object): | |
71 """ | |
72 column-oriented data | |
73 """ | |
74 | |
75 def __init__(self, *columns): | |
76 self.columns = OrderedDict() # this should be ordered | |
23 | 77 for name, values in columns: |
78 self += (name, values) | |
79 | |
80 def __iadd__(self, item): | |
81 column_name, values = item | |
82 assert column_name not in self.columns | |
83 return self |