Skip to content

Commit

Permalink
bug fix - binary literals
Browse files Browse the repository at this point in the history
0b10 was accidentally transformed to 0*b10
  • Loading branch information
idanpa committed Mar 28, 2024
1 parent b59491b commit 06e1918
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
6 changes: 5 additions & 1 deletion calcpy/tests/test_transformers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sympy import symbols, I
from datetime import datetime

def test_autodate(ip):
def test_auto_date(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)
Expand All @@ -26,6 +26,10 @@ def test_auto_product(ip):
assert ip.run_cell('f\'{1.1:1.2f}\'').result == '1.10'
assert ip.run_cell('\'%1.2f\' % 1.234').result == '1.23'

# check no false positives:
assert ip.run_cell('0b1010').result == 0b1010
assert ip.run_cell('0x10f').result == 0x10f

def test_unicode_pow(ip):
x = ip.run_cell('x').result
assert ip.run_cell('x⁻¹³⁴').result == x**-134
Expand Down
8 changes: 4 additions & 4 deletions calcpy/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ def auto_prod_replace(match):
return match[0]

if ip.calcpy.auto_product:
# number - hex number | engineering number | number
num_pat = r'0x[0-9a-f]*|0X[0-9A-F]*|\d*\.?\d+e-?\d+|\d*\.?\d+'
# number - binary | hex | engineering number | number
num_pat = r'0[bB][01]*|0[xX][0-9a-fA-F]*|\d*\.?\d+e-?\d+|\d*\.?\d+'
# prod - (format spec|middle of name detection)?(number)(var name)?
prod_pat = rf'(: *|[^\d\W])?({num_pat})({var_pat})?'
code = re.sub(prod_pat, auto_prod_replace, code)

# pattern is (right parentheses)(hex number | engineering number | number | var name)
prod_pat = rf'(\))(0x[0-9a-f]*|0X[0-9A-F]*|\d*\.?\d+e-?\d+|\d*\.?\d+|{var_pat})'
# pattern is (right parentheses)(binary | hex | engineering number | number | var name)
prod_pat = rf'(\))(0[bB][01]*|0[xX][0-9a-fA-F]*|\d*\.?\d+e-?\d+|\d*\.?\d+|{var_pat})'
code = re.sub(prod_pat, r'\1*\2', code)

for key, val in latex_matches.items():
Expand Down

0 comments on commit 06e1918

Please sign in to comment.