From 06e1918e0fc0d20032aa28573884e9c7dc13641b Mon Sep 17 00:00:00 2001 From: Idan Pazi Date: Thu, 28 Mar 2024 10:25:41 +0200 Subject: [PATCH] bug fix - binary literals 0b10 was accidentally transformed to 0*b10 --- calcpy/tests/test_transformers.py | 6 +++++- calcpy/transformers.py | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/calcpy/tests/test_transformers.py b/calcpy/tests/test_transformers.py index e470323..dd062ca 100644 --- a/calcpy/tests/test_transformers.py +++ b/calcpy/tests/test_transformers.py @@ -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) @@ -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 diff --git a/calcpy/transformers.py b/calcpy/transformers.py index 5e968fb..9064103 100644 --- a/calcpy/transformers.py +++ b/calcpy/transformers.py @@ -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():