Skip to content

Commit e40738b

Browse files
committed
1.0.2rc4 catch IPython not installed
1 parent 5f55165 commit e40738b

File tree

5 files changed

+54
-40
lines changed

5 files changed

+54
-40
lines changed

Demonstration of equation class.ipynb

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"name": "stdout",
2929
"output_type": "stream",
3030
"text": [
31-
"This notebook is running Algebra_with_Sympy version 1.0.1.\n"
31+
"This notebook is running Algebra_with_Sympy version 1.0.2rc4.\n"
3232
]
3333
}
3434
],
@@ -4164,7 +4164,7 @@
41644164
{
41654165
"data": {
41664166
"text/plain": [
4167-
"'1.0.1'"
4167+
"'1.0.2rc4'"
41684168
]
41694169
},
41704170
"execution_count": 167,
@@ -4184,7 +4184,7 @@
41844184
{
41854185
"data": {
41864186
"text/plain": [
4187-
"'1.0.1'"
4187+
"'1.0.2rc4'"
41884188
]
41894189
},
41904190
"execution_count": 168,

ReadMe.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This tool defines relations that all high school and college students would
1616
recognize as mathematical equations.
1717
They consist of a left hand side (lhs) and a right hand side (rhs) connected by
1818
the relation operator "=". In addition, it sets some convenient defaults and
19-
provides some useful controls of output formatting that may be useful even if
19+
provides some controls of output formatting that may be useful even if
2020
you do not use the `Equation` class (see [Conveniences for
2121
SymPy](#convenience-tools-and-defaults-for-interactive-use-of-sympy)).
2222

@@ -41,9 +41,9 @@ The last cell illustrates how it is possible to substitute numbers with
4141
units into the solved equation to calculate a numerical solution with
4242
proper units. The `units(...)` operation is part this package, not Sympy.
4343

44-
In IPython environments (IPython and Jupyter) there is also a shorthand
45-
syntax for entering equations provided through the IPython preparser. An
46-
equation can be specified as `eq1 =@ a/b = c/d`.
44+
In IPython environments (IPython, Jupyter, Google Colab, etc...) there is
45+
also a shorthand syntax for entering equations provided through the IPython
46+
preparser. An equation can be specified as `eq1 =@ a/b = c/d`.
4747

4848

4949
![screenshot of short syntax](https://gutow.github.io/Algebra_with_Sympy/resources/short_syntax.png)
@@ -154,7 +154,7 @@ github](https://github.com/gutow/Algebra_with_Sympy/issues).
154154

155155
## Change Log
156156

157-
* 1.0.2 (July 4, 2024)
157+
* 1.0.2 (July 5, 2024)
158158
* Removed requirements for Jupyter and Jupyterlab as code will work in
159159
vanilla python or Google Colab.
160160
* Workaround for Google Colab's inconsistent handling of mixed Latex and

algebra_with_sympy/__init__.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
from algebra_with_sympy.algebraic_equation import *
77

88
# Set up numerics behaviors
9-
from IPython import get_ipython
10-
if get_ipython():
11-
get_ipython().input_transformers_post.append(integers_as_exact)
12-
algwsym_config.numerics.integers_as_exact = True
9+
try:
10+
from IPython import get_ipython
11+
12+
if get_ipython():
13+
get_ipython().input_transformers_post.append(integers_as_exact)
14+
algwsym_config.numerics.integers_as_exact = True
15+
except ModuleNotFoundError:
16+
pass
1317

1418
from algebra_with_sympy.preparser import *
1519

algebra_with_sympy/algebraic_equation.py

+37-27
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,12 @@ def integers_as_exact(self):
389389
return self.integers_as_exact
390390

391391
def __latex_override__(expr, *arg):
392+
algwsym_config = False
393+
ip = False
392394
try:
393395
from IPython import get_ipython
396+
if get_ipython():
397+
ip = True
394398
except ModuleNotFoundError:
395399
pass
396400
colab = False
@@ -401,7 +405,7 @@ def __latex_override__(expr, *arg):
401405
pass
402406
show_code = False
403407
latex_as_equations = False
404-
if get_ipython():
408+
if ip:
405409
algwsym_config = get_ipython().user_ns.get("algwsym_config", False)
406410
else:
407411
algwsym_config = globals()['algwsym_config']
@@ -492,17 +496,20 @@ def set_integers_as_exact():
492496
mode of algebra_with_sympy. To turn this off call
493497
`unset_integers_as_exact()`.
494498
"""
499+
ip = False
495500
try:
496501
from IPython import get_ipython
502+
ip = True
497503
except ModuleNotFoundError:
498-
pass
499-
if get_ipython():
500-
get_ipython().input_transformers_post.append(integers_as_exact)
501-
algwsym_config = get_ipython().user_ns.get("algwsym_config", False)
502-
if algwsym_config:
503-
algwsym_config.numerics.integers_as_exact = True
504-
else:
505-
raise ValueError("The algwsym_config object does not exist.")
504+
ip = False
505+
if ip:
506+
if get_ipython():
507+
get_ipython().input_transformers_post.append(integers_as_exact)
508+
algwsym_config = get_ipython().user_ns.get("algwsym_config", False)
509+
if algwsym_config:
510+
algwsym_config.numerics.integers_as_exact = True
511+
else:
512+
raise ValueError("The algwsym_config object does not exist.")
506513
return
507514

508515
def unset_integers_as_exact():
@@ -516,22 +523,25 @@ def unset_integers_as_exact():
516523
starts with `set_integers_as_exact()` enabled (
517524
`algwsym_config.numerics.integers_as_exact = True`).
518525
"""
526+
ip = False
519527
try:
520528
from IPython import get_ipython
529+
ip = True
521530
except ModuleNotFoundError:
522-
pass
523-
if get_ipython():
524-
pre = get_ipython().input_transformers_post
525-
# The below looks excessively complicated, but more reliably finds the
526-
# transformer to remove across varying IPython environments.
527-
for k in pre:
528-
if "integers_as_exact" in k.__name__:
529-
pre.remove(k)
530-
algwsym_config = get_ipython().user_ns.get("algwsym_config", False)
531-
if algwsym_config:
532-
algwsym_config.numerics.integers_as_exact = False
533-
else:
534-
raise ValueError("The algwsym_config object does not exist.")
531+
ip = False
532+
if ip:
533+
if get_ipython():
534+
pre = get_ipython().input_transformers_post
535+
# The below looks excessively complicated, but more reliably finds the
536+
# transformer to remove across varying IPython environments.
537+
for k in pre:
538+
if "integers_as_exact" in k.__name__:
539+
pre.remove(k)
540+
algwsym_config = get_ipython().user_ns.get("algwsym_config", False)
541+
if algwsym_config:
542+
algwsym_config.numerics.integers_as_exact = False
543+
else:
544+
raise ValueError("The algwsym_config object does not exist.")
535545

536546
return
537547

@@ -557,16 +567,17 @@ def units(names):
557567
"""
558568
from sympy.core.symbol import symbols
559569
#import __main__ as shell
570+
user_namespace = None
560571
try:
561572
from IPython import get_ipython
573+
if get_ipython():
574+
user_namespace = get_ipython().user_ns
562575
except ModuleNotFoundError:
563576
pass
564577
syms = names.split(' ')
565-
user_namespace = None
566578
retstr = ''
567-
if get_ipython():
568-
user_namespace = get_ipython().user_ns
569-
else:
579+
580+
if user_namespace==None:
570581
import sys
571582
frame_num = 0
572583
frame_name = None
@@ -703,7 +714,6 @@ def solveset(f, symbols, domain=sympy.Complexes):
703714
equations unless you directly select `linsolve`, etc...
704715
"""
705716
from sympy.solvers import solveset as solve
706-
from IPython.display import display
707717
newf = []
708718
solns = []
709719
displaysolns = []

version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.0.2rc1'
1+
__version__ = '1.0.2rc4'

0 commit comments

Comments
 (0)