Skip to content

Commit 459d247

Browse files
Fixed JS-like String-logic in wrapper.py
1 parent 53584ba commit 459d247

1 file changed

Lines changed: 11 additions & 21 deletions

File tree

microps/wrappers/wrapper.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def get_mm(obj_raw, name, engine=None):
2222

2323
return None
2424

25+
def js_str(val):
26+
"""Formats numbers like JS: 5.0 -> '5', 5.5 -> '5.5'"""
27+
if isinstance(val, float) and val.is_integer():
28+
return str(int(val))
29+
return str(val)
30+
2531
class BaseValue:
2632
def __init__(self, v, engine):
2733
self.__dict__['_val'] = unwrap(v)
@@ -47,29 +53,13 @@ def __add__(self, o):
4753
return self.__class__(mm_func(right, left), self._engine)
4854

4955
# 3) JS-like string concatenation if either operand is a string
50-
def is_str(val):
51-
# Recognizes both Python str and wrapped JS strings
52-
return isinstance(val, str) or (hasattr(val, '_val') and isinstance(getattr(val, '_val', str)))
53-
54-
if is_str(left) or is_str(right):
55-
result = str(left) + str(right)
56+
if isinstance(left, str) or isinstance(right, str):
57+
result = js_str(left) + js_str(right)
5658
return self.__class__(result, self._engine)
57-
58-
# 4) Try converting string operands to numeric floats and do numeric add
59-
for idx, val in enumerate([left, right]):
60-
if isinstance(val, str):
61-
try:
62-
if idx == 0:
63-
left = float(left)
64-
else:
65-
right = float(right)
66-
except ValueError:
67-
pass
68-
59+
60+
# 4) Numeric Add
6961
if isinstance(left, (int, float)) and isinstance(right, (int, float)):
70-
return self.__class__(_core.add(left, right), self._engine)
71-
72-
raise TypeError(f"Cannot add {type(left).__name__} and {type(right).__name__}")
62+
return self.__class(_core.add(left, right), self._engine)
7363

7464
def __sub__(self, o): return self.__class__(_core.sub(self._val, unwrap(o)), self._engine)
7565
def __mul__(self, o): return self.__class__(_core.mul(self._val, unwrap(o)), self._engine)

0 commit comments

Comments
 (0)