Skip to content

Commit 273ca51

Browse files
mmaterarocky
authored andcommitted
more singletonizing
1 parent 79e1369 commit 273ca51

10 files changed

Lines changed: 88 additions & 77 deletions

File tree

mathics/builtin/arithmetic.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@
3131
Real,
3232
String,
3333
Symbol,
34-
SymbolN,
34+
SymbolComplexInfinity,
35+
SymbolDirectedInfinity,
3536
SymbolFalse,
37+
SymbolInfinity,
38+
SymbolN,
3639
SymbolNull,
40+
SymbolSequence,
3741
SymbolTrue,
38-
SymbolList,
39-
SymbolInfinity,
40-
SymbolDirectedInfinity,
41-
SymbolComplexInfinity,
42-
from_python,
4342
from_mpmath,
43+
from_python,
4444
)
4545
from mathics.core.numbers import min_prec, dps, SpecialValueError
4646

@@ -964,7 +964,7 @@ def apply_check(self, x, y, evaluation):
964964
factor = self.apply(Expression("Sequence", x.imag, y), evaluation)
965965
return Expression("Times", factor, Expression("Power", Integer(-1), yhalf))
966966

967-
result = self.apply(Expression("Sequence", x, y), evaluation)
967+
result = self.apply(Expression(SymbolSequence, x, y), evaluation)
968968
if result is None or result != SymbolNull:
969969
return result
970970

mathics/builtin/calculus.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from mathics.version import __version__ # noqa used in loading to check consistency.
77

88
from mathics.builtin.base import Builtin, PostfixOperator, SympyFunction
9-
from mathics.core.expression import Expression, Integer, Number, SymbolTrue, SymbolFalse, SymbolList, SymbolN
9+
from mathics.core.expression import Expression, Integer, Number, SymbolTrue, SymbolFalse, SymbolList, SymbolN, SymbolRule
1010
from mathics.core.convert import sympy_symbol_prefix, SympyExpression, from_sympy
1111
from mathics.core.rules import Pattern
1212
from mathics.core.numbers import dps
@@ -842,7 +842,7 @@ def transform_solution(sol):
842842
Expression(
843843
"List",
844844
*(
845-
Expression("Rule", var, from_sympy(sol[var_sympy]))
845+
Expression(SymbolRule, var, from_sympy(sol[var_sympy]))
846846
for var, var_sympy in zip(vars, vars_sympy)
847847
if var_sympy in sol
848848
)
@@ -1141,4 +1141,4 @@ def sub(evaluation):
11411141
else:
11421142
evaluation.message("FindRoot", "maxiter")
11431143

1144-
return Expression(SymbolList, Expression("Rule", x, x0))
1144+
return Expression(SymbolList, Expression(SymbolRule, x, x0))

mathics/builtin/graphics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
Symbol,
2929
SymbolList,
3030
SymbolN,
31+
SymbolMakeBoxes,
3132
strip_context,
3233
system_symbols,
3334
system_symbols_dict,
@@ -292,7 +293,7 @@ def _CMC_distance(lab1, lab2, l, c):
292293

293294

294295
def _extract_graphics(graphics, format, evaluation):
295-
graphics_box = Expression("MakeBoxes", graphics).evaluate(evaluation)
296+
graphics_box = Expression(SymbolMakeBoxes, graphics).evaluate(evaluation)
296297
builtin = GraphicsBox(expression=False)
297298
elements, calc_dimensions = builtin._prepare_elements(
298299
graphics_box.leaves, {"evaluation": evaluation}, neg_y=True

mathics/builtin/image.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
MachineReal,
1616
Symbol,
1717
SymbolNull,
18+
SymbolList,
19+
SymbolRule,
1820
from_python,
1921
)
2022
from mathics.builtin.colors import (
@@ -194,7 +196,7 @@ def extract(im, evaluation):
194196
else:
195197
continue
196198

197-
yield Expression("Rule", String(_Exif._names.get(k, name)), value)
199+
yield Expression(SymbolRule, String(_Exif._names.get(k, name)), value)
198200

199201

200202
class ImageImport(_ImageBuiltin):
@@ -217,15 +219,15 @@ def apply(self, path, evaluation):
217219
pillow = PIL.Image.open(path.get_string_value())
218220
pixels = numpy.asarray(pillow)
219221
is_rgb = len(pixels.shape) >= 3 and pixels.shape[2] >= 3
220-
exif = Expression("List", *list(_Exif.extract(pillow, evaluation)))
222+
exif = Expression(SymbolList, *list(_Exif.extract(pillow, evaluation)))
221223

222224
image = Image(pixels, "RGB" if is_rgb else "Grayscale")
223225
return Expression(
224226
"List",
225-
Expression("Rule", String("Image"), image),
226-
Expression("Rule", String("ColorSpace"), String(image.color_space)),
227-
Expression("Rule", String("ImageSize"), from_python(image.dimensions())),
228-
Expression("Rule", String("RawExif"), exif),
227+
Expression(SymbolRule, String("Image"), image),
228+
Expression(SymbolRule, String("ColorSpace"), String(image.color_space)),
229+
Expression(SymbolRule, String("ImageSize"), from_python(image.dimensions())),
230+
Expression(SymbolRule, String("RawExif"), exif),
229231
)
230232

231233

@@ -569,7 +571,7 @@ def apply_resize_width_height(self, image, width, height, evaluation, options):
569571
h = self._get_image_size_spec(old_h, height)
570572
if h is None or w is None:
571573
return evaluation.message(
572-
"ImageResize", "imgrssz", Expression("List", width, height)
574+
"ImageResize", "imgrssz", Expression(SymbolList, width, height)
573575
)
574576

575577
# handle Automatic
@@ -696,7 +698,7 @@ def apply(self, image, orig, dest, evaluation):
696698

697699
if method is None:
698700
return evaluation.message(
699-
"ImageReflect", "bdrfl2", Expression("Rule", orig, dest)
701+
"ImageReflect", "bdrfl2", Expression(SymbolRule, orig, dest)
700702
)
701703

702704
return Image(method(image.pixels), image.color_space)
@@ -1041,7 +1043,7 @@ def apply(self, image, r, t, evaluation):
10411043

10421044

10431045
def _matrix(rows):
1044-
return Expression("List", *[Expression("List", *r) for r in rows])
1046+
return Expression(SymbolList, *[Expression(SymbolList, *r) for r in rows])
10451047

10461048

10471049
class BoxMatrix(_ImageBuiltin):
@@ -1467,7 +1469,7 @@ def apply(self, image, evaluation):
14671469
else:
14681470
for i in range(pixels.shape[2]):
14691471
images.append(Image(pixels[:, :, i], "Grayscale"))
1470-
return Expression("List", *images)
1472+
return Expression(SymbolList, *images)
14711473

14721474

14731475
class ColorCombine(_ImageBuiltin):
@@ -1755,7 +1757,7 @@ def result():
17551757
else:
17561758
yield Expression(out_palette_head, *prototype)
17571759

1758-
return Expression("List", *itertools.islice(result(), 0, at_most))
1760+
return Expression(SymbolList, *itertools.islice(result(), 0, at_most))
17591761

17601762

17611763
# pixel access
@@ -1893,7 +1895,7 @@ def apply(self, image, x, y, evaluation):
18931895
return evaluation.message("PixelValue", "nopad")
18941896
pixel = pixels_as_float(image.pixels)[height - y, x - 1]
18951897
if isinstance(pixel, (numpy.ndarray, numpy.generic, list)):
1896-
return Expression("List", *[MachineReal(float(x)) for x in list(pixel)])
1898+
return Expression(SymbolList, *[MachineReal(float(x)) for x in list(pixel)])
18971899
else:
18981900
return MachineReal(float(pixel))
18991901

@@ -1940,7 +1942,7 @@ def apply(self, image, val, d, evaluation):
19401942
result = sorted(
19411943
(j + 1, height - i, k + 1) for i, j, k in positions.tolist()
19421944
)
1943-
return Expression("List", *(Expression("List", *arg) for arg in result))
1945+
return Expression(SymbolList, *(Expression(SymbolList, *arg) for arg in result))
19441946

19451947

19461948
# image attribute queries
@@ -1968,7 +1970,7 @@ class ImageDimensions(_ImageBuiltin):
19681970

19691971
def apply(self, image, evaluation):
19701972
"ImageDimensions[image_Image]"
1971-
return Expression("List", *image.dimensions())
1973+
return Expression(SymbolList, *image.dimensions())
19721974

19731975

19741976
class ImageAspectRatio(_ImageBuiltin):
@@ -2297,8 +2299,8 @@ def storage_type(self):
22972299
def options(self):
22982300
return Expression(
22992301
"List",
2300-
Expression("Rule", String("ColorSpace"), String(self.color_space)),
2301-
Expression("Rule", String("MetaInformation"), self.metadata),
2302+
Expression(SymbolRule, String("ColorSpace"), String(self.color_space)),
2303+
Expression(SymbolRule, String("MetaInformation"), self.metadata),
23022304
)
23032305

23042306

mathics/builtin/importexport.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from mathics.core.expression import (
1010
ByteArrayAtom,
1111
SymbolList,
12+
SymbolRule,
1213
Expression,
1314
from_python,
1415
strip_context,
@@ -904,7 +905,7 @@ def _importer_exporter_options(
904905
if py_name:
905906
value = get_option(remaining_options, py_name, evaluation, pop=True)
906907
if value is not None:
907-
expr = Expression("Rule", String(py_name), value)
908+
expr = Expression(SymbolRule, String(py_name), value)
908909
if py_name == "CharacterEncoding":
909910
stream_options.append(expr)
910911
else:
@@ -917,7 +918,7 @@ def _importer_exporter_options(
917918
evaluation.message(
918919
builtin_name,
919920
"optx",
920-
Expression("Rule", strip_context(name), value),
921+
Expression(SymbolRule, strip_context(name), value),
921922
strip_context(builtin_name),
922923
)
923924

@@ -1478,7 +1479,7 @@ def get_results(tmp_function, findfile):
14781479
return Expression(
14791480
"List",
14801481
*(
1481-
Expression("Rule", String(key), defaults[key])
1482+
Expression(SymbolRule, String(key), defaults[key])
14821483
for key in defaults.keys()
14831484
)
14841485
)

0 commit comments

Comments
 (0)