Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
idanpa committed Dec 31, 2023
1 parent 06a79c2 commit 2010497
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion calcpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def unload_previewer(self):

def load_ipython_extension(ip:IPython.InteractiveShell):
if ip.profile != CALCPY_PROFILE_NAME:
print(f'warning: Not using the {CALCPY_PROFILE_NAME} profile (current profile is {ip.profile}')
print(f'warning: Not using the {CALCPY_PROFILE_NAME} profile (current profile: \'{ip.profile}\')')

ip.calcpy = CalcPy(ip)
ip.push({'calcpy': ip.calcpy}, interactive=False)
Expand Down
1 change: 0 additions & 1 deletion calcpy/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import calcpy
import IPython
import argparse
import os

def main():
parser = argparse.ArgumentParser()
Expand Down
7 changes: 4 additions & 3 deletions calcpy/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def sympy_expr_formatter(s, printer, cycle):
out = pretty_s

try:
if not isinstance(s, (sympy.core.numbers.Integer, sympy.core.numbers.Float)):
if not isinstance(s, (sympy.Integer, sympy.Float)):
evalu_s = pretty(evalf(s), n_col - len(" ≈ "), n_row)
if evalu_s != pretty_s:
out = pretty_stack(out, " ≈ ", evalu_s, n_col)
Expand Down Expand Up @@ -215,6 +215,7 @@ def init(ip: IPython.InteractiveShell):
sympy.interactive.printing.init_printing(
pretty_print=True,
use_latex='mathjax',
use_unicode=not (ip.config.TerminalInteractiveShell.simple_prompt==True),
num_columns=shutil.get_terminal_size().columns,
ip=ip)

Expand All @@ -223,7 +224,7 @@ def init(ip: IPython.InteractiveShell):
IPython.lib.pretty.for_type(datetime.datetime, datetime_formatter)
IPython.lib.pretty.for_type(datetime.timedelta, timedelta_formatter)
IPython.lib.pretty.for_type(sympy.printing.defaults.Printable, ip_sympy_pretty_if_oneline_formatter)
IPython.lib.pretty.for_type(sympy.matrices.common.MatrixShaping, ip_matrix_formatter)
IPython.lib.pretty.for_type(sympy.matrices.MatrixBase, ip_matrix_formatter)
IPython.lib.pretty.for_type(sympy.combinatorics.Cycle, sympy_pretty_formatter)
IPython.lib.pretty.for_type(sympy.combinatorics.Permutation, ip_permutation_formatter)

Expand All @@ -238,7 +239,7 @@ def init(ip: IPython.InteractiveShell):
formatter.for_type(tuple, iterable_formatter)
formatter.for_type(dict, sympy_dict_formatter)
formatter.for_type(sympy.Expr, sympy_expr_formatter)
formatter.for_type(sympy.matrices.common.MatrixCommon, sympy_expr_formatter)
formatter.for_type(sympy.matrices.MatrixBase, sympy_expr_formatter)
formatter.for_type(sympy.core.function.FunctionClass, IPython.lib.pretty._function_pprint)
formatter.for_type(sympy.combinatorics.Permutation, sympy_pretty_formatter)
formatter.for_type(sympy.combinatorics.Cycle, sympy_pretty_formatter)
Expand Down
20 changes: 7 additions & 13 deletions calcpy/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,14 @@ def print_info_job(res):
try:
res_p = pretty(res)

if isinstance(res, (float, sympy.core.numbers.Float)):
if isinstance(res, (float, sympy.Float)):
print(f'\n{pretty(sympy.Rational(res))} = Rational(_)')
elif isinstance(res, (complex, sympy.core.numbers.Float)):
elif isinstance(res, (complex, sympy.Rational)):
pass
elif isinstance(res, (int, sympy.core.numbers.Integer)):
f_dict = sympy.factorint(res)
f_expr = None
for base, expo in f_dict.items():
pow = sympy.Pow(base, expo, evaluate=False)
if f_expr is None:
f_expr = pow
else:
f_expr = sympy.Mul(f_expr, pow, evaluate=False)
print(f'\n{pretty(f_expr)} {pretty(f_dict)} = _.factorint()')
elif isinstance(res, (int, sympy.Integer)):
factors_dict = sympy.factorint(res)
factors_expr = sympy.Mul(*[sympy.Pow(base, expo, evaluate=False) for base, expo in factors_dict.items()], evaluate=False)
print(f'\n{pretty(factors_expr)} {pretty(factors_dict)} = factorint(_)')
elif isinstance(res, sympy.Expr):
# sympy.factor(res, extension=[i]) could be nice (when len(res.free_symbols) >= 1) but not working most of the time
if len(res.free_symbols) == 1:
Expand Down Expand Up @@ -119,7 +113,7 @@ def print_info_job(res):
N_p = pretty(sympy.N(res))
if N_p != res_p:
print(f'\n{N_p} = N(_)')
elif isinstance(res, sympy.matrices.common.MatrixCommon):
elif isinstance(res, sympy.matrices.MatrixBase):
if res.rows == res.cols:
print(f'\n{pretty(sympy.det(res))} = det(_)')
print(f'{pretty(sympy.trace(res))} = trace(_)')
Expand Down
2 changes: 1 addition & 1 deletion calcpy/tests/test_startup_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_startup_time(n=10):
start_time = time.time()
subprocess.check_call(['calcpy', '-c', '4+4'], stdout=None, stdin=subprocess.PIPE)
elapsed.append(time.time() - start_time)
print(elapsed)
print(elapsed[-1])
print(f'max {max(elapsed)}')
print(f'min {min(elapsed)}')
print(f'avg {sum(elapsed)/n}')
Expand Down
2 changes: 2 additions & 0 deletions calcpy/tests/test_transformers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from sympy import symbols
from datetime import datetime

def test_autodate(ip):
dt = ip.run_cell('d"today"-d"yesterday"').result
assert dt.days == 1 or 24*60*60-1 <= dt.seconds <= 24*60*60
assert ip.run_cell('d\'1 January 1970\'').result == datetime(1970, 1, 1)

def test_auto_product(ip):
assert ip.run_cell('2(1+1)').result == 4
Expand Down
6 changes: 6 additions & 0 deletions previewer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def sandbox_pre(self):
sys.modules[module_name] = None

def sandbox_post(self):
try:
# cache tz before removing access to it
import tzlocal
tzlocal.reload_localzone()
except (ModuleNotFoundError, ImportError):
pass
import subprocess
subprocess.Popen = None
import builtins
Expand Down
5 changes: 4 additions & 1 deletion previewer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
ns_conn, ns_conn_c = mp.Pipe()
sys.stdin.close()
proc = IPythonProcess(exec_conn_c, ctrl_conn_c, ns_conn_c, interactive=True)
proc.join()
try:
proc.join()
except KeyboardInterrupt:
pass
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
install_requires=[
'requests',
'ipython',
'pickleshare',
'matplotlib',
'sympy',
'dateparser',
Expand Down

0 comments on commit 2010497

Please sign in to comment.