Skip to content

Commit 9e3ec3b

Browse files
rockymmatera
andauthored
Use get_eval_Expression() more often (#1446)
Use `get_eval_Expression()` more often Also, remove some duplicate error message definitions One error message corrected - we had a duplicate stray message added in there. --------- Co-authored-by: Juan Mauricio Matera <matera@fisica.unlp.edu.ar>
1 parent 27ce9f2 commit 9e3ec3b

File tree

16 files changed

+38
-71
lines changed

16 files changed

+38
-71
lines changed

mathics/builtin/arithfns/basic.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
)
4040
from mathics.core.convert.expression import to_expression
4141
from mathics.core.convert.sympy import from_sympy
42+
from mathics.core.evaluation import Evaluation
4243
from mathics.core.expression import Expression
4344
from mathics.core.list import ListExpression
4445
from mathics.core.symbols import (
@@ -283,7 +284,7 @@ class Plus(InfixOperator, SympyFunction):
283284
# Remember to up sympy doc link when this is corrected
284285
sympy_name = "Add"
285286

286-
def format_plus(self, items, evaluation):
287+
def format_plus(self, items, evaluation: Evaluation):
287288
"Plus[items__]"
288289

289290
def negate(item): # -> Expression (see FIXME below)
@@ -334,7 +335,7 @@ def is_negative(value) -> bool:
334335
SymbolLeft,
335336
)
336337

337-
def eval(self, items, evaluation):
338+
def eval(self, items, evaluation: Evaluation):
338339
"Plus[items___]"
339340
items_tuple = numerify(items, evaluation).get_sequence()
340341
return eval_Plus(*items_tuple)
@@ -437,7 +438,7 @@ class Power(InfixOperator, MPMathFunction):
437438
# Remember to up sympy doc link when this is corrected
438439
sympy_name = "Pow"
439440

440-
def eval_check(self, x, y, evaluation):
441+
def eval_check(self, x, y, evaluation: Evaluation):
441442
"Power[x_, y_]"
442443

443444
# Power uses MPMathFunction but does some error checking first

mathics/builtin/atomic/symbols.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
)
4040
from mathics.core.systemsymbols import (
4141
SymbolAttributes,
42-
SymbolContext,
4342
SymbolDefinition,
4443
SymbolFormat,
4544
SymbolGrid,
@@ -50,6 +49,7 @@
5049
SymbolSet,
5150
)
5251
from mathics.doc.online import online_doc_string
52+
from mathics.eval.stackframe import get_eval_Expression
5353

5454

5555
def gather_and_format_definition_rules(
@@ -204,9 +204,7 @@ def eval(self, symbol, evaluation):
204204

205205
name = symbol.get_name()
206206
if not name:
207-
evaluation.message(
208-
"Context", "normal", Integer1, Expression(SymbolContext, symbol)
209-
)
207+
evaluation.message("Context", "normal", Integer1, get_eval_Expression())
210208
return
211209
assert "`" in name
212210
context = name[: name.rindex("`") + 1]

mathics/builtin/colors/color_operations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import itertools
1010
from math import floor
1111

12+
import numpy
13+
import PIL.ImageOps
14+
1215
from mathics.builtin.colors.color_directives import ColorError, RGBColor, _ColorObject
1316
from mathics.builtin.colors.color_internals import convert_color
1417
from mathics.builtin.image.base import Image
@@ -23,9 +26,6 @@
2326

2427
_image_requires = ("numpy", "PIL")
2528

26-
import numpy
27-
import PIL.ImageOps
28-
2929

3030
class Blend(Builtin):
3131
"""

mathics/builtin/directories/directory_operations.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class CreateDirectory(Builtin):
4747
"fstr": (
4848
"File specification `1` is not a string of " "one or more characters."
4949
),
50-
"nffil": "File not found during `1`.",
5150
"filex": "`1` already exists.",
5251
}
5352
summary_text = "create a directory"

mathics/builtin/distance/clusters.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
SymbolClusteringComponents,
2626
SymbolFailed,
2727
SymbolFindClusters,
28+
SymbolMethod,
2829
SymbolRule,
2930
)
3031
from mathics.eval.distance.clusters import (
@@ -35,6 +36,7 @@
3536
)
3637
from mathics.eval.nevaluator import eval_N
3738
from mathics.eval.parts import walk_levels
39+
from mathics.eval.stackframe import get_eval_Expression
3840
from mathics.eval.tensors import get_default_distance
3941

4042

@@ -74,7 +76,6 @@ class _Cluster(Builtin):
7476
"amtd": "`1` failed to pick a suitable distance function for `2`.",
7577
"bdmtd": 'Method in `` must be either "Optimize", "Agglomerate" or "KMeans".',
7678
"intpm": "Positive integer expected at position 2 in ``.",
77-
"list": "Expected a list or a rule with equally sized lists at position 1 in ``.",
7879
"nclst": "Cannot find more clusters than there are elements: `1` is larger than `2`.",
7980
"xnum": "The distance function returned ``, which is not a non-negative real value.",
8081
"rseed": "The random seed specified through `` must be an integer or Automatic.",
@@ -91,7 +92,7 @@ def _cluster(self, p, k, mode, evaluation, options, expr):
9192
method_string, method = self.get_option_string(options, "Method", evaluation)
9293
if method_string not in ("Optimize", "Agglomerate", "KMeans"):
9394
evaluation.message(
94-
self.get_name(), "bdmtd", Expression(SymbolRule, "Method", method)
95+
self.get_name(), "bdmtd", Expression(SymbolRule, SymbolMethod, method)
9596
)
9697
return
9798

@@ -407,7 +408,6 @@ class Nearest(Builtin):
407408

408409
messages = {
409410
"amtd": "`1` failed to pick a suitable distance function for `2`.",
410-
"list": "Expected a list or a rule with equally sized lists at position 1 in ``.",
411411
"nimp": "Method `1` is not implemented yet.",
412412
}
413413

@@ -463,9 +463,7 @@ def eval(
463463
if distance_function_string == "Automatic":
464464
distance_function = get_default_distance(dist_p)
465465
if distance_function is None:
466-
evaluation.message(
467-
self.get_name(), "amtd", "Nearest", ListExpression(*dist_p)
468-
)
466+
evaluation.message(self.get_name(), "amtd", get_eval_Expression())
469467
return
470468

471469
if pivot.get_head_name() == "System`List":

mathics/builtin/exp_structure/general.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
Structural Expression Functions
44
"""
55

6-
from mathics.core.atoms import Integer, Integer1
6+
from mathics.core.atoms import Integer, Integer1, Integer2
77
from mathics.core.builtin import Builtin, InfixOperator, Predefined
88
from mathics.core.exceptions import InvalidLevelspecError
99
from mathics.core.expression import Evaluation, Expression
1010
from mathics.core.list import ListExpression
1111
from mathics.core.rules import BasePattern
1212
from mathics.core.symbols import Atom, SymbolFalse, SymbolTrue
13-
from mathics.core.systemsymbols import SymbolMap, SymbolSortBy
13+
from mathics.core.systemsymbols import SymbolMap
1414
from mathics.eval.parts import python_levelspec, walk_levels
15+
from mathics.eval.stackframe import get_eval_Expression
1516

1617

1718
class MapApply(InfixOperator):
@@ -264,13 +265,10 @@ def eval(self, li, f, evaluation: Evaluation):
264265
"SortBy[li_, f_]"
265266

266267
if isinstance(li, Atom):
267-
evaluation.message(
268-
"Sort", "normal", Integer1, Expression(SymbolSortBy, li, f)
269-
)
268+
evaluation.message("Sort", "normal", Integer1, get_eval_Expression())
270269
return
271270
elif li.get_head_name() != "System`List":
272-
expr = Expression(SymbolSortBy, li, f)
273-
evaluation.message(self.get_name(), "list", expr, 1)
271+
evaluation.message(self.get_name(), "list", get_eval_Expression(), Integer1)
274272
return
275273
else:
276274
keys_expr = Expression(SymbolMap, f, li).evaluate(evaluation) # precompute:
@@ -282,8 +280,7 @@ def eval(self, li, f, evaluation: Evaluation):
282280
or keys_expr.get_head_name() != "System`List"
283281
or len(keys_expr.elements) != len(li.elements)
284282
):
285-
expr = Expression(SymbolSortBy, li, f)
286-
evaluation.message("SortBy", "func", expr, 2)
283+
evaluation.message("SortBy", "func", get_eval_Expression(), Integer2)
287284
return
288285

289286
keys = keys_expr.elements

mathics/builtin/file_operations/file_properties.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class FileDate(Builtin):
5050
"""
5151

5252
messages = {
53-
"nffil": "File not found during `1`.",
5453
"datetype": (
5554
'Date type Fail should be "Access", "Modification", '
5655
'"Creation" (Windows only), '
@@ -248,7 +247,6 @@ class SetFileDate(Builtin):
248247
"fstr": (
249248
"File specification `1` is not a string of one or " "more characters."
250249
),
251-
"nffil": "File not found during `1`.",
252250
"fdate": (
253251
"Date specification should be either the number of seconds "
254252
"since January 1, 1900 or a {y, m, d, h, m, s} list."

mathics/builtin/files_io/files.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ def eval_input(self, exprs, name, n, evaluation: Evaluation):
587587
stream = stream_manager.lookup_stream(n.get_int_value())
588588

589589
if stream is None or stream.io.closed:
590-
evaluation.message("Put", "openx", to_expression("OutputSteam", name, n))
590+
evaluation.message("Put", "openx", get_eval_Expression())
591591
return
592592

593593
# In Mathics-server, evaluation.format_output is modified.
@@ -1304,11 +1304,7 @@ def eval(self, name, n, typ, m, evaluation: Evaluation, options: dict):
13041304

13051305
py_m = m.to_python()
13061306
if not (isinstance(py_m, int) and py_m > 0):
1307-
evaluation.message(
1308-
"Skip",
1309-
"intm",
1310-
to_expression("Skip", stream, typ, m),
1311-
)
1307+
evaluation.message("Skip", "intm", get_eval_Expression())
13121308
return
13131309
for i in range(py_m):
13141310
result = super(Skip, self).eval(stream, typ, evaluation, options)

mathics/builtin/files_io/filesystem.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
)
3737
from mathics.eval.directories import DIRECTORY_STACK
3838
from mathics.eval.files_io.files import eval_Get
39-
40-
SymbolAbsoluteTime = Symbol("AbsoluteTime")
39+
from mathics.eval.stackframe import get_eval_Expression
4140

4241

4342
class AbsoluteFileName(Builtin):
@@ -57,7 +56,6 @@ class AbsoluteFileName(Builtin):
5756

5857
messages = {
5958
"fstr": ("File specification x is not a string of one or more characters."),
60-
"nffil": "File not found during `1`.",
6159
}
6260
summary_text = "get absolute file path"
6361

@@ -155,7 +153,6 @@ class CopyFile(Builtin):
155153
"fstr": (
156154
"File specification `1` is not a string of " "one or more characters."
157155
),
158-
"nffil": "File not found during `1`.",
159156
}
160157
summary_text = "copy a file into a new path"
161158

@@ -192,9 +189,7 @@ def eval(self, source, dest, evaluation):
192189
try:
193190
shutil.copy(py_source, py_dest)
194191
except IOError:
195-
evaluation.message(
196-
"CopyFile", "nffil", to_expression("CopyFile", source, dest)
197-
)
192+
evaluation.message("CopyFile", "nffil", get_eval_Expression())
198193
return SymbolFailed
199194

200195
return dest
@@ -287,7 +282,6 @@ class DeleteFile(Builtin):
287282
"strs": (
288283
"String or non-empty list of strings expected at " "position `1` in `2`."
289284
),
290-
"nffil": "File not found during `1`.",
291285
}
292286
summary_text = "delete a file"
293287

@@ -303,10 +297,7 @@ def eval(self, filename, evaluation):
303297
# Check filenames
304298
if not isinstance(path, str):
305299
evaluation.message(
306-
"DeleteFile",
307-
"strs",
308-
filename,
309-
to_expression("DeleteFile", filename),
300+
"DeleteFile", "strs", filename, get_eval_Expression()
310301
)
311302
return
312303

@@ -315,9 +306,7 @@ def eval(self, filename, evaluation):
315306
path, _ = path_search(path)
316307

317308
if path is None:
318-
evaluation.message(
319-
"DeleteFile", "nffil", to_expression("DeleteFile", filename)
320-
)
309+
evaluation.message("DeleteFile", "nffil", get_eval_Expression())
321310
return SymbolFailed
322311
py_paths.append(path)
323312

@@ -397,9 +386,7 @@ def eval(self, name, evaluation):
397386
py_name = name.to_python()
398387

399388
if not (isinstance(py_name, str) and py_name[0] == py_name[-1] == '"'):
400-
evaluation.message(
401-
"ExpandFileName", "string", to_expression("ExpandFileName", name)
402-
)
389+
evaluation.message("ExpandFileName", "string", get_eval_Expression())
403390
return
404391
py_name = py_name[1:-1]
405392

@@ -618,7 +605,7 @@ def eval(self, name, evaluation):
618605
py_name = name.to_python()
619606

620607
if not (isinstance(py_name, str) and py_name[0] == py_name[-1] == '"'):
621-
evaluation.message("FindFile", "string", to_expression("FindFile", name))
608+
evaluation.message("FindFile", "string", get_eval_Expression())
622609
return
623610
py_name = py_name[1:-1]
624611

@@ -961,7 +948,6 @@ class RenameFile(Builtin):
961948
"fstr": (
962949
"File specification `1` is not a string of " "one or more characters."
963950
),
964-
"nffil": "File not found during `1`.",
965951
}
966952
summary_text = "change the name of a file"
967953

@@ -982,7 +968,7 @@ def eval(self, source, dest, evaluation):
982968
py_source = py_source[1:-1]
983969
py_dest = py_dest[1:-1]
984970

985-
py_source, is_temporary_file = path_search(py_source)
971+
py_source, _ = path_search(py_source)
986972

987973
if py_source is None:
988974
evaluation.message("RenameFile", "filex", source)

mathics/builtin/files_io/importexport.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,9 +2068,6 @@ class FileFormat(Builtin):
20682068
"""
20692069

20702070
summary_text = "determine the file format of a file"
2071-
messages = {
2072-
"nffil": "File not found during `1`.",
2073-
}
20742071

20752072
detector = None
20762073

0 commit comments

Comments
 (0)