Package omero :: Package util :: Module text
[hide private]
[frames] | no frames]

Source Code for Module omero.util.text

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # OMERO Text handling utilities 
  5  # 
  6  # Copyright 2010 Glencoe Software, Inc.  All Rights Reserved. 
  7  # Use is subject to license terms supplied in LICENSE.txt 
  8  # 
  9   
 10  # 
 11  # The following classes (ALIGN, Column, Table) were originally from 
 12  # http://code.activestate.com/recipes/577202-render-tables-for-text-interface/ 
 13  # 
 14   
15 -class TableBuilder(object):
16 """ 17 OMERO-addition to make working with Tables easier 18 """ 19
20 - def __init__(self, *headers):
21 self.headers = list(headers) 22 self.results = [[] for x in self.headers]
23
24 - def col(self, name):
25 """ 26 Add a new column and back fill spaces 27 """ 28 self.headers.append(name) 29 self.results.append(["" for x in range(len(self.results[0]))])
30
31 - def cols(self, names):
32 """ 33 Similar to col() but only adds unknown columns 34 """ 35 for name in names: 36 if name not in self.headers: 37 self.col(name)
38
39 - def row(self, *items, **by_name):
40 41 if len(items) > len(self.headers): 42 raise ValueError("Size mismatch: %s != %s" % (len(items), len(self.headers))) 43 44 # Fill in all values, even if missing 45 for idx in range(len(self.results)): 46 value = None 47 if idx < len(items): 48 value = items[idx] 49 self.results[idx].append(value) 50 51 size = len(self.results[0]) 52 for k, v in by_name.items(): 53 if k not in self.headers: 54 raise KeyError("%s not in %s" % (k, self.headers)) 55 idx = self.headers.index(k) 56 self.results[idx][-1] = by_name[self.headers[idx]]
57
58 - def build(self):
59 columns = [] 60 for i, x in enumerate(self.headers): 61 columns.append(Column(x, self.results[i])) 62 return Table(*columns)
63
64 - def __str__(self):
65 return str(self.build())
66 67
68 -class ALIGN:
69 LEFT, RIGHT = '-', ''
70 71
72 -class Column(list):
73
74 - def __init__(self, name, data, align=ALIGN.LEFT):
75 list.__init__(self, data) 76 self.name = name 77 self.width = max(len(str(x).decode("utf-8")) for x in data + [name]) 78 self.format = ' %%%s%ds ' % (align, self.width)
79 80
81 -class Table:
82
83 - def __init__(self, *columns):
84 self.columns = columns 85 self.length = max(len(x) for x in columns)
86
87 - def get_row(self, i=None):
88 for x in self.columns: 89 if i is None: 90 yield x.format % x.name 91 else: 92 try: 93 x[i].decode("ascii") 94 except UnicodeDecodeError: # Unicode characters are present 95 yield (x.format % x[i].decode("utf-8")).encode("utf-8") 96 except AttributeError: # Unicode characters are present 97 yield x.format % x[i] 98 else: 99 yield x.format % x[i]
100
101 - def get_rows(self):
102 yield '|'.join(self.get_row(None)) 103 yield "+".join(["-"* (x.width+2) for x in self.columns]) 104 for i in range(0, self.length): 105 yield '|'.join(self.get_row(i)) 106 yield "(%s %s)" % (self.length, (self.length == 1 and "row" or "rows"))
107
108 - def __str__(self):
109 return '\n'.join(self.get_rows())
110