Package qubx :: Module table_tree
[hide private]
[frames] | no frames]

Source Code for Module qubx.table_tree

  1  """Storage and retrieval of qubx.table info into qubx.tree format. 
  2   
  3  Copyright 2008-2011 Research Foundation State University of New York  
  4  This file is part of QUB Express.                                           
  5   
  6  QUB Express is free software; you can redistribute it and/or modify           
  7  it under the terms of the GNU General Public License as published by  
  8  the Free Software Foundation, either version 3 of the License, or     
  9  (at your option) any later version.                                   
 10   
 11  QUB Express is distributed in the hope that it will be useful,                
 12  but WITHOUT ANY WARRANTY; without even the implied warranty of        
 13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
 14  GNU General Public License for more details.                          
 15   
 16  You should have received a copy of the GNU General Public License,    
 17  named LICENSE.txt, in the QUB Express program directory.  If not, see         
 18  <http://www.gnu.org/licenses/>.                                       
 19   
 20  """ 
 21   
 22  import numbers 
 23  import numpy 
 24  import traceback 
 25   
 26  import qubx.table 
 27  import qubx.tree 
 28   
 29  from itertools import izip, count 
 30   
31 -def table_to_tree(table, compact=True):
32 if compact: 33 return table_to_tree_compact(table) 34 tree = qubx.tree.Node(table.label) 35 child = qubx.tree.NullNode() 36 for r in xrange(table.size): 37 child = tree.insert('Row', child) 38 row = table.get_row(r) 39 for field in table.fields: 40 child[field].data = table.format[field](row.fields[field]) 41 return tree
42
43 -def tree_to_table(tree, table):
44 if tree.find('Row').isNull: 45 return tree_to_table_compact(tree, table) 46 for r, child in enumerate(qubx.tree.children(tree, 'Row')): 47 str_fields = [(field.name, str(field.data)) for field in qubx.tree.children(child)] 48 row = {} 49 for fieldname, strval in str_fields: 50 try: # table's accept functions 51 try: 52 row[fieldname] = table.accept[fieldname](strval) 53 except KeyError: # no such field (yet) 54 row[fieldname] = table.auto_accept(strval) 55 except: # most- to least-restrictive casts 56 try: 57 row[fieldname] = int(strval) 58 except: 59 try: 60 row[fieldname] = float(strval) 61 except: 62 row[fieldname] = strval 63 if r >= table.size: 64 table.append(row) 65 else: 66 for fieldname, val in row.iteritems(): 67 table.set(r, fieldname, val)
68 69 # that gets way too verbose... 70
71 -def table_to_tree_compact(table):
72 tree = qubx.tree.Node(table.label) 73 if table.size == 0: 74 return tree 75 child = qubx.tree.NullNode() 76 def copy_numbers(data, field): 77 for i in xrange(table.size): 78 data[i] = table[i, field]
79 for field in table.fields: 80 if field == 'Index': 81 continue 82 child = tree.insert(field, child) 83 x = table[0, field] 84 if isinstance(x, numbers.Integral) or isinstance(x, numpy.integer): 85 child.data.setup(qubx.tree.QTR_TYPE_INT, table.size, 1) 86 copy_numbers(child.data, field) 87 elif isinstance(x, numbers.Real) or isinstance(x, numpy.floating): 88 child.data.setup(qubx.tree.QTR_TYPE_DOUBLE, table.size, 1) 89 copy_numbers(child.data, field) 90 else: 91 sub = qubx.tree.NullNode() 92 for i in xrange(table.size): 93 sub = child.insert('Row', sub) 94 sub.data = table.format[field](table[i, field]) 95 return tree 96
97 -def tree_to_table_compact(tree, table):
98 # special handling for Lists: From and To come first, together: 99 skip_from_to = False 100 child_from = tree.find('From') 101 child_to = tree.find('To') 102 if not (child_from.isNull or child_to.isNull): 103 skip_from_to = True 104 for i, f, t in izip(count(), child_from.data, child_to.data): 105 if i == table.size: 106 table.append({'From' : f, 'To' : t}) 107 else: 108 table[i,'From'] = f 109 table[i,'To'] = t 110 for child in qubx.tree.children(tree): 111 field = child.name 112 if skip_from_to and (field in ('From', 'To')): 113 continue 114 if child.data.type in (qubx.tree.QTR_TYPE_INT, qubx.tree.QTR_TYPE_DOUBLE): 115 for i, x in enumerate(child.data): 116 if i == table.size: 117 table.append({}) 118 try: 119 table[i, field] = x 120 except KeyError: # not allowed to add arbitrary fields 121 pass 122 else: 123 for i, sub in enumerate(qubx.tree.children(child)): 124 if i == table.size: 125 table.append({}) 126 sdata = str(sub.data) 127 try: 128 try: 129 try: 130 accept = table.accept[field] 131 except KeyError: # no such field (yet) 132 table[i, field] = table.auto_accept(sdata) 133 table[i, field] = accept(sdata) 134 except KeyError: # not allowed to add this field 135 pass 136 except: # not 'accept'ed 137 try: 138 table[i, field] = sdata 139 except KeyError: 140 pass
141