-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathoverride_summary.py
executable file
·68 lines (53 loc) · 1.81 KB
/
override_summary.py
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
#!/usr/bin/env python
"""
Example that demonstrates overriding the summary column
"""
from __future__ import print_function
import sys, logging, copy
from objbrowser import browse, logging_basic_config
from objbrowser.attribute_model import DEFAULT_ATTR_COLS, tio_summary
logger = logging.getLogger(__name__)
class MyPoint(object):
""" Example class for representing a point
"""
def __init__(self, x, y):
""" Constructor
"""
self.x = x
self.y = y
def __repr__(self):
""" String representation
"""
return "<MyPoint: x = {}, y = {}>".format(self.x, self.y)
def my_summary(tree_item):
""" Returns (x, y) if the tree_item object is a point.
Otherwise uses the default summary
"""
tio = tree_item.obj
if isinstance(tio, MyPoint):
return "({}, {})".format(tio.x, tio.y)
else:
return tio_summary(tree_item)
def my_browse(*args, **kwargs):
""" Creates and starts an ObjectBrowser with modified summary column.
"""
attribute_columns = copy.deepcopy(DEFAULT_ATTR_COLS)
summary_column = [col for col in attribute_columns if col.name == 'summary'][0]
summary_column.data_fn = my_summary
return browse(*args, attribute_columns = attribute_columns, **kwargs)
def main():
""" Main program
"""
logging_basic_config('DEBUG')
logger.info('Started example')
my_list = range(5, 9)
p1 = MyPoint(0.0, 0.0)
p2 = MyPoint(-4, 1)
exit_code = my_browse({'my_list': my_list, 'p1': p1, 'p2': p2},
show_callable_attributes=False,
show_dunder_attributes=False,
reset=True)
logging.info('Done example')
sys.exit(exit_code)
if __name__ == '__main__':
main()