5050from sage .env import DOT_SAGE , HOSTNAME
5151from sage .misc .lazy_import import lazy_import
5252
53- lazy_import ("sage.combinat.subset" , ["powerset" , "subsets" , "uniq" ],
54- deprecation = 35564 )
53+ lazy_import ("sage.combinat.subset" , ["powerset" , "subsets" , "uniq" ], deprecation = 35564 )
5554
56- lazy_import ("sage.misc.timing" , ["cputime" , "GlobalCputime" , "walltime" ],
57- deprecation = 35816 )
55+ lazy_import (
56+ "sage.misc.timing" , ["cputime" , "GlobalCputime" , "walltime" ], deprecation = 35816
57+ )
5858
5959LOCAL_IDENTIFIER = '%s.%s' % (HOSTNAME , os .getpid ())
6060
@@ -168,6 +168,7 @@ def try_read(obj, splitlines=False):
168168
169169 return data
170170
171+
171172try :
172173 # Create the matplotlib config directory.
173174 os .makedirs (os .environ ["MPLCONFIGDIR" ], exist_ok = True )
@@ -347,6 +348,7 @@ def nest(f, n, x):
347348 x
348349 """
349350 from sage .rings .integer import Integer
351+
350352 n = Integer (n )
351353
352354 if n < 0 :
@@ -361,6 +363,7 @@ def nest(f, n, x):
361363# The A \ b operator
362364#################################################################
363365
366+
364367class BackslashOperator :
365368 r"""
366369 Implement Matlab-style backslash operator for solving systems::
@@ -385,6 +388,7 @@ class BackslashOperator:
385388 sage: preparse("A^3 \\ b")
386389 'A**Integer(3) * BackslashOperator() * b'
387390 """
391+
388392 def __rmul__ (self , left ):
389393 """
390394 EXAMPLES::
@@ -408,6 +412,7 @@ def __rmul__(self, left):
408412 True
409413 """
410414 from sage .misc .superseded import deprecation
415+
411416 deprecation (36394 , 'the backslash operator has been deprecated' )
412417 self .left = left
413418 return self
@@ -439,6 +444,7 @@ def __mul__(self, right):
439444 (0.0, 0.5, 1.0, 1.5, 2.0)
440445 """
441446 from sage .misc .superseded import deprecation
447+
442448 deprecation (36394 , 'the backslash operator has been deprecated' )
443449 return self .left ._backslash_ (right )
444450
@@ -527,6 +533,7 @@ def random_sublist(X, s):
527533 True
528534 """
529535 import sage .misc .prandom as random
536+
530537 return [a for a in X if random .random () <= s ]
531538
532539
@@ -599,6 +606,7 @@ def some_tuples(elements, repeat, bound, max_samples=None):
599606 """
600607 if max_samples is None :
601608 from itertools import islice , product
609+
602610 P = elements if repeat is None else product (elements , repeat = repeat )
603611 return islice (P , int (bound ))
604612 else :
@@ -608,6 +616,7 @@ def some_tuples(elements, repeat, bound, max_samples=None):
608616 N = n if repeat is None else n ** repeat
609617 if N <= max_samples :
610618 from itertools import product
619+
611620 return elements if repeat is None else product (elements , repeat = repeat )
612621 return _some_tuples_sampling (elements , repeat , max_samples , n )
613622
@@ -634,6 +643,7 @@ def _some_tuples_sampling(elements, repeat, max_samples, n):
634643 """
635644 from sage .rings .integer import Integer
636645 import sage .misc .prandom as random
646+
637647 N = n if repeat is None else n ** repeat
638648 # We sample on range(N) and create tuples manually since we don't want to create the list of all possible tuples in memory
639649 for a in random .sample (range (N ), max_samples ):
@@ -647,6 +657,7 @@ def _some_tuples_sampling(elements, repeat, max_samples, n):
647657# Misc.
648658#################################################################
649659
660+
650661def exists (S , P ):
651662 """
652663 If S contains an element x such that P(x) is ``True``, this function
@@ -832,8 +843,8 @@ def in_quote():
832843 # which is the case if the previous character isn't
833844 # a backslash, or it is but both previous characters
834845 # are backslashes.
835- if line [i - 1 : i ] != '\\ ' or line [i - 2 : i ] == '\\ \\ ' :
836- if line [i : i + 3 ] in ['"""' , "'''" ]:
846+ if line [i - 1 : i ] != '\\ ' or line [i - 2 : i ] == '\\ \\ ' :
847+ if line [i : i + 3 ] in ['"""' , "'''" ]:
837848 if not in_quote ():
838849 in_triple_quote = True
839850 elif in_triple_quote :
@@ -895,6 +906,7 @@ def get_main_globals():
895906 module.
896907 """
897908 import sys
909+
898910 depth = 0
899911 while True :
900912 G = sys ._getframe (depth ).f_globals
@@ -953,8 +965,9 @@ def inject_variable(name, value, warn=True):
953965 # also from functions in various modules.
954966 G = get_main_globals ()
955967 if name in G and warn :
956- warnings .warn ("redefining global value `%s`" % name ,
957- RuntimeWarning , stacklevel = 2 )
968+ warnings .warn (
969+ "redefining global value `%s`" % name , RuntimeWarning , stacklevel = 2
970+ )
958971 G [name ] = value
959972
960973
@@ -1009,12 +1022,14 @@ def run_once(func):
10091022 sage: foo(False)
10101023 sage: foo(True)
10111024 """
1025+
10121026 @functools .wraps (func )
10131027 def wrapper (* args , ** kwargs ):
10141028 if not wrapper .has_run :
10151029 result = func (* args , ** kwargs )
10161030 wrapper .has_run = True
10171031 return result
1032+
10181033 wrapper .has_run = False
10191034 return wrapper
10201035
0 commit comments