Skip to content

Commit 539a997

Browse files
author
Matthias Koeppe
committed
sage.plot, sage.repl: Modularization fixes (imports), # needs
1 parent 31e2166 commit 539a997

File tree

9 files changed

+58
-37
lines changed

9 files changed

+58
-37
lines changed

src/sage/plot/graphics.py

Lines changed: 5 additions & 5 deletions
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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
from sage.rings.cc import CC
2323
from sage.rings.integer import Integer
2424
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
25+
from sage.misc.lazy_import import lazy_import
26+
lazy_import("sage.symbolic.constants", ["pi", "e"])
27+
lazy_import("sage.functions.hyperbolic", "arccosh")
28+
lazy_import("sage.functions.trig", ["sin", "cos", "cot"])
2829
from sage.misc.functional import is_odd
2930
from sage.matrix.constructor import matrix
3031

src/sage/plot/plot3d/plot3d.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ def f(x,y): return math.exp(x/5)*math.cos(y)
154154
from sage.plot.colors import rainbow
155155
from .texture import Texture
156156

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

160161

src/sage/repl/display/formatter.py

Lines changed: 17 additions & 13 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
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
39+
40+
from ipywidgets.widgets import SelectionSlider, ValueWidget, ToggleButtons
41+
from ipywidgets.widgets.interaction import interactive, signature
42+
4143
from .widgets import EvalText, SageColorPicker
4244
from sage.structure.element import parent
4345
import sage.rings.abc
@@ -173,8 +175,14 @@ def widget_from_single_value(cls, abbrev, *args, **kwds):
173175

174176
return input_grid(abbrev.nrows(), abbrev.ncols(),
175177
default=abbrev.list(), to_value=abbrev.parent())
176-
if isinstance(abbrev, Color):
177-
return SageColorPicker(value=abbrev.html_color())
178+
179+
try:
180+
from sage.plot.colors import Color
181+
except ImportError:
182+
pass
183+
else:
184+
if isinstance(abbrev, Color):
185+
return SageColorPicker(value=abbrev.html_color())
178186
# Get widget from IPython if possible
179187
widget = super().widget_from_single_value(abbrev, *args, **kwds)
180188
if widget is not None or isinstance(abbrev, Iterable):

src/sage/repl/ipython_kernel/widgets.py

Lines changed: 11 additions & 6 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 2 deletions
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)