-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue_repr.py
More file actions
96 lines (78 loc) · 2.68 KB
/
value_repr.py
File metadata and controls
96 lines (78 loc) · 2.68 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
from base64 import b64encode
from cStringIO import StringIO
def value_repr(value):
if hasattr(value, 'toJSON'):
return value.toJSON()
if value is None:
return {'type': 'NullValue'}
if (value is True) or (value is False):
return {'type': 'BooleanValue', 'value': value}
if (
isinstance(value, int) or
isinstance(value, long) or
isinstance(value, float)):
return {'type': 'NumberValue', 'value': value}
if isinstance(value, unicode):
return {'type': 'StringValue', 'value': value}
if isinstance(value, str):
try:
return {'type': 'StringValue', 'value': unicode(value, 'ascii')}
except UnicodeDecodeError:
pass
if isinstance(value, Exception):
attributes = {
'type': 'ErrorValue',
'name': value.__class__.__name__,
}
if hasattr(value, 'message'):
attributes['message'] = value.message
return attributes
figure = detect_matplotlib_figure(value)
if figure:
return {
'type': 'InlineImageValue',
'ext': 'png',
'data64': b64encode(matplotlib_figure_png(figure))
}
if isinstance(value, list) or isinstance(value, tuple):
return {
'type': 'ArrayValue',
'value': [value_repr(element) for element in value]
}
if isinstance(value, dict):
pairs = []
for k, v in value.items():
if isinstance(k, str):
try:
k = unicode(k, 'ascii')
except UnicodeDecodeError:
return {'type': 'ReprValue', 'repr': repr(value)}
if not isinstance(k, unicode):
return {'type': 'ReprValue', 'repr': repr(value)}
pairs.append([k, value_repr(v)])
return {
'type': 'DictionaryValue',
'value': dict(pairs)
}
return {'type': 'ReprValue', 'repr': repr(value)}
def matplotlib_figure_png(figure, dpi=400):
f = StringIO()
figure.savefig(f, dpi=dpi, format='png')
f.seek(0)
return f.read()
def detect_matplotlib_figure(value):
if not hasattr(value, '__class__'):
return None
class_str = str(value.__class__)
if class_str == "<class 'matplotlib.figure.Figure'>":
return value
if class_str in [
"<class 'matplotlib.axes.AxesSubplot'>",
"<class 'matplotlib.text.Text'>"]:
return value.figure
if (
isinstance(value, list) and
len(value) == 1 and
(str(value[0].__class__) == "<class 'matplotlib.lines.Line2D'>")):
return value[0].figure
return None