Skip to content

Commit a689e7d

Browse files
author
Release Manager
committed
sagemathgh-38181: `sage.plot`, `sage.repl`: Modularization fixes (imports), `# needs` <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Also removing a use of `SR`, when testing for `Expression` is good enough. - Cherry-picked from sagemath#35095. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#38181 Reported by: Matthias Köppe Reviewer(s): Kwankyu Lee, Matthias Köppe
2 parents fd71f04 + 5dfa06c commit a689e7d

File tree

9 files changed

+66
-48
lines changed

9 files changed

+66
-48
lines changed

src/sage/plot/graphics.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -2438,15 +2438,15 @@ def _matplotlib_tick_formatter(self, subplot, base=(10, 10),
24382438
x_formatter, y_formatter = tick_formatter
24392439
from matplotlib.ticker import FuncFormatter, FixedFormatter
24402440
from sage.misc.latex import latex
2441-
from sage.symbolic.ring import SR
2441+
from sage.structure.element import Expression
24422442
from .misc import _multiple_of_constant
24432443
# ---------------------- Formatting x-ticks ----------------------
24442444
if x_formatter is None:
24452445
if scale[0] == 'log':
24462446
x_formatter = LogFormatterMathtext(base=base[0])
24472447
else:
24482448
x_formatter = ScalarFormatter()
2449-
elif x_formatter in SR:
2449+
elif isinstance(x_formatter, Expression):
24502450
x_const = x_formatter
24512451
x_formatter = FuncFormatter(lambda n, pos:
24522452
_multiple_of_constant(n, pos, x_const))
@@ -2475,7 +2475,7 @@ def _matplotlib_tick_formatter(self, subplot, base=(10, 10),
24752475
y_formatter = LogFormatterMathtext(base=base[1])
24762476
else:
24772477
y_formatter = ScalarFormatter()
2478-
elif y_formatter in SR:
2478+
elif isinstance(y_formatter, Expression):
24792479
y_const = y_formatter
24802480
y_formatter = FuncFormatter(lambda n, pos:
24812481
_multiple_of_constant(n, pos, y_const))
@@ -2763,10 +2763,10 @@ def matplotlib(self, filename=None,
27632763
if stylesheet in plt.style.available:
27642764
plt.style.use(stylesheet)
27652765

2766-
from sage.symbolic.ring import SR
2766+
from sage.structure.element import Expression
27672767
# make sure both formatters typeset or both don't
27682768
if not isinstance(tick_formatter, (list, tuple)):
2769-
if tick_formatter == "latex" or tick_formatter in SR:
2769+
if tick_formatter == "latex" or isinstance(tick_formatter, Expression):
27702770
tick_formatter = (tick_formatter, "latex")
27712771
else:
27722772
tick_formatter = (tick_formatter, None)

src/sage/plot/hyperbolic_regular_polygon.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@
1717
# https://www.gnu.org/licenses/
1818
#*****************************************************************************
1919

20+
from sage.matrix.constructor import matrix
21+
from sage.misc.decorators import options, rename_keyword
22+
from sage.misc.functional import is_odd
23+
from sage.misc.lazy_import import lazy_import
2024
from sage.plot.hyperbolic_polygon import HyperbolicPolygon
21-
from sage.plot.all import Graphics
25+
from sage.plot.plot import Graphics
2226
from sage.rings.cc import CC
2327
from sage.rings.integer import Integer
24-
from sage.misc.decorators import options, rename_keyword
25-
from sage.symbolic.constants import pi, e
26-
from sage.functions.hyperbolic import arccosh
27-
from sage.functions.trig import sin, cos, cot
28-
from sage.misc.functional import is_odd
29-
from sage.matrix.constructor import matrix
28+
29+
lazy_import("sage.functions.hyperbolic", "arccosh")
30+
lazy_import("sage.functions.trig", ["sin", "cos", "cot"])
31+
lazy_import("sage.symbolic.constants", ["pi", "e"])
3032

3133

3234
class HyperbolicRegularPolygon(HyperbolicPolygon):

src/sage/plot/plot3d/plot3d.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,16 @@ def f(x,y): return math.exp(x/5)*math.cos(y)
147147
# https://www.gnu.org/licenses/
148148
# ****************************************************************************
149149

150-
from .tri_plot import TrianglePlot
151-
from .index_face_set import IndexFaceSet
152-
from .shapes import arrow3d
153-
from .base import Graphics3dGroup
150+
from sage.misc.lazy_import import lazy_import
151+
from sage.misc.sageinspect import sage_getargspec, is_function_or_cython_function
154152
from sage.plot.colors import rainbow
155-
from .texture import Texture
153+
from sage.plot.plot3d.base import Graphics3dGroup
154+
from sage.plot.plot3d.index_face_set import IndexFaceSet
155+
from sage.plot.plot3d.shapes import arrow3d
156+
from sage.plot.plot3d.texture import Texture
157+
from sage.plot.plot3d.tri_plot import TrianglePlot
156158

157-
from sage.functions.trig import cos, sin
158-
from sage.misc.sageinspect import sage_getargspec, is_function_or_cython_function
159+
lazy_import("sage.functions.trig", ["cos", "sin"])
159160

160161

161162
class _Coordinates:

src/sage/repl/display/formatter.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
contains a new facility for displaying lists of matrices in an easier
88
to read format::
99
10-
sage: [identity_matrix(i) for i in range(2,5)]
10+
sage: [identity_matrix(i) for i in range(2, 5)] # needs sage.modules
1111
[
1212
[1 0 0 0]
1313
[1 0 0] [0 1 0 0]
@@ -236,19 +236,23 @@ def _ipython_float_precision_changed(change):
236236
sage: shell.run_cell('matrix.options.precision') # indirect doctest # needs sage.modules
237237
None
238238
"""
239-
from sage.matrix.constructor import options
240-
s = change.new
241-
if not s:
242-
# unset the precision
243-
options.precision = None
239+
try:
240+
from sage.matrix.constructor import options
241+
except ImportError:
242+
pass
244243
else:
245-
try:
246-
prec = int(s)
247-
if prec >= 0:
248-
options.precision = prec
249-
# otherwise ignore the change
250-
except ValueError:
251-
pass
244+
s = change.new
245+
if not s:
246+
# unset the precision
247+
options.precision = None
248+
else:
249+
try:
250+
prec = int(s)
251+
if prec >= 0:
252+
options.precision = prec
253+
# otherwise ignore the change
254+
except ValueError:
255+
pass
252256

253257

254258
class SagePlainTextFormatter(PlainTextFormatter):

src/sage/repl/image.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# sage_setup: distribution = sagemath-repl
2+
# sage.doctest: needs pillow
23
"""
34
Sage Wrapper for Bitmap Images
45

src/sage/repl/ipython_kernel/interact.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@
3434
# https://www.gnu.org/licenses/
3535
# ****************************************************************************
3636

37-
from ipywidgets.widgets import SelectionSlider, ValueWidget, ToggleButtons
38-
from ipywidgets.widgets.interaction import interactive, signature
3937
from collections import OrderedDict
4038
from collections.abc import Iterable, Iterator
41-
from .widgets import EvalText, SageColorPicker
42-
from sage.structure.element import parent
39+
40+
from ipywidgets.widgets import SelectionSlider, ValueWidget, ToggleButtons
41+
from ipywidgets.widgets.interaction import interactive, signature
42+
4343
import sage.rings.abc
44+
4445
from sage.misc.lazy_import import lazy_import
45-
from sage.structure.element import Matrix
46+
from sage.repl.ipython_kernel.widgets import EvalText, SageColorPicker
47+
from sage.structure.element import Matrix, parent
48+
4649
lazy_import("sage.plot.colors", "Color")
4750

4851

@@ -173,6 +176,7 @@ def widget_from_single_value(cls, abbrev, *args, **kwds):
173176

174177
return input_grid(abbrev.nrows(), abbrev.ncols(),
175178
default=abbrev.list(), to_value=abbrev.parent())
179+
176180
if isinstance(abbrev, Color):
177181
return SageColorPicker(value=abbrev.html_color())
178182
# Get widget from IPython if possible

src/sage/repl/ipython_kernel/widgets.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class TransformWidget():
101101
sage: from ipywidgets import ToggleButtons
102102
sage: from sage.repl.ipython_kernel.widgets import TransformWidget
103103
sage: class TransformToggleButtons(TransformWidget, ToggleButtons): pass
104-
sage: w = TransformToggleButtons(options=["pi", "e"], transform=lambda x: x+x)
104+
sage: w = TransformToggleButtons(options=["pi", "e"], transform=lambda x: x + x)
105105
sage: w
106106
TransformToggleButtons(options=('pi', 'e'), value='pi')
107107
sage: w.get_interact_value()
@@ -240,7 +240,8 @@ class TransformIntRangeSlider(TransformWidget, IntRangeSlider):
240240
EXAMPLES::
241241
242242
sage: from sage.repl.ipython_kernel.widgets import TransformIntRangeSlider
243-
sage: w = TransformIntRangeSlider(min=0, max=100, value=(7,9), transform=lambda x: x[1]-x[0])
243+
sage: w = TransformIntRangeSlider(min=0, max=100, value=(7, 9),
244+
....: transform=lambda x: x[1] - x[0])
244245
sage: w
245246
TransformIntRangeSlider(value=(7, 9))
246247
sage: w.get_interact_value()
@@ -257,7 +258,8 @@ class TransformFloatRangeSlider(TransformWidget, FloatRangeSlider):
257258
EXAMPLES::
258259
259260
sage: from sage.repl.ipython_kernel.widgets import TransformFloatRangeSlider
260-
sage: w = TransformFloatRangeSlider(min=0, max=100, value=(7,9), transform=lambda x: x[1]-x[0])
261+
sage: w = TransformFloatRangeSlider(min=0, max=100, value=(7, 9),
262+
....: transform=lambda x: x[1] - x[0])
261263
sage: w
262264
TransformFloatRangeSlider(value=(7.0, 9.0))
263265
sage: w.get_interact_value()
@@ -274,7 +276,7 @@ class TransformText(TransformWidget, Text):
274276
EXAMPLES::
275277
276278
sage: from sage.repl.ipython_kernel.widgets import TransformText
277-
sage: w = TransformText(value="hello", transform=lambda x: x+x)
279+
sage: w = TransformText(value="hello", transform=lambda x: x + x)
278280
sage: w
279281
TransformText(value='hello')
280282
sage: w.get_interact_value()
@@ -291,7 +293,7 @@ class TransformTextarea(TransformWidget, Textarea):
291293
EXAMPLES::
292294
293295
sage: from sage.repl.ipython_kernel.widgets import TransformTextarea
294-
sage: w = TransformTextarea(value="hello", transform=lambda x: x+x)
296+
sage: w = TransformTextarea(value="hello", transform=lambda x: x + x)
295297
sage: w
296298
TransformTextarea(value='hello')
297299
sage: w.get_interact_value()
@@ -371,7 +373,10 @@ class Grid(TransformWidget, HBox, ValueWidget):
371373
sage: from sage.repl.ipython_kernel.widgets import Grid
372374
sage: w = Grid(2, 2, lambda i,j: Text(value="%s,%s"%(i,j)))
373375
sage: w
374-
Grid(value=[['0,0', '0,1'], ['1,0', '1,1']], children=(Label(value=''), VBox(children=(Text(value='0,0'), Text(value='1,0'))), VBox(children=(Text(value='0,1'), Text(value='1,1')))))
376+
Grid(value=[['0,0', '0,1'], ['1,0', '1,1']],
377+
children=(Label(value=''),
378+
VBox(children=(Text(value='0,0'), Text(value='1,0'))),
379+
VBox(children=(Text(value='0,1'), Text(value='1,1')))))
375380
sage: w.get_interact_value()
376381
[['0,0', '0,1'], ['1,0', '1,1']]
377382
"""

src/sage/repl/preparse.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
4
6565
sage: 87.factor()
6666
3 * 29
67-
sage: 15.10.sqrt()
67+
sage: 15.10.sqrt() # needs sage.rings.real_mpfr
6868
3.88587184554509
6969
sage: preparse('87.sqrt()')
7070
'Integer(87).sqrt()'
@@ -83,7 +83,7 @@
8383
Symbolic functional notation::
8484
8585
sage: # needs sage.symbolic
86-
sage: a=10; f(theta, beta) = theta + beta; b = x^2 + theta
86+
sage: a = 10; f(theta, beta) = theta + beta; b = x^2 + theta
8787
sage: f
8888
(theta, beta) |--> beta + theta
8989
sage: a
@@ -1032,7 +1032,7 @@ def parse_ellipsis(code, preparse_step=True):
10321032
'(ellipsis_range(1,2,Ellipsis,n))'
10331033
sage: parse_ellipsis("for i in (f(x) .. L[10]):")
10341034
'for i in (ellipsis_iter(f(x) ,Ellipsis, L[10])):'
1035-
sage: [1.0..2.0]
1035+
sage: [1.0..2.0] # needs sage.rings.real_mpfr
10361036
[1.00000000000000, 2.00000000000000]
10371037
10381038
TESTS:

src/sage/repl/user_globals.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ def initialize_globals(all, g=None):
164164

165165
def get_global(name):
166166
"""
167-
Return the value of global variable ``name``. Raise ``NameError``
168-
if there is no such global variable.
167+
Return the value of global variable ``name``.
168+
169+
Raise :class:`NameError` if there is no such global variable.
169170
170171
INPUT:
171172

0 commit comments

Comments
 (0)