Skip to content

Commit a643483

Browse files
committed
move @run_once from ipython_extension to misc.misc
It made the sympy interface depend on IPython...
1 parent 853d070 commit a643483

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

src/sage/interfaces/sympy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ def _sympysage_true(self):
10161016

10171017

10181018
#------------------------------------------------------------------
1019-
from sage.repl.ipython_extension import run_once
1019+
from sage.misc.misc import run_once
10201020

10211021
@run_once
10221022
def sympy_init():

src/sage/misc/misc.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
# https://www.gnu.org/licenses/
3939
# ****************************************************************************
4040

41+
import functools
4142
import os
4243
import pdb
4344
import warnings
@@ -1132,3 +1133,37 @@ def inject_variable_test(name, value, depth):
11321133
inject_variable(name, value)
11331134
else:
11341135
inject_variable_test(name, value, depth - 1)
1136+
1137+
1138+
# from https://stackoverflow.com/questions/4103773/efficient-way-of-having-a-function-only-execute-once-in-a-loop
1139+
def run_once(func):
1140+
"""
1141+
Runs a function (successfully) only once.
1142+
1143+
The running can be reset by setting the ``has_run`` attribute to False
1144+
1145+
TESTS::
1146+
1147+
sage: from sage.repl.ipython_extension import run_once
1148+
sage: @run_once
1149+
....: def foo(work):
1150+
....: if work:
1151+
....: return 'foo worked'
1152+
....: raise RuntimeError("foo didn't work")
1153+
sage: foo(False)
1154+
Traceback (most recent call last):
1155+
...
1156+
RuntimeError: foo didn't work
1157+
sage: foo(True)
1158+
'foo worked'
1159+
sage: foo(False)
1160+
sage: foo(True)
1161+
"""
1162+
@functools.wraps(func)
1163+
def wrapper(*args, **kwargs):
1164+
if not wrapper.has_run:
1165+
result = func(*args, **kwargs)
1166+
wrapper.has_run = True
1167+
return result
1168+
wrapper.has_run = False
1169+
return wrapper

src/sage/repl/ipython_extension.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
from sage.repl.load import load_wrap
6969
from sage.env import SAGE_IMPORTALL, SAGE_STARTUP_FILE
7070
from sage.misc.lazy_import import LazyImport
71+
from sage.misc.misc import run_once
7172

7273
@magics_class
7374
class SageMagics(Magics):
@@ -583,41 +584,6 @@ def all_globals():
583584
return all_jupyter
584585

585586

586-
# from https://stackoverflow.com/questions/4103773/efficient-way-of-having-a-function-only-execute-once-in-a-loop
587-
from functools import wraps
588-
def run_once(func):
589-
"""
590-
Runs a function (successfully) only once.
591-
592-
The running can be reset by setting the ``has_run`` attribute to False
593-
594-
TESTS::
595-
596-
sage: from sage.repl.ipython_extension import run_once
597-
sage: @run_once
598-
....: def foo(work):
599-
....: if work:
600-
....: return 'foo worked'
601-
....: raise RuntimeError("foo didn't work")
602-
sage: foo(False)
603-
Traceback (most recent call last):
604-
...
605-
RuntimeError: foo didn't work
606-
sage: foo(True)
607-
'foo worked'
608-
sage: foo(False)
609-
sage: foo(True)
610-
"""
611-
@wraps(func)
612-
def wrapper(*args, **kwargs):
613-
if not wrapper.has_run:
614-
result = func(*args, **kwargs)
615-
wrapper.has_run = True
616-
return result
617-
wrapper.has_run = False
618-
return wrapper
619-
620-
621587
@run_once
622588
def load_ipython_extension(ip):
623589
"""

0 commit comments

Comments
 (0)