Skip to content

Commit 9a7376f

Browse files
authored
Improve traceprint (#1484)
This PR changes `TracePrint` to show the result through the `Print[]` interface. This allows us to show the result in the browser/CLI/docpipeline instead of the (raw) terminal. This is how it looks in the PDF documentation: <img width="743" height="677" alt="imagen" src="https://github.com/user-attachments/assets/c6dd2ebd-513a-4f77-a109-23a00739c355" /> and this is how it looks in Mathics-Django help <img width="787" height="683" alt="imagen" src="https://github.com/user-attachments/assets/9ffe7427-71bc-4693-9acc-9d2d24e9a951" />
1 parent 28dd3df commit 9a7376f

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

mathics/builtin/trace.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
strip_context,
4242
)
4343

44+
SymbolTableForm = Symbol("System`TableForm")
45+
4446

4547
def traced_apply_function(
4648
self, expression, vars, options: dict, evaluation: Evaluation
@@ -95,7 +97,7 @@ class ClearTrace(Builtin):
9597
9698
Dump Builtin-Function statistics gathered in running that assignment:
9799
>> PrintTrace[]
98-
100+
| ...
99101
>> ClearTrace[]
100102
101103
#> $TraceBuiltins = False
@@ -136,12 +138,13 @@ class PrintTrace(_TraceBase):
136138
137139
Note: before '\$TraceBuiltins' is set to 'True', 'PrintTrace[]' will print an empty
138140
list.
139-
>> PrintTrace[] (* See console log *)
140-
141+
>> PrintTrace[]
142+
| ...
141143
>> $TraceBuiltins = True
142144
= True
143145
144146
>> PrintTrace[SortBy -> "time"]
147+
| ...
145148
146149
#> $TraceBuiltins = False
147150
= False
@@ -180,22 +183,26 @@ class TraceBuiltins(_TraceBase):
180183
</ul>
181184
182185
183-
>> TraceBuiltins[Graphics3D[Tetrahedron[]]] (* See console log *)
186+
>> TraceBuiltins[Graphics3D[Tetrahedron[]]]
187+
| ...
184188
= -Graphics3D-
185189
186190
By default, the output is sorted by the name:
187-
>> TraceBuiltins[Times[x, x]] (* See console log *)
191+
>> TraceBuiltins[Times[x, x]]
192+
| ...
188193
= x ^ 2
189194
190195
By default, the output is sorted by the number of calls of the builtin from \
191196
highest to lowest:
192-
>> TraceBuiltins[Times[x, x], SortBy->"count"] (* See console log *)
197+
>> TraceBuiltins[Times[x, x], SortBy->"count"]
198+
| ...
193199
= x ^ 2
194200
195201
You can have results ordered by name, or time.
196202
197203
Trace an expression and list the result by time from highest to lowest.
198-
>> TraceBuiltins[Times[x, x], SortBy->"time"] (* See console log *)
204+
>> TraceBuiltins[Times[x, x], SortBy->"time"]
205+
| ...
199206
= x ^ 2
200207
"""
201208

@@ -231,7 +238,13 @@ def sort_by_time(tup: tuple):
231238
def sort_by_name(tup: tuple):
232239
return tup[0]
233240

234-
print("count ms Builtin name")
241+
header = [
242+
(
243+
" count",
244+
"ms",
245+
"Builtin name",
246+
)
247+
]
235248

236249
if sort_by == "count":
237250
inverse = True
@@ -243,16 +256,20 @@ def sort_by_name(tup: tuple):
243256
inverse = False
244257
sort_fn = sort_by_name
245258

246-
for name, statistic in sorted(
247-
TraceBuiltins.function_stats.items(),
248-
key=sort_fn,
249-
reverse=inverse,
250-
):
251259
# TODO: show a table through a message...
252-
print(
253-
"%5d %6g %s"
254-
% (statistic["count"], int(statistic["elapsed_milliseconds"]), name)
260+
table = header + [
261+
(
262+
statistic["count"],
263+
int(statistic["elapsed_milliseconds"]),
264+
name,
265+
)
266+
for name, statistic in sorted(
267+
TraceBuiltins.function_stats.items(),
268+
key=sort_fn,
269+
reverse=inverse,
255270
)
271+
]
272+
evaluation.print_out(Expression(SymbolTableForm, from_python(table)))
256273

257274
@staticmethod
258275
def enable_trace(evaluation) -> None:

mathics/doc/latex/mathics.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@
232232
}
233233
% Test case print output
234234
\newenvironment{testprint}{\item
235-
\vspace{-0.5em}
235+
\vspace{0.0em}
236236
\begin{minipage}{\codewidth}
237237
\begin{ttfamily}%
238238
\vspace{0.1em}%

0 commit comments

Comments
 (0)