From 8cb37957a2e64022b15c9088eba1d51f2c086b46 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Fri, 11 Aug 2023 23:11:43 -0700 Subject: [PATCH] Use more block tags --- src/sage/misc/cachefunc.pyx | 151 ++++++++++--------- src/sage/misc/converting_dict.py | 9 +- src/sage/misc/decorators.py | 9 +- src/sage/misc/dev_tools.py | 9 +- src/sage/misc/element_with_label.py | 12 +- src/sage/misc/function_mangling.pyx | 3 +- src/sage/misc/functional.py | 61 ++++---- src/sage/misc/gperftools.py | 2 +- src/sage/misc/inline_fortran.py | 11 +- src/sage/misc/latex.py | 31 ++-- src/sage/misc/lazy_import.pyx | 9 +- src/sage/misc/lazy_string.pyx | 9 +- src/sage/misc/package.py | 6 +- src/sage/misc/persist.pyx | 15 +- src/sage/misc/reset.pyx | 11 +- src/sage/misc/sage_eval.py | 27 ++-- src/sage/misc/sagedoc.py | 9 +- src/sage/misc/sageinspect.py | 17 ++- src/sage/misc/trace.py | 2 +- src/sage/misc/weak_dict.pyx | 4 +- src/sage/repl/interpreter.py | 14 +- src/sage/repl/ipython_extension.py | 9 +- src/sage/repl/rich_output/display_manager.py | 11 +- src/sage/typeset/ascii_art.py | 10 +- 24 files changed, 245 insertions(+), 206 deletions(-) diff --git a/src/sage/misc/cachefunc.pyx b/src/sage/misc/cachefunc.pyx index 1b44e606933..a307c33e32b 100644 --- a/src/sage/misc/cachefunc.pyx +++ b/src/sage/misc/cachefunc.pyx @@ -67,6 +67,7 @@ example. By :trac:`12951`, cached methods of extension classes can be defined by simply using the decorator. However, an indirect approach is still needed for cpdef methods:: + sage: # needs sage.misc.cython sage: cython_code = ['cpdef test_meth(self,x):', ....: ' "some doc for a wrapped cython method"', ....: ' return -x', @@ -78,21 +79,21 @@ approach is still needed for cpdef methods:: ....: ' "Some doc for direct method"', ....: ' return 2*x', ....: ' wrapped_method = cached_method(test_meth,name="wrapped_method")'] - sage: cython(os.linesep.join(cython_code)) # needs sage.misc.cython - sage: O = MyClass() # needs sage.misc.cython - sage: O.direct_method # needs sage.misc.cython + sage: cython(os.linesep.join(cython_code)) + sage: O = MyClass() + sage: O.direct_method Cached version of - sage: O.wrapped_method # needs sage.misc.cython + sage: O.wrapped_method Cached version of - sage: O.wrapped_method.__name__ # needs sage.misc.cython + sage: O.wrapped_method.__name__ 'wrapped_method' - sage: O.wrapped_method(5) # needs sage.misc.cython + sage: O.wrapped_method(5) -5 - sage: O.wrapped_method(5) is O.wrapped_method(5) # needs sage.misc.cython + sage: O.wrapped_method(5) is O.wrapped_method(5) True - sage: O.direct_method(5) # needs sage.misc.cython + sage: O.direct_method(5) 10 - sage: O.direct_method(5) is O.direct_method(5) # needs sage.misc.cython + sage: O.direct_method(5) is O.direct_method(5) True In some cases, one would only want to keep the result in cache as long @@ -148,6 +149,7 @@ cached methods. We remark, however, that cached methods are hardly by used. :: + sage: # needs sage.misc.cython sage: cython_code = ["from sage.structure.element cimport Element, ElementWithCachedMethod", "from cpython.object cimport PyObject_RichCompare", ....: "cdef class MyBrokenElement(Element):", ....: " cdef public object x", @@ -182,10 +184,10 @@ hardly by used. ....: "from sage.structure.parent cimport Parent", ....: "cdef class MyParent(Parent):", ....: " Element = MyElement"] - sage: cython('\n'.join(cython_code)) # needs sage.misc.cython - sage: P = MyParent(category=C) # needs sage.misc.cython - sage: ebroken = MyBrokenElement(P, 5) # needs sage.misc.cython - sage: e = MyElement(P, 5) # needs sage.misc.cython + sage: cython('\n'.join(cython_code)) + sage: P = MyParent(category=C) + sage: ebroken = MyBrokenElement(P, 5) + sage: e = MyElement(P, 5) The cached methods inherited by the parent works:: @@ -302,6 +304,7 @@ methods of extension classes, as long as they either support attribute assignmen or have a public attribute of type ```` called ``__cached_methods``. The latter is easy:: + sage: # needs sage.misc.cython sage: cython_code = [ ....: "from sage.misc.cachefunc import cached_method", ....: "cdef class MyClass:", @@ -309,11 +312,11 @@ latter is easy:: ....: " @cached_method", ....: " def f(self, a,b):", ....: " return a*b"] - sage: cython(os.linesep.join(cython_code)) # needs sage.misc.cython - sage: P = MyClass() # needs sage.misc.cython - sage: P.f(2, 3) # needs sage.misc.cython + sage: cython(os.linesep.join(cython_code)) + sage: P = MyClass() + sage: P.f(2, 3) 6 - sage: P.f(2, 3) is P.f(2, 3) # needs sage.misc.cython + sage: P.f(2, 3) is P.f(2, 3) True Providing attribute access is a bit more tricky, since it is needed that @@ -321,6 +324,7 @@ an attribute inherited by the instance from its class can be overridden on the instance. That is why providing a ``__getattr__`` would not be enough in the following example:: + sage: # needs sage.misc.cython sage: cython_code = [ ....: "from sage.misc.cachefunc import cached_method", ....: "cdef class MyOtherClass:", @@ -338,11 +342,11 @@ enough in the following example:: ....: " @cached_method", ....: " def f(self, a,b):", ....: " return a+b"] - sage: cython(os.linesep.join(cython_code)) # needs sage.misc.cython - sage: Q = MyOtherClass() # needs sage.misc.cython - sage: Q.f(2, 3) # needs sage.misc.cython + sage: cython(os.linesep.join(cython_code)) + sage: Q = MyOtherClass() + sage: Q.f(2, 3) 5 - sage: Q.f(2, 3) is Q.f(2, 3) # needs sage.misc.cython + sage: Q.f(2, 3) is Q.f(2, 3) True Note that supporting attribute access is somehow faster than the @@ -378,21 +382,21 @@ caching in many places. However, such objects should still be usable in caches. This can be achieved by defining an appropriate method ``_cache_key``:: - sage: hash(b) # needs sage.rings.padics + sage: # needs sage.rings.padics + sage: hash(b) Traceback (most recent call last): ... TypeError: unhashable type: 'sage.rings.padics.qadic_flint_CR.qAdicCappedRelativeElement' sage: from sage.misc.cachefunc import cached_method sage: @cached_method ....: def f(x): return x == a - sage: f(b) # needs sage.rings.padics + sage: f(b) True sage: f(c) # if b and c were hashable, this would return True False - - sage: b._cache_key() # needs sage.rings.padics + sage: b._cache_key() (..., ((0, 1),), 0, 1) - sage: c._cache_key() # needs sage.rings.padics + sage: c._cache_key() (..., ((0, 1), (1,)), 0, 20) .. NOTE:: @@ -958,35 +962,36 @@ cdef class CachedFunction(): TESTS:: - sage: # needs sage.combinat + sage: # needs sage.combinat sage.libs.flint sage: g = CachedFunction(number_of_partitions) - sage: a = g(5) # needs sage.libs.flint - sage: g.cache # needs sage.libs.flint + sage: a = g(5) + sage: g.cache {((5, 'default'), ()): 7} - sage: a = g(10^5) # indirect doctest # needs sage.libs.flint - sage: a == number_of_partitions(10^5) # needs sage.libs.flint + sage: a = g(10^5) # indirect doctest + sage: a == number_of_partitions(10^5) True - sage: a is g(10^5) # needs sage.libs.flint + sage: a is g(10^5) True - sage: a is number_of_partitions(10^5) # needs sage.libs.flint + sage: a is number_of_partitions(10^5) True Check that :trac:`16316` has been fixed, i.e., caching works for immutable unhashable objects which define :meth:`sage.structure.sage_object.SageObject._cache_key`:: + sage: # needs sage.rings.padics sage: @cached_function ....: def f(x): return x+x - sage: K. = Qq(4) # needs sage.rings.padics - sage: x = K(1,1); x # needs sage.rings.padics + sage: K. = Qq(4) + sage: x = K(1,1); x 1 + O(2) - sage: y = K(1,2); y # needs sage.rings.padics + sage: y = K(1,2); y 1 + O(2^2) - sage: x == y # needs sage.rings.padics + sage: x == y True - sage: f(x) is f(x) # needs sage.rings.padics + sage: f(x) is f(x) True - sage: f(y) is not f(x) # needs sage.rings.padics + sage: f(y) is not f(x) True """ @@ -1056,16 +1061,17 @@ cdef class CachedFunction(): immutable unhashable objects which define :meth:`sage.structure.sage_object.SageObject._cache_key`:: + sage: # needs sage.rings.padics sage: @cached_function ....: def f(x): return x - sage: K. = Qq(4) # needs sage.rings.padics - sage: x = K(1,1); x # needs sage.rings.padics + sage: K. = Qq(4) + sage: x = K(1,1); x 1 + O(2) - sage: f.is_in_cache(x) # needs sage.rings.padics + sage: f.is_in_cache(x) False - sage: f(x) # needs sage.rings.padics + sage: f(x) 1 + O(2) - sage: f.is_in_cache(x) # needs sage.rings.padics + sage: f.is_in_cache(x) True """ @@ -1101,13 +1107,14 @@ cdef class CachedFunction(): immutable unhashable objects which define :meth:`sage.structure.sage_object.SageObject._cache_key`:: + sage: # needs sage.rings.padics sage: @cached_function ....: def f(x): return x - sage: K. = Qq(4) # needs sage.rings.padics - sage: x = K(1,1); x # needs sage.rings.padics + sage: K. = Qq(4) + sage: x = K(1,1); x 1 + O(2) - sage: f.set_cache(x, x) # needs sage.rings.padics - sage: f.is_in_cache(x) # needs sage.rings.padics + sage: f.set_cache(x, x) + sage: f.is_in_cache(x) True DEVELOPER NOTE: @@ -1327,20 +1334,21 @@ cdef class WeakCachedFunction(CachedFunction): immutable unhashable objects which define :meth:`sage.structure.sage_object.SageObject._cache_key`:: + sage: # needs sage.rings.padics sage: from sage.misc.cachefunc import weak_cached_function sage: @weak_cached_function ....: def f(x): return x+x - sage: K. = Qq(4) # needs sage.rings.padics - sage: R. = K[] # needs sage.rings.padics - sage: x = t + K(1,1); x # needs sage.rings.padics + sage: K. = Qq(4) + sage: R. = K[] + sage: x = t + K(1,1); x (1 + O(2^20))*t + 1 + O(2) - sage: y = t + K(1,2); y # needs sage.rings.padics + sage: y = t + K(1,2); y (1 + O(2^20))*t + 1 + O(2^2) - sage: x == y # needs sage.rings.padics + sage: x == y True - sage: f(x) is f(x) # needs sage.rings.padics + sage: f(x) is f(x) True - sage: f(y) is not f(x) # needs sage.rings.padics + sage: f(y) is not f(x) True Examples and tests for ``is_in_cache``:: @@ -1371,16 +1379,17 @@ cdef class WeakCachedFunction(CachedFunction): immutable unhashable objects which define :meth:`sage.structure.sage_object.SageObject._cache_key`:: + sage: # needs sage.rings.padics sage: from sage.misc.cachefunc import weak_cached_function sage: @weak_cached_function ....: def f(x): return x - sage: K. = Qq(4) # needs sage.rings.padics - sage: R. = K[] # needs sage.rings.padics - sage: f.is_in_cache(t) # needs sage.rings.padics + sage: K. = Qq(4) + sage: R. = K[] + sage: f.is_in_cache(t) False - sage: f(t) # needs sage.rings.padics + sage: f(t) (1 + O(2^20))*t - sage: f.is_in_cache(t) # needs sage.rings.padics + sage: f.is_in_cache(t) True Examples and tests for ``set_cache``:: @@ -1397,13 +1406,14 @@ cdef class WeakCachedFunction(CachedFunction): immutable unhashable objects which define :meth:`sage.structure.sage_object.SageObject._cache_key`:: + sage: # needs sage.rings.padics sage: from sage.misc.cachefunc import weak_cached_function sage: @weak_cached_function ....: def f(x): return x - sage: K. = Qq(4) # needs sage.rings.padics - sage: R. = K[] # needs sage.rings.padics - sage: f.set_cache(t,t) # needs sage.rings.padics - sage: f.is_in_cache(t) # needs sage.rings.padics + sage: K. = Qq(4) + sage: R. = K[] + sage: f.set_cache(t,t) + sage: f.is_in_cache(t) True """ def __init__(self, f, *, classmethod=False, name=None, key=None, **kwds): @@ -1915,20 +1925,21 @@ cdef class CachedMethodCaller(CachedFunction): immutable unhashable objects which define :meth:`sage.structure.sage_object.SageObject._cache_key`:: - sage: K. = Qq(4) # needs sage.rings.padics + sage: # needs sage.rings.padics + sage: K. = Qq(4) sage: class A(): ....: @cached_method ....: def f(self, x): return x+x sage: a = A() - sage: x = K(1,1); x # needs sage.rings.padics + sage: x = K(1,1); x 1 + O(2) - sage: y = K(1,2); y # needs sage.rings.padics + sage: y = K(1,2); y 1 + O(2^2) - sage: x == y # needs sage.rings.padics + sage: x == y True - sage: a.f(x) is a.f(x) # needs sage.rings.padics + sage: a.f(x) is a.f(x) True - sage: a.f(y) is not a.f(x) # needs sage.rings.padics + sage: a.f(y) is not a.f(x) True """ diff --git a/src/sage/misc/converting_dict.py b/src/sage/misc/converting_dict.py index 47ed94446d1..39f66c9295a 100644 --- a/src/sage/misc/converting_dict.py +++ b/src/sage/misc/converting_dict.py @@ -26,13 +26,14 @@ This is used e.g. in the result of a variety, to allow access to the result no matter how a generator is identified:: + sage: # needs sage.rings.number_field sage: K. = QQ[] sage: I = ideal([x^2 + 2*y - 5, x + y + 3]) - sage: V = sorted(I.variety(AA), key=str) # needs sage.rings.number_field - sage: v = V[0] # needs sage.rings.number_field - sage: v['x'], v['y'] # needs sage.rings.number_field + sage: V = sorted(I.variety(AA), key=str) + sage: v = V[0] + sage: v['x'], v['y'] (-2.464101615137755?, -0.535898384862246?) - sage: list(v)[0].parent() # needs sage.rings.number_field + sage: list(v)[0].parent() Multivariate Polynomial Ring in x, y over Algebraic Real Field """ # **************************************************************************** diff --git a/src/sage/misc/decorators.py b/src/sage/misc/decorators.py index d8f2539d4cb..a13f1523c74 100644 --- a/src/sage/misc/decorators.py +++ b/src/sage/misc/decorators.py @@ -199,14 +199,15 @@ class infix_operator(): An infix element-wise addition operator:: + sage: # needs sage.modules sage: @infix_operator('add') ....: def eadd(a, b): ....: return a.parent([i + j for i, j in zip(a, b)]) - sage: u = vector([1, 2, 3]) # needs sage.modules - sage: v = vector([5, 4, 3]) # needs sage.modules - sage: u +eadd+ v # needs sage.modules + sage: u = vector([1, 2, 3]) + sage: v = vector([5, 4, 3]) + sage: u +eadd+ v (6, 6, 6) - sage: 2*u +eadd+ v # needs sage.modules + sage: 2*u +eadd+ v (7, 8, 9) A hack to simulate a postfix operator:: diff --git a/src/sage/misc/dev_tools.py b/src/sage/misc/dev_tools.py index c4df0596fba..a964ebfc524 100644 --- a/src/sage/misc/dev_tools.py +++ b/src/sage/misc/dev_tools.py @@ -222,12 +222,13 @@ def find_objects_from_name(name, module_name=None): sage: dt.find_objects_from_name('FareySymbol') # needs sage.modular [] - sage: import sympy # needs sympy - sage: dt.find_objects_from_name('RR') # needs sympy + sage: # needs sympy + sage: import sympy + sage: dt.find_objects_from_name('RR') [Real Field with 53 bits of precision, RR] - sage: dt.find_objects_from_name('RR', 'sage') # needs sympy + sage: dt.find_objects_from_name('RR', 'sage') [Real Field with 53 bits of precision] - sage: dt.find_objects_from_name('RR', 'sympy') # needs sympy + sage: dt.find_objects_from_name('RR', 'sympy') [RR] Examples that do not belong to the global namespace but in a loaded module:: diff --git a/src/sage/misc/element_with_label.py b/src/sage/misc/element_with_label.py index 438f697d907..831b012e31a 100644 --- a/src/sage/misc/element_with_label.py +++ b/src/sage/misc/element_with_label.py @@ -33,14 +33,14 @@ class ElementWithLabel(): sage: print(P.plot(element_labels=labs)) # needs sage.plot Graphics object consisting of 6 graphics primitives - sage: # needs sage.combinat sage.modules + sage: # needs sage.combinat sage.graphs sage.modules sage: from sage.misc.element_with_label import ElementWithLabel sage: W = WeylGroup("A1") - sage: P = W.bruhat_poset(facade=True) # needs sage.graphs - sage: D = W.domain() # needs sage.graphs - sage: v = D.rho() - D.fundamental_weight(1) # needs sage.graphs - sage: nP = P.relabel(lambda w: ElementWithLabel(w, w.action(v))) # needs sage.graphs - sage: list(nP) # needs sage.graphs + sage: P = W.bruhat_poset(facade=True) + sage: D = W.domain() + sage: v = D.rho() - D.fundamental_weight(1) + sage: nP = P.relabel(lambda w: ElementWithLabel(w, w.action(v))) + sage: list(nP) [(0, 0), (0, 0)] """ def __init__(self, element, label): diff --git a/src/sage/misc/function_mangling.pyx b/src/sage/misc/function_mangling.pyx index 3c6d5cc7190..1392fc4f2fd 100644 --- a/src/sage/misc/function_mangling.pyx +++ b/src/sage/misc/function_mangling.pyx @@ -157,8 +157,7 @@ cdef class ArgumentFixer: EXAMPLES:: sage: from sage.misc.function_mangling import ArgumentFixer - sage: g = ArgumentFixer(number_of_partitions) # needs sage.combinat - sage: g # needs sage.combinat + sage: g = ArgumentFixer(number_of_partitions); g # needs sage.combinat Argument Fixer of """ return "Argument Fixer of %s"%self.f diff --git a/src/sage/misc/functional.py b/src/sage/misc/functional.py index 3eed5fab108..72a40915b4d 100644 --- a/src/sage/misc/functional.py +++ b/src/sage/misc/functional.py @@ -139,12 +139,12 @@ def characteristic_polynomial(x, var='x'): EXAMPLES:: - sage: # needs sage.modules + sage: # needs sage.libs.pari sage.modules sage: M = MatrixSpace(QQ, 3, 3) sage: A = M([1,2,3,4,5,6,7,8,9]) - sage: charpoly(A) # needs sage.libs.pari + sage: charpoly(A) x^3 - 15*x^2 - 18*x - sage: charpoly(A, 't') # needs sage.libs.pari + sage: charpoly(A, 't') t^3 - 15*t^2 - 18*t sage: k. = GF(7^10); k # needs sage.rings.finite_rings @@ -728,11 +728,11 @@ def integral(x, *args, **kwds): sage: sage.calculus.calculus.maxima('domain: real') real sage: f = exp(-x) * sinh(sqrt(x)) - sage: t = integrate(f, x, 0, Infinity); t # long time + sage: t = integrate(f, x, 0, Infinity); t # long time 1/4*sqrt(pi)*(erf(1) - 1)*e^(1/4) - 1/4*(sqrt(pi)*(erf(1) - 1) - sqrt(pi) + 2*e^(-1) - 2)*e^(1/4) + 1/4*sqrt(pi)*e^(1/4) - 1/2*e^(1/4) + 1/2*e^(-3/4) - sage: t.canonicalize_radical() # long time + sage: t.canonicalize_radical() # long time 1/2*sqrt(pi)*e^(1/4) sage: sage.calculus.calculus.maxima('domain: complex') complex @@ -770,10 +770,11 @@ def integral(x, *args, **kwds): sage: integrate(sin(x)*tan(x)/(1-cos(x)), x, a, b, algorithm='sympy') -integrate(sin(x)*tan(x)/(cos(x) - 1), x, a, b) - sage: import sympy # needs sympy - sage: x, y, z = sympy.symbols('x y z') # needs sympy - sage: f = sympy.Function('f') # needs sympy - sage: SR(sympy.Integral(f(x,y,z), x, y, z)) # needs sympy sage.symbolic + sage: # needs sympy + sage: import sympy + sage: x, y, z = sympy.symbols('x y z') + sage: f = sympy.Function('f') + sage: SR(sympy.Integral(f(x,y,z), x, y, z)) # needs sage.symbolic integrate(integrate(integrate(f(x, y, z), x), y), z) Ensure that the following integral containing a signum term from @@ -1225,15 +1226,15 @@ def minimal_polynomial(x, var='x'): EXAMPLES:: - sage: # needs sage.modules + sage: # needs sage.libs.pari sage.modules sage: a = matrix(ZZ, 2, [1..4]) - sage: minpoly(a) # needs sage.libs.pari + sage: minpoly(a) x^2 - 5*x - 2 - sage: minpoly(a, 't') # needs sage.libs.pari + sage: minpoly(a, 't') t^2 - 5*t - 2 - sage: minimal_polynomial(a) # needs sage.libs.pari + sage: minimal_polynomial(a) x^2 - 5*x - 2 - sage: minimal_polynomial(a, 'theta') # needs sage.libs.pari + sage: minimal_polynomial(a, 'theta') theta^2 - 5*theta - 2 """ try: @@ -1248,7 +1249,7 @@ def minimal_polynomial(x, var='x'): def multiplicative_order(x): r""" Return the multiplicative order of ``x``, if ``x`` is a unit, or - raise ``ArithmeticError`` otherwise. + raise :class:`ArithmeticError` otherwise. EXAMPLES:: @@ -1725,18 +1726,20 @@ def round(x, ndigits=0): EXAMPLES:: - sage: round(sqrt(2), 2) # needs sage.symbolic + sage: # needs sage.symbolic + sage: round(sqrt(2), 2) 1.41 - sage: q = round(sqrt(2), 5); q # needs sage.symbolic + sage: q = round(sqrt(2), 5); q 1.41421 - sage: type(q) # needs sage.symbolic + sage: type(q) - sage: q = round(sqrt(2)); q # needs sage.symbolic + sage: q = round(sqrt(2)); q 1 - sage: type(q) # needs sage.symbolic + sage: type(q) - sage: round(pi) # needs sage.symbolic + sage: round(pi) 3 + sage: b = 5.4999999999999999 sage: round(b) 5 @@ -1954,17 +1957,19 @@ def sqrt(x, *args, **kwds): EXAMPLES:: - sage: sqrt(-1) # needs sage.symbolic - I - sage: sqrt(2) # needs sage.symbolic - sqrt(2) - sage: sqrt(2)^2 # needs sage.symbolic - 2 sage: sqrt(4) 2 sage: sqrt(4, all=True) [2, -2] - sage: sqrt(x^2) # needs sage.symbolic + + sage: # needs sage.symbolic + sage: sqrt(-1) + I + sage: sqrt(2) + sqrt(2) + sage: sqrt(2)^2 + 2 + sage: sqrt(x^2) sqrt(x^2) For a non-symbolic square root, there are a few options. diff --git a/src/sage/misc/gperftools.py b/src/sage/misc/gperftools.py index fc66a770337..b928ef18e08 100644 --- a/src/sage/misc/gperftools.py +++ b/src/sage/misc/gperftools.py @@ -1,4 +1,4 @@ -# sage.doctest: optional - sage.symbolic +# sage.doctest: needs sage.symbolic """ C Function Profiler Using Google Perftools diff --git a/src/sage/misc/inline_fortran.py b/src/sage/misc/inline_fortran.py index 90418b13f30..0ace070510b 100644 --- a/src/sage/misc/inline_fortran.py +++ b/src/sage/misc/inline_fortran.py @@ -91,6 +91,7 @@ def eval(self, x, globals=None, locals=None): EXAMPLES:: + sage: # needs numpy sage: code = ''' ....: C FILE: FIB1.F ....: SUBROUTINE FIB(A,N) @@ -111,11 +112,11 @@ def eval(self, x, globals=None, locals=None): ....: END ....: C END FILE FIB1.F ....: ''' - sage: fortran(code, globals()) # needs numpy - sage: import numpy # needs numpy - sage: a = numpy.array(range(10), dtype=float) # needs numpy - sage: fib(a, 10) # needs numpy - sage: a # needs numpy + sage: fortran(code, globals()) + sage: import numpy + sage: a = numpy.array(range(10), dtype=float) + sage: fib(a, 10) + sage: a array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34.]) TESTS:: diff --git a/src/sage/misc/latex.py b/src/sage/misc/latex.py index b671904e68f..25b1ff557be 100644 --- a/src/sage/misc/latex.py +++ b/src/sage/misc/latex.py @@ -967,7 +967,7 @@ class Latex(LatexCall): sage: latex(x^20 + 1) # needs sage.symbolic x^{20} + 1 - sage: latex(FiniteField(25,'a')) # needs sage.libs.pari + sage: latex(FiniteField(25,'a')) # needs sage.rings.finite_rings \Bold{F}_{5^{2}} sage: latex("hello") \text{\texttt{hello}} @@ -1216,18 +1216,19 @@ def matrix_delimiters(self, left=None, right=None): EXAMPLES:: - sage: a = matrix(1, 1, [17]) # needs sage.modules - sage: latex(a) # needs sage.modules + sage: # needs sage.modules + sage: a = matrix(1, 1, [17]) + sage: latex(a) \left(\begin{array}{r} 17 \end{array}\right) sage: latex.matrix_delimiters("[", "]") - sage: latex(a) # needs sage.modules + sage: latex(a) \left[\begin{array}{r} 17 \end{array}\right] sage: latex.matrix_delimiters(left="\\{") - sage: latex(a) # needs sage.modules + sage: latex(a) \left\{\begin{array}{r} 17 \end{array}\right] @@ -1277,14 +1278,15 @@ def vector_delimiters(self, left=None, right=None): EXAMPLES:: - sage: a = vector(QQ, [1,2,3]) # needs sage.modules - sage: latex(a) # needs sage.modules + sage: # needs sage.modules + sage: a = vector(QQ, [1,2,3]) + sage: latex(a) \left(1,\,2,\,3\right) sage: latex.vector_delimiters("[", "]") - sage: latex(a) # needs sage.modules + sage: latex(a) \left[1,\,2,\,3\right] sage: latex.vector_delimiters(right="\\}") - sage: latex(a) # needs sage.modules + sage: latex(a) \left[1,\,2,\,3\right\} sage: latex.vector_delimiters() ['[', '\\}'] @@ -1322,18 +1324,19 @@ def matrix_column_alignment(self, align=None): EXAMPLES:: - sage: a = matrix(1, 1, [42]) # needs sage.modules - sage: latex(a) # needs sage.modules + sage: # needs sage.modules + sage: a = matrix(1, 1, [42]) + sage: latex(a) \left(\begin{array}{r} 42 \end{array}\right) sage: latex.matrix_column_alignment('c') - sage: latex(a) # needs sage.modules + sage: latex(a) \left(\begin{array}{c} 42 \end{array}\right) sage: latex.matrix_column_alignment('l') - sage: latex(a) # needs sage.modules + sage: latex(a) \left(\begin{array}{l} 42 \end{array}\right) @@ -1971,7 +1974,7 @@ def png(x, filename, density=150, debug=False, sage: from sage.misc.latex import png sage: import tempfile - sage: with tempfile.NamedTemporaryFile(suffix=".png") as f: # optional - imagemagick latex, needs sage.plot + sage: with tempfile.NamedTemporaryFile(suffix=".png") as f: # random # optional - imagemagick latex, needs sage.plot ....: png(ZZ[x], f.name) """ if not pdflatex: diff --git a/src/sage/misc/lazy_import.pyx b/src/sage/misc/lazy_import.pyx index 3395837c01e..c33a1e74efd 100644 --- a/src/sage/misc/lazy_import.pyx +++ b/src/sage/misc/lazy_import.pyx @@ -667,11 +667,12 @@ cdef class LazyImport(): """ TESTS:: - sage: from sympy import Matrix # needs sympy + sage: # needs sympy + sage: from sympy import Matrix sage: import sage.all__sagemath_objects - sage: sage.all__sagemath_objects.foo = Matrix([[1,1], [0,1]]) # needs sympy - sage: lazy_import('sage.all__sagemath_objects', 'foo') # needs sympy - sage: foo.__matmul__(foo) # needs sympy + sage: sage.all__sagemath_objects.foo = Matrix([[1,1], [0,1]]) + sage: lazy_import('sage.all__sagemath_objects', 'foo') + sage: foo.__matmul__(foo) Matrix([ [1, 2], [0, 1]]) diff --git a/src/sage/misc/lazy_string.pyx b/src/sage/misc/lazy_string.pyx index b02f0f2c9c0..dd92fb38142 100644 --- a/src/sage/misc/lazy_string.pyx +++ b/src/sage/misc/lazy_string.pyx @@ -519,25 +519,24 @@ cdef class _LazyString(): EXAMPLES:: - sage: # needs sage.libs.pari + sage: # needs sage.rings.finite_rings sage: from sage.misc.lazy_string import lazy_string sage: def f(op, A, B): ....: return "unsupported operand parent(s) for %s: '%s' and '%s'" % (op, A, B) sage: R = GF(5) sage: S = GF(3) - sage: D = lazy_string(f, '+', R, S) - sage: D + sage: D = lazy_string(f, '+', R, S); D l"unsupported operand parent(s) for +: 'Finite Field of size 5' and 'Finite Field of size 3'" sage: D.update_lazy_string(('+', S, R), {}) Apparently, the lazy string got changed in-place:: - sage: D # needs sage.libs.pari + sage: D # needs sage.rings.finite_rings l"unsupported operand parent(s) for +: 'Finite Field of size 3' and 'Finite Field of size 5'" TESTS:: - sage: D.update_lazy_string(None, None) # needs sage.libs.pari + sage: D.update_lazy_string(None, None) # needs sage.rings.finite_rings Traceback (most recent call last): ... TypeError: Expected tuple, got NoneType diff --git a/src/sage/misc/package.py b/src/sage/misc/package.py index 851fdc4f5ae..98e263d7b9c 100644 --- a/src/sage/misc/package.py +++ b/src/sage/misc/package.py @@ -297,7 +297,7 @@ def list_packages(*pkg_types: str, pkg_sources: List[str] = ['normal', 'pip', 's sage: # optional - sage_spkg sage: from sage.misc.package import list_packages sage: L = list_packages('standard') - sage: sorted(L.keys()) + sage: sorted(L.keys()) # random ['alabaster', 'arb', 'babel', @@ -428,9 +428,10 @@ def installed_packages(exclude_pip=True): main Sage venv; it could be a user-created venv or a venv created by tox.):: sage: # optional - sage_spkg + sage: from sage.misc.package import installed_packages sage: sorted(installed_packages().keys()) [...'conway_polynomials', ...] - sage: installed_packages()['conway_polynomials'] + sage: installed_packages()['conway_polynomials'] # random '0.5' .. SEEALSO:: @@ -538,6 +539,7 @@ def package_versions(package_type, local=False): EXAMPLES:: sage: # optional - sage_spkg + sage: from sage.misc.package import package_versions sage: std = package_versions('standard', local=True) sage: 'gap' in std True diff --git a/src/sage/misc/persist.pyx b/src/sage/misc/persist.pyx index 39e03876f51..780fdd40847 100644 --- a/src/sage/misc/persist.pyx +++ b/src/sage/misc/persist.pyx @@ -246,17 +246,20 @@ def save(obj, filename, compress=True, **kwargs): sage: load(objfile_short) # needs sage.modules [ 1 2] [ 3 -5/2] - sage: E = EllipticCurve([-1,0]) # needs sage.plot sage.schemes - sage: P = plot(E) # needs sage.plot sage.schemes - sage: save(P, objfile_short) # saves the plot to "test.sobj" # needs sage.plot sage.schemes - sage: save(P, filename=os.path.join(d.name, "sage.png"), xmin=-2) # needs sage.plot sage.schemes - sage: save(P, os.path.join(d.name, "filename.with.some.wrong.ext")) # needs sage.plot sage.schemes + + sage: # needs sage.plot sage.schemes + sage: E = EllipticCurve([-1,0]) + sage: P = plot(E) + sage: save(P, objfile_short) # saves the plot to "test.sobj" + sage: save(P, filename=os.path.join(d.name, "sage.png"), xmin=-2) + sage: save(P, os.path.join(d.name, "filename.with.some.wrong.ext")) Traceback (most recent call last): ... ValueError: allowed file extensions for images are '.eps', '.pdf', '.pgf', '.png', '.ps', '.sobj', '.svg'! - sage: print(load(objfile)) # needs sage.plot sage.schemes + sage: print(load(objfile)) Graphics object consisting of 2 graphics primitives + sage: save("A python string", os.path.join(d.name, 'test')) sage: load(objfile) 'A python string' diff --git a/src/sage/misc/reset.pyx b/src/sage/misc/reset.pyx index 04306e567e8..5d712e5da7a 100644 --- a/src/sage/misc/reset.pyx +++ b/src/sage/misc/reset.pyx @@ -57,15 +57,16 @@ def reset(vars=None, attached=False): Confirm that assumptions don't survive a reset (:trac:`10855`):: - sage: assume(x > 3) # needs sage.symbolic - sage: assumptions() # needs sage.symbolic + sage: # needs sage.symbolic + sage: assume(x > 3) + sage: assumptions() [x > 3] - sage: bool(x > 3) # needs sage.symbolic + sage: bool(x > 3) True sage: reset() - sage: assumptions() # needs sage.symbolic + sage: assumptions() [] - sage: bool(x > 3) # needs sage.symbolic + sage: bool(x > 3) False """ diff --git a/src/sage/misc/sage_eval.py b/src/sage/misc/sage_eval.py index e27facaefa9..ec6d94543f9 100644 --- a/src/sage/misc/sage_eval.py +++ b/src/sage/misc/sage_eval.py @@ -131,14 +131,15 @@ def sage_eval(source, locals=None, cmds='', preparse=True): :: + sage: # needs sage.libs.gap sage: R. = PolynomialRing(RationalField()) - sage: gap.eval('R:=PolynomialRing(Rationals,["x"]);') # needs sage.libs.gap + sage: gap.eval('R:=PolynomialRing(Rationals,["x"]);') 'Rationals[x]' - sage: ff = gap.eval('x:=IndeterminatesOfPolynomialRing(R);; f:=x^2+1;'); ff # needs sage.libs.gap + sage: ff = gap.eval('x:=IndeterminatesOfPolynomialRing(R);; f:=x^2+1;'); ff 'x^2+1' - sage: sage_eval(ff, locals={'x':x}) # needs sage.libs.gap + sage: sage_eval(ff, locals={'x':x}) x^2 + 1 - sage: eval(ff) # needs sage.libs.gap + sage: eval(ff) Traceback (most recent call last): ... RuntimeError: Use ** for exponentiation, not '^', which means xor @@ -214,6 +215,7 @@ def sageobj(x, vars=None): sage: type(sageobj(gp('34/56'))) # needs sage.libs.pari + sage: n = 5/2 sage: sageobj(n) is n True @@ -224,17 +226,20 @@ def sageobj(x, vars=None): This illustrates interfaces:: - sage: f = gp('2/3') # needs sage.libs.pari - sage: type(f) # needs sage.libs.pari + sage: # needs sage.libs.pari + sage: f = gp('2/3') + sage: type(f) - sage: f._sage_() # needs sage.libs.pari + sage: f._sage_() 2/3 - sage: type(f._sage_()) # needs sage.libs.pari + sage: type(f._sage_()) - sage: a = gap(939393/2433) # needs sage.libs.gap - sage: a._sage_() # needs sage.libs.gap + + sage: # needs sage.libs.gap + sage: a = gap(939393/2433) + sage: a._sage_() 313131/811 - sage: type(a._sage_()) # needs sage.libs.gap + sage: type(a._sage_()) """ try: diff --git a/src/sage/misc/sagedoc.py b/src/sage/misc/sagedoc.py index 74189b415f5..6271b74d7c1 100644 --- a/src/sage/misc/sagedoc.py +++ b/src/sage/misc/sagedoc.py @@ -1231,13 +1231,14 @@ def search_doc(string, extra1='', extra2='', extra3='', extra4='', counting the length of ``search_doc('tree', interact=False).splitlines()`` gives the number of matches. :: - sage: N = len(search_doc('tree', interact=False).splitlines()) # long time, needs sagemath_doc_html - sage: L = search_doc('tree', whole_word=True, interact=False).splitlines() # long time, needs sagemath_doc_html - sage: len(L) < N # long time # needs sagemath_doc_html + sage: # long time, needs sagemath_doc_html + sage: N = len(search_doc('tree', interact=False).splitlines()) + sage: L = search_doc('tree', whole_word=True, interact=False).splitlines() + sage: len(L) < N True sage: import re sage: tree_re = re.compile(r'(^|\W)tree(\W|$)', re.I) - sage: all(tree_re.search(l) for l in L) # long time # needs sagemath_doc_html + sage: all(tree_re.search(l) for l in L) True """ return _search_src_or_doc('doc', string, extra1=extra1, extra2=extra2, diff --git a/src/sage/misc/sageinspect.py b/src/sage/misc/sageinspect.py index e1661d717e0..34677d69421 100644 --- a/src/sage/misc/sageinspect.py +++ b/src/sage/misc/sageinspect.py @@ -1570,23 +1570,24 @@ def sage_getargspec(obj): method. We saw an easy example above, namely ``I.groebner_basis``. Here is a more difficult one:: + sage: # needs sage.misc.cython sage: cython_code = [ ....: 'cdef class MyClass:', ....: ' def _sage_src_(self):', ....: ' return "def foo(x, a=\\\')\\\"\\\', b={(2+1):\\\'bar\\\', not 1:3, 3<<4:5}): return\\n"', ....: ' def __call__(self, m,n): return "something"'] - sage: cython('\n'.join(cython_code)) # needs sage.misc.cython - sage: O = MyClass() # needs sage.misc.cython - sage: print(sage.misc.sageinspect.sage_getsource(O)) # needs sage.misc.cython + sage: cython('\n'.join(cython_code)) + sage: O = MyClass() + sage: print(sage.misc.sageinspect.sage_getsource(O)) def foo(x, a=')"', b={(2+1):'bar', not 1:3, 3<<4:5}): return - sage: spec = sage.misc.sageinspect.sage_getargspec(O) # needs sage.misc.cython - sage: spec.args, spec.varargs, spec.varkw # needs sage.misc.cython + sage: spec = sage.misc.sageinspect.sage_getargspec(O) + sage: spec.args, spec.varargs, spec.varkw (['x', 'a', 'b'], None, None) - sage: spec.defaults[0] # needs sage.misc.cython + sage: spec.defaults[0] ')"' - sage: sorted(spec.defaults[1].items(), key=lambda x: str(x)) # needs sage.misc.cython + sage: sorted(spec.defaults[1].items(), key=lambda x: str(x)) [(3, 'bar'), (48, 5), (False, 3)] - sage: sage.misc.sageinspect.sage_getargspec(O.__call__) # needs sage.misc.cython + sage: sage.misc.sageinspect.sage_getargspec(O.__call__) FullArgSpec(args=['self', 'm', 'n'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={}) diff --git a/src/sage/misc/trace.py b/src/sage/misc/trace.py index c29af507e18..6807cfb1940 100644 --- a/src/sage/misc/trace.py +++ b/src/sage/misc/trace.py @@ -57,7 +57,7 @@ def trace(code, preparse=True): sage: # needs pexpect sage.all sage: import pexpect sage: s = pexpect.spawn('sage') - sage: _ = s.sendline("trace('print(factor(10))'); print(3+97)") + sage: _ = s.sendline("from sage.misc.trace import trace; trace('print(factor(10))'); print(3+97)") sage: _ = s.expect('ipdb>', timeout=90) sage: _ = s.sendline("s"); _ = s.sendline("c") sage: _ = s.expect('100', timeout=90) diff --git a/src/sage/misc/weak_dict.pyx b/src/sage/misc/weak_dict.pyx index 018f1b06228..e4a2d9d9189 100644 --- a/src/sage/misc/weak_dict.pyx +++ b/src/sage/misc/weak_dict.pyx @@ -337,7 +337,7 @@ cdef class WeakValueDictionary(dict): EXAMPLES:: - sage: # needs sage.libs.pari + sage: # needs sage.rings.finite_rings sage: L = [(p, GF(p)) for p in prime_range(10)] sage: import sage.misc.weak_dict sage: D = sage.misc.weak_dict.WeakValueDictionary() @@ -584,7 +584,7 @@ cdef class WeakValueDictionary(dict): raised for unhashable objects:: sage: D = sage.misc.weak_dict.WeakValueDictionary() - sage: D.pop(matrix([])) # needs sage.all + sage: D.pop(matrix([])) # needs sage.modules Traceback (most recent call last): ... TypeError: mutable matrices are unhashable diff --git a/src/sage/repl/interpreter.py b/src/sage/repl/interpreter.py index addcc8884be..68d2f263f0f 100644 --- a/src/sage/repl/interpreter.py +++ b/src/sage/repl/interpreter.py @@ -555,9 +555,9 @@ def preparse_imports_from_sage(self, line): containing nested parentheses:: sage: # needs sage.libs.gap - sage: shell = interface_shell_embed(gap) + sage: shell = interface_shell_embed(gap) # needs sage.symbolic sage: shell.user_ns = locals() - sage: ift = InterfaceShellTransformer(shell=shell, config=shell.config, + sage: ift = InterfaceShellTransformer(shell=shell, config=shell.config, # needs sage.symbolic ....: prefilter_manager=shell.prefilter_manager) sage: line = '2 + sage((1+2)*gap(-(5-3)^2).sage()) - gap(1+(2-1))' sage: line = ift.preparse_imports_from_sage(line) @@ -594,8 +594,8 @@ def transform(self, line, continue_prompt): sage: # needs sage.symbolic sage: from sage.repl.interpreter import interface_shell_embed, InterfaceShellTransformer - sage: shell = interface_shell_embed(maxima) # optional - sage.symbolic - sage: ift = InterfaceShellTransformer(shell=shell, config=shell.config, # optional - sage.symbolic + sage: shell = interface_shell_embed(maxima) + sage: ift = InterfaceShellTransformer(shell=shell, config=shell.config, ....: prefilter_manager=shell.prefilter_manager) sage: ift.transform('2+2', False) # note: output contains triple quotation marks 'sage.repl.interpreter.logstr(r"""4""")' @@ -604,8 +604,8 @@ def transform(self, line, continue_prompt): 'sage.repl.interpreter.logstr(r"""8""")' sage: ift.temporary_objects set() - sage: shell = interface_shell_embed(gap) # optional - sage.libs.gap - sage: ift = InterfaceShellTransformer(shell=shell, config=shell.config, # optional - sage.libs.gap + sage: shell = interface_shell_embed(gap) # needs sage.libs.gap + sage: ift = InterfaceShellTransformer(shell=shell, config=shell.config, # needs sage.libs.gap ....: prefilter_manager=shell.prefilter_manager) sage: ift.transform('2+2', False) 'sage.repl.interpreter.logstr(r"""4""")' @@ -618,7 +618,7 @@ def transform(self, line, continue_prompt): sage: shell = interface_shell_embed(gap) # needs sage.libs.gap sage.symbolic sage: ift = InterfaceShellTransformer(shell=shell, config=shell.config, # needs sage.libs.gap sage.symbolic ....: prefilter_manager=shell.prefilter_manager) - sage: ift.transform(r'Print(" -\n\\\\- ");', False) + sage: ift.transform(r'Print(" -\n\\\\- ");', False) # needs sage.symbolic 'sage.repl.interpreter.logstr(r""" -\n\\\\-""")' sage: # optional - macaulay2 diff --git a/src/sage/repl/ipython_extension.py b/src/sage/repl/ipython_extension.py index 6c3a622f8cf..14c945a8517 100644 --- a/src/sage/repl/ipython_extension.py +++ b/src/sage/repl/ipython_extension.py @@ -384,6 +384,7 @@ def fortran(self, line, cell): EXAMPLES:: + sage: # needs numpy sage: from sage.repl.interpreter import get_test_shell sage: shell = get_test_shell() sage: shell.run_cell(''' @@ -409,10 +410,10 @@ def fortran(self, line, cell): ....: ''') sage: fib - sage: from numpy import array # needs numpy - sage: a = array(range(10), dtype=float) # needs numpy - sage: fib(a, 10) # needs numpy - sage: a # needs numpy + sage: from numpy import array + sage: a = array(range(10), dtype=float) + sage: fib(a, 10) + sage: a array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34.]) """ from sage.misc.inline_fortran import fortran diff --git a/src/sage/repl/rich_output/display_manager.py b/src/sage/repl/rich_output/display_manager.py index aa80fe8e3b6..f6bec0209e1 100644 --- a/src/sage/repl/rich_output/display_manager.py +++ b/src/sage/repl/rich_output/display_manager.py @@ -708,16 +708,17 @@ def graphics_from_save(self, save_function, save_kwds, EXAMPLES:: + sage: # needs sage.plot sage.symbolic sage: from sage.repl.rich_output import get_display_manager sage: dm = get_display_manager() - sage: plt = plot(sin) # needs sage.plot sage.symbolic - sage: out = dm.graphics_from_save(plt.save, dict(), '.png', # needs sage.plot sage.symbolic + sage: plt = plot(sin) + sage: out = dm.graphics_from_save(plt.save, dict(), '.png', ....: dm.types.OutputImagePng) - sage: out # needs sage.plot sage.symbolic + sage: out OutputImagePng container - sage: out.png.get().startswith(b'\x89PNG') # needs sage.plot sage.symbolic + sage: out.png.get().startswith(b'\x89PNG') True - sage: out.png.filename() # random # needs sage.plot sage.symbolic + sage: out.png.filename() # random '/home/user/.sage/temp/localhost.localdomain/23903/tmp_pu5woK.png' """ import os diff --git a/src/sage/typeset/ascii_art.py b/src/sage/typeset/ascii_art.py index 3ce428c6308..032b79322da 100644 --- a/src/sage/typeset/ascii_art.py +++ b/src/sage/typeset/ascii_art.py @@ -15,17 +15,19 @@ EXAMPLES:: - sage: n = var('n') # needs sage.symbolic - sage: integrate(n^2/x, x) # needs sage.symbolic + sage: # needs sage.symbolic + sage: n = var('n') + sage: integrate(n^2/x, x) n^2*log(x) - sage: ascii_art(integrate(n^2/x, x)) # needs sage.symbolic + sage: ascii_art(integrate(n^2/x, x)) 2 n *log(x) - sage: ascii_art(integrate(n^2/(pi*x), x)) # needs sage.symbolic + sage: ascii_art(integrate(n^2/(pi*x), x)) 2 n *log(x) --------- pi + sage: ascii_art(list(Partitions(6))) # needs sage.combinat sage.libs.flint [ * ] [ ** * ]