/timm's /charming /python /tricks

table.py

Download table.py.
Read more on How to be Charming (in Python).


01: import sys
02: sys.dont_write_bytecode=True
03: from about import *
04: 
05: ###############################
06: class Row(Charmed):
07:   "Information about decisions, objectives."
08:   id = -1
09:   def __init__(i,t,cells):
10:     i.id = Row.id = Row.id + 1
11:     i.cells, i.t = cells,t
12:     i.theScore=None
13:   def score(i):
14:     if i.theScore == None:
15:       i.theScore = fromhell(i.cells,i.t)
16:     return i.theScore
17:   def approx(i):
18:     out = i.cells[:]
19:     for h in i.t.nums:
20:       out[h.pos] = approx(h.bins(),out[h.pos])
21:     return out
22:  
23: def fromhell(cells,t): 
24:   "Sum of the distance from hell on all objectives."
25:   s = 0.0
26:   less, more = t.less, t.more
27:   for h in less:
28:     x= h.norm(cells[h.pos])
29:     s += (1-x)
30:   for h in more: 
31:     x= h.norm(cells[h.pos])
32:     s += x
33:   return  s/ (len(less)+len(more) + 1/The.math.inf)
34: 
35: class Table(Charmed):
36:   "Storage for what we know about Rows."
37:   id = -1
38:   def __init__(i,spec=[],keeping=True):
39:     i.id = Table.id = Table.id+1
40:     i.more, i.less, i.klass, i.nums = [] ,[], [], []
41:     i.syms, i.rows, i.columns       = [], [], []
42:     i.dep , i.indep, i._at          = [], [], {}
43:     i.keeping = keeping
44:     if spec: 
45:       i.header(spec)
46:   def at(i,x): return i._at[x]
47:   def centroid(i): 
48:     return [h.centroid() for h in i.columns]
49:   def clone(i,lstOfCells=[],keeping=True): 
50:     t = Table(spec=i.spec(), keeping=keeping)
51:     for cells in lstOfCells: t.seen(cells) 
52:     return t
53:   def discretize(i,rnn=True):    
54:     def sym(x): 
55:       return x.translate(None,The.read.num)
56:     t = Table(spec = [sym(h.name) for h in i.columns])
57:     for row in i.rows:
58:         t.seen(row.approx())
59:     return t 
60:   def spec(i)   : return [x.name for x in i.columns]
61:   def nump(i,x) : return x in [The.read.less,
62:                          The.read.more,The.read.num]
63:   def add(i,txt,head):
64:     r = The.read
65:     if   txt[0] == r.more  : also = i.more 
66:     elif txt[0] == r.less  : also = i.less
67:     elif txt[0] == r.klass : also = i.klass
68:     elif txt[0] == r.num   : also = i.nums
69:     else:                    also = i.syms
70:     i.columns += [head]
71:     head.pos   = i._at[txt] = len(i.columns) -1
72:     txt1 = ''.join([y.strip(The.read.chars) 
73:                     for y in txt])
74:     i._at[txt1] = head.pos
75:     also    += [head]
76:     i.indep  = i.nums + i.syms
77:     i.dep    = i.more + i.less + i.klass
78:   def has(i,*things):
79:     for thing in things: i.add(thing.name,thing)
80:   def header(i,spec): # e.g. Num(name=fred,min=
81:     for m,txt in enumerate(spec):
82:       head = Num if i.nump(txt[0]) else Sym
83:       i.add(txt, head(name=txt))
84:   def __repr__(i):
85:     return 'Table(%g,%g)' % (i.id,len(i.rows)) 
86:   def seen(i,cells):
87:     row = [None] * len(i.columns)
88:     for head in i.columns:
89:       cell = cells[head.pos]
90:       if cell != The.read.ignore:
91:         cell = head.seen(cell)
92:       row[head.pos] = cell
93:     if i.keeping: 
94:       i.rows += [Row(cells=row,t=i)]
95: 

This file is part of Timm's charming Python tricks.
© 2014, Tim Menzies: tim.menzies@gmail.com, http://menzies.us.

Timm's charming Python tricks are free software: you can redistribute it and/or modify it under the terms of the GNU Lesser Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Timm's charming Python tricks are distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU Lesser Public License along with Foobar. If not, see http://www.gnu.org/licenses.