Skip to content

Commit 1f8100f

Browse files
committed
Small changes to Result class.
This came up when I was considering a larger refactor of testing and doc generation
1 parent bb38d34 commit 1f8100f

File tree

1 file changed

+91
-76
lines changed

1 file changed

+91
-76
lines changed

mathics/core/evaluation.py

Lines changed: 91 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
from queue import Queue
77
from threading import Thread, stack_size as set_thread_stack_size
8-
from typing import Tuple
8+
from typing import List, Tuple
99

1010
from mathics_scanner import TranslateError
1111

@@ -142,7 +142,7 @@ def run_with_timeout_and_stack(request, timeout, evaluation):
142142
raise result[0].with_traceback(result[1], result[2])
143143

144144

145-
class Out(KeyComparable):
145+
class _Out(KeyComparable):
146146
def __init__(self) -> None:
147147
self.is_message = False
148148
self.is_print = False
@@ -152,80 +152,6 @@ def get_sort_key(self) -> Tuple[bool, bool, str]:
152152
return (self.is_message, self.is_print, self.text)
153153

154154

155-
class Message(Out):
156-
def __init__(self, symbol, tag, text: str) -> None:
157-
super(Message, self).__init__()
158-
self.is_message = True
159-
self.symbol = symbol
160-
self.tag = tag
161-
self.text = text
162-
163-
def __str__(self) -> str:
164-
return "{}::{}: {}".format(self.symbol, self.tag, self.text)
165-
166-
def __eq__(self, other) -> bool:
167-
return self.is_message == other.is_message and self.text == other.text
168-
169-
def get_data(self):
170-
return {
171-
"message": True,
172-
"symbol": self.symbol,
173-
"tag": self.tag,
174-
"prefix": "%s::%s" % (self.symbol, self.tag),
175-
"text": self.text,
176-
}
177-
178-
179-
class Print(Out):
180-
def __init__(self, text) -> None:
181-
super(Print, self).__init__()
182-
self.is_print = True
183-
self.text = text
184-
185-
def __str__(self) -> str:
186-
return self.text
187-
188-
def __eq__(self, other) -> bool:
189-
return self.is_message == other.is_message and self.text == other.text
190-
191-
def get_data(self):
192-
return {
193-
"message": False,
194-
"text": self.text,
195-
}
196-
197-
198-
class Result:
199-
def __init__(self, out, result, line_no, last_eval=None, form=None) -> None:
200-
self.out = out
201-
self.result = result
202-
self.line_no = line_no
203-
self.last_eval = last_eval
204-
self.form = form
205-
206-
def get_data(self):
207-
return {
208-
"out": [out.get_data() for out in self.out],
209-
"result": self.result,
210-
"line": self.line_no,
211-
"form": self.form,
212-
}
213-
214-
215-
class Output:
216-
def max_stored_size(self, settings) -> int:
217-
return settings.MAX_STORED_SIZE
218-
219-
def out(self, out):
220-
pass
221-
222-
def clear(self, wait):
223-
raise NotImplementedError
224-
225-
def display(self, data, metadata):
226-
raise NotImplementedError
227-
228-
229155
class Evaluation:
230156
def __init__(
231157
self, definitions=None, output=None, format="text", catch_interrupt=True
@@ -620,3 +546,92 @@ def publish(self, tag, *args, **kwargs) -> None:
620546
for listener in listeners:
621547
if listener(*args, **kwargs):
622548
break
549+
550+
551+
class Message(_Out):
552+
def __init__(self, symbol, tag, text: str) -> None:
553+
super(Message, self).__init__()
554+
self.is_message = True
555+
self.symbol = symbol
556+
self.tag = tag
557+
self.text = text
558+
559+
def __str__(self) -> str:
560+
return "{}::{}: {}".format(self.symbol, self.tag, self.text)
561+
562+
def __eq__(self, other) -> bool:
563+
return self.is_message == other.is_message and self.text == other.text
564+
565+
def get_data(self):
566+
return {
567+
"message": True,
568+
"symbol": self.symbol,
569+
"tag": self.tag,
570+
"prefix": "%s::%s" % (self.symbol, self.tag),
571+
"text": self.text,
572+
}
573+
574+
575+
class Print(_Out):
576+
def __init__(self, text) -> None:
577+
super(Print, self).__init__()
578+
self.is_print = True
579+
self.text = text
580+
581+
def __str__(self) -> str:
582+
return self.text
583+
584+
def __eq__(self, other) -> bool:
585+
return self.is_message == other.is_message and self.text == other.text
586+
587+
def get_data(self):
588+
return {
589+
"message": False,
590+
"text": self.text,
591+
}
592+
593+
594+
class Output:
595+
def max_stored_size(self, settings) -> int:
596+
return settings.MAX_STORED_SIZE
597+
598+
def out(self, out):
599+
pass
600+
601+
def clear(self, wait):
602+
raise NotImplementedError
603+
604+
def display(self, data, metadata):
605+
raise NotImplementedError
606+
607+
608+
OutputLines = List[str]
609+
610+
611+
class Result:
612+
"""
613+
A structure containing the result of an evaluation.
614+
615+
In particular, there are the following fields:
616+
617+
result: the actual result produced. However the dataset and form of this is influenced by "form".
618+
out: a list of additional output product
619+
"""
620+
621+
def __init__(
622+
self, out: OutputLines, result, line_no: int, last_eval=None, form=None
623+
) -> None:
624+
self.out = out
625+
self.result = result
626+
self.line_no = line_no
627+
self.last_eval = last_eval
628+
self.form = form
629+
630+
# FIXME: consider using a named tuple
631+
def get_data(self) -> dict:
632+
return {
633+
"out": [out.get_data() for out in self.out],
634+
"result": self.result,
635+
"line": self.line_no,
636+
"form": self.form,
637+
}

0 commit comments

Comments
 (0)