-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugutil.py
More file actions
293 lines (261 loc) · 12.2 KB
/
debugutil.py
File metadata and controls
293 lines (261 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# COPYRIGHT (c) 2008
# THE REGENTS OF THE UNIVERSITY OF MICHIGAN
# ALL RIGHTS RESERVED
#
# PERMISSION IS GRANTED TO USE, COPY, CREATE DERIVATIVE WORKS AND
# REDISTRIBUTE THIS SOFTWARE AND SUCH DERIVATIVE WORKS FOR NONCOMMERCIAL
# EDUCATION AND RESEARCH PURPOSES, SO LONG AS NO FEE IS CHARGED, AND SO
# LONG AS THE COPYRIGHT NOTICE ABOVE, THIS GRANT OF PERMISSION, AND THE
# DISCLAIMER BELOW APPEAR IN ALL COPIES MADE; AND SO LONG AS THE NAME OF
# THE UNIVERSITY OF MICHIGAN IS NOT USED IN ANY ADVERTISING OR PUBLICITY
# PERTAINING TO THE USE OR DISTRIBUTION OF THIS SOFTWARE WITHOUT SPECIFIC,
# WRITTEN PRIOR AUTHORIZATION.
#
# THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE
# UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND WITHOUT
# WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE REGENTS OF THE
# UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE FOR ANY DAMAGES, INCLUDING
# SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WITH RESPECT TO
# ANY CLAIM ARISING OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE,
# EVEN IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGES.
"""Helper functions to make subject output easier (as for tailor.py)
"""
import cStringIO
import operator
from tailoring2.dictionary import CharacteristicDefinition
# ------------------------------------------------------------
class CharacteristicValue(object):
def __init__(self, chardef, selval, msgval):
self.chardef = chardef
self.selval = selval
self.msgval = msgval
def SubjectReprIntermediate(subject, mtsdict, data_sources=None):
"""before dumping to text or html or whatever, put together
collect the subject info in a useful intermediate format suitable
for quick conversion into some tabular form.
returned data structure is a dict of sources. Each source is a
dict of characteristic name to a CharacteristicValue instance.
allchars[source] = {charname: CharacteristicValue()}
"""
allchars = {}
data_sources = data_sources or subject.data_sources \
or mtsdict.sources or ['']
for source in data_sources:
allchars[source] = {}
selchars = subject.selection_chars.get(source, {})
msgchars = subject.message_chars.get(source, {})
for key, value in selchars.items():
try:
chardef = mtsdict.char_index[key]
except KeyError:
chardef = CharacteristicDefinition.synthesize(key, value)
allchars[source][key] = CharacteristicValue(
chardef, selchars[key], msgchars[key])
return allchars
def deunicode(s):
"""s is a unicode string but doesn't necessarily contain any unicode
characters -- try to convert it to a plain string. Return the plain
string version if conversion was possible, the original unicode if
not.
"""
if isinstance(s, unicode):
try:
s = str(s)
except UnicodeDecodeError:
pass
return s
class SubjectRepr(object):
"""helper class for a subject that provides useful debugging output
"""
def __init__(self, subject, mtsdict, maxcolwidth=30):
self.subject = subject
self.mtsdict = mtsdict
self.maxcolwidth = maxcolwidth
def dump(self, data_sources=None):
"""return a string, a table showing all the subject's
selection_chars, one per row alphabetically.
Derived characteristics are denoted by a D. Multiple-response
characteristics are denoted by a +. Values that are different in
message_chars are shown in the rightmost column. For visual
clarity, characteristic values are converted from unicode to
plain strings where possible.
"""
# lines accumulator -- first row is header
lines = [['Source', 'Name', 'Selection', 'Message']]
data_sources = data_sources or self.mtsdict.sources or ['']
subjectinfo = SubjectReprIntermediate(self.subject,
self.mtsdict, data_sources)
for source in data_sources:
lines.append([repr(source), '', '', ''])
key_order = self.subject.selection_chars[source].keys()
key_order.sort()
for key in key_order:
charval = subjectinfo[source][key]
chardef, selval, msgval = charval.chardef, charval.selval, charval.msgval
if source not in chardef.sources:
# not really in source -- exclude from output
continue
lineitems = [' ']
name_col = chardef.name
if chardef.is_multivalued:
name_col += " [+]"
if chardef.is_derived:
name_col += " [D]"
lineitems.append("%s" % name_col)
selvalrepr = repr(deunicode(selval))
lineitems.append(selvalrepr)
if msgval == selval:
lineitems.append('')
else:
lineitems.append("%s" % (repr(msgval)))
lines.append(lineitems)
wrapper = lambda t: wrap_onspace(t, self.maxcolwidth)
return indent(lines, hasHeader=True, wrapfunc=wrapper)
def __repr__(self):
return "<SubjectRepr for %s on %s>" % (self.subject, self.mtsdict)
# ------------------------------------------------------------
# table-presentation function by George Sakkis:
# http://code.activestate.com/recipes/267662/
def indent(rows, hasHeader=False, headerChar='-', delim=' | ', justify='left',
separateRows=False, prefix='', postfix='', wrapfunc=lambda x: x):
"""Indents a table by column.
- rows: A sequence of sequences of items, one sequence per row.
- hasHeader: True if the first row consists of the columns' names.
- headerChar: Character to be used for the row separator line
(if hasHeader==True or separateRows==True).
- delim: The column delimiter.
- justify: Determines how are data justified in their column.
Valid values are 'left','right' and 'center'.
- separateRows: True if rows are to be separated by a line
of 'headerChar's.
- prefix: A string prepended to each printed row.
- postfix: A string appended to each printed row.
- wrapfunc: A function f(text) for wrapping text; each element in
the table is first wrapped by this function."""
# closure for breaking logical rows to physical, using wrapfunc
def rowWrapper(row):
newRows = [wrapfunc(item).split('\n') for item in row]
return [[substr or '' for substr in item] for item in map(None, *newRows)]
# break each logical row into one or more physical ones
logicalRows = [rowWrapper(row) for row in rows]
# columns of physical rows
columns = map(None, *reduce(operator.add, logicalRows))
# get the maximum of each column by the string length of its items
maxWidths = [max([len(str(item)) for item in column]) for column in columns]
rowSeparator = headerChar * (len(prefix) + len(postfix) + sum(maxWidths) + \
len(delim)*(len(maxWidths)-1))
# select the appropriate justify method
justify = {'center': str.center, 'right': str.rjust, 'left': str.ljust}[justify.lower()]
output=cStringIO.StringIO()
if separateRows:
print >> output, rowSeparator
for physicalRows in logicalRows:
for row in physicalRows:
print >> output, \
prefix \
+ delim.join([justify(str(item), width) for (item, width) in zip(row, maxWidths)]) \
+ postfix
if separateRows or hasHeader:
print >> output, rowSeparator
hasHeader=False
return output.getvalue()
# written by Mike Brown
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061
def wrap_onspace(text, width):
"""
A word-wrap function that preserves existing line breaks
and most spaces in the text. Expects that existing line
breaks are posix newlines (\n).
"""
return reduce(lambda line, word, width=width: '%s%s%s' %
(line,
' \n'[(len(line[line.rfind('\n')+1:])
+ len(word.split('\n', 1)[0]) >= width)],
word),
text.split(' '))
import re
def wrap_onspace_strict(text, width):
"""Similar to wrap_onspace, but enforces the width constraint:
words longer than width are split."""
wordRegex = re.compile(r'\S{'+str(width)+r',}')
return wrap_onspace(wordRegex.sub(
lambda m: wrap_always(m.group(), width), text), width)
import math
def wrap_always(text, width):
"""A simple word-wrap function that wraps text on exactly width characters.
It doesn't split the text in words."""
return '\n'.join([text[width*i: width*(i+1)] \
for i in xrange(int(math.ceil(1.*len(text)/width)))])
if __name__ == '__main__':
labels = ('First Name', 'Last Name', 'Age', 'Position')
data = \
'''John,Smith,24,Software Engineer
Mary,Brohowski,23,Sales Manager
Aristidis,Papageorgopoulos,28,Senior Reseacher'''
rows = [row.strip().split(',') for row in data.splitlines()]
print 'Without wrapping function\n'
print indent([labels]+rows, hasHeader=True)
# test indent with different wrapping functions
width = 10
for wrapper in (wrap_always, wrap_onspace, wrap_onspace_strict):
print 'Wrapping function: %s(x,width=%d)\n' % (wrapper.__name__, width)
print indent([labels]+rows, hasHeader=True, separateRows=True,
prefix='| ', postfix=' |',
wrapfunc=lambda x: wrapper(x, width))
# output:
#
#Without wrapping function
#
#First Name | Last Name | Age | Position
#-------------------------------------------------------
#John | Smith | 24 | Software Engineer
#Mary | Brohowski | 23 | Sales Manager
#Aristidis | Papageorgopoulos | 28 | Senior Reseacher
#
#Wrapping function: wrap_always(x,width=10)
#
#----------------------------------------------
#| First Name | Last Name | Age | Position |
#----------------------------------------------
#| John | Smith | 24 | Software E |
#| | | | ngineer |
#----------------------------------------------
#| Mary | Brohowski | 23 | Sales Mana |
#| | | | ger |
#----------------------------------------------
#| Aristidis | Papageorgo | 28 | Senior Res |
#| | poulos | | eacher |
#----------------------------------------------
#
#Wrapping function: wrap_onspace(x,width=10)
#
#---------------------------------------------------
#| First Name | Last Name | Age | Position |
#---------------------------------------------------
#| John | Smith | 24 | Software |
#| | | | Engineer |
#---------------------------------------------------
#| Mary | Brohowski | 23 | Sales |
#| | | | Manager |
#---------------------------------------------------
#| Aristidis | Papageorgopoulos | 28 | Senior |
#| | | | Reseacher |
#---------------------------------------------------
#
#Wrapping function: wrap_onspace_strict(x,width=10)
#
#---------------------------------------------
#| First Name | Last Name | Age | Position |
#---------------------------------------------
#| John | Smith | 24 | Software |
#| | | | Engineer |
#---------------------------------------------
#| Mary | Brohowski | 23 | Sales |
#| | | | Manager |
#---------------------------------------------
#| Aristidis | Papageorgo | 28 | Senior |
#| | poulos | | Reseacher |
#---------------------------------------------