-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcompound_message.py
More file actions
54 lines (44 loc) · 1.73 KB
/
compound_message.py
File metadata and controls
54 lines (44 loc) · 1.73 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
from sqlflow.rows import Rows
class CompoundMessage:
def __init__(self):
"""Message containing return result of several SQL statements
CompoundMessage can not display in notebook since we need to
output log messages for long running training sqls.
"""
self._messages = []
self.TypeRows = 1
self.TypeMessage = 2
self.TypeHTML = 3
def add_rows(self, rows, eoe):
assert(isinstance(rows, Rows))
# call __str__() to trigger rows_gen
rows.__str__()
self._messages.append((rows, eoe, self.TypeRows))
def add_message(self, message, eoe):
assert(isinstance(message, str))
self._messages.append((message, eoe, self.TypeMessage))
def add_html(self, message, eoe):
assert(isinstance(message, str))
self._messages.append((message, eoe, self.TypeHTML))
def length(self):
return len(self._messages)
def __str__(self):
return self.__repr__()
def __repr__(self):
all_string = ""
for r in self._messages:
if isinstance(r[0], Rows):
all_string = '\n'.join([all_string, r[0].__repr__()])
else:
all_string = '\n'.join([all_string, r[0]."<p>%s</p>" % (r[0].__str__().replace("\n", "<br>"))])
return all_string
def _repr_html_(self):
all_html = ""
for r in self._messages:
if isinstance(r[0], Rows):
all_html = ''.join([all_html, r[0]._repr_html_()])
else:
all_html = ''.join([all_html, "<p>%s</p>" % (r[0].__str__().replace("\n", "<br>"))])
return all_html
def get(self, idx):
return self._messages[idx][0]