Skip to content

Commit 3eb9dcb

Browse files
JWock82JWock82
authored andcommitted
Update for v0.0.73
1 parent 1b33097 commit 3eb9dcb

File tree

2 files changed

+18
-38
lines changed

2 files changed

+18
-38
lines changed

ipycalc/calc.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -265,47 +265,30 @@ def process_line(calc_line, local_ns):
265265
# The variable does not carry units
266266
unit = None
267267

268-
# Sometimes the user may want to display a value without an equation
269-
if equation == '':
270-
271-
if unit != None:
272-
value = str((eval(variable).to(unit)).magnitude) + '*' + unit
273-
elif precision != None:
274-
# Unitless values require special consideration. Pint leaves values in terms of the
275-
# units used to calculate them. That means 60 ft / 12 in = 5 ft/in instead of 60.
276-
# As a workaround we'll convert that quantity to some units that cancel each other out.
277-
# In cases where there are no units (a pure float) we'll need to tag on some units
278-
# to make the `to` function available. These units should also cancel each other out.
279-
value = str((eval(variable)*inch/inch).to(inch/inch))
280-
else:
281-
eval_result = eval(variable)
282-
if isinstance(eval_result, str):
283-
value = repr(eval_result) # Use repr to get quoted string
284-
else:
285-
value = str(eval_result)
286-
287-
else:
288-
289-
if unit != None:
290-
value = str((eval(equation).to(unit)).magnitude) + '*' + unit
291-
elif precision != None:
292-
# Unitless values require special consideration. Pint leaves values in terms of the
293-
# units used to calculate them. That means 60 ft / 12 in = 5 ft/in instead of 60.
294-
# As a workaround we'll convert that quantity to some units that cancel each other out.
295-
# In cases where there are no units (a pure float) we'll need to tag on some units
296-
# to make the `to` function available. These units should also cancel each other out.
297-
value = str((eval(equation)*inch/inch).to(inch/inch))
268+
# Determine what expression to evaluate (equation or variable)
269+
expr_to_eval = equation if equation != '' else variable
270+
271+
# Helper function to compute the value string
272+
def compute_value(expression, target_unit, target_precision):
273+
"""Computes the value string for storage and later evaluation"""
274+
if target_unit != None:
275+
return str((eval(expression).to(target_unit)).magnitude) + '*' + target_unit
276+
elif target_precision != None:
277+
# Unitless values - convert to ensure proper cancellation
278+
return str((eval(expression)*inch/inch).to(inch/inch))
298279
else:
299-
eval_result = eval(equation)
280+
eval_result = eval(expression)
300281
if isinstance(eval_result, str):
301-
value = repr(eval_result) # Use repr to get quoted string
282+
return repr(eval_result) # Use repr to get quoted string
302283
else:
303-
value = str(eval_result)
284+
return str(eval_result)
285+
286+
# Compute the value
287+
value = compute_value(expr_to_eval, unit, precision)
304288

305289
# When pint returns unit strings, it uses abbreviated forms like 'in' instead of 'inch'
306290
# 'in' is a Python keyword, so we need to convert it back to 'inch' for eval()
307291
# Use regex to replace 'in' only when it appears as a standalone unit (word boundary)
308-
import re
309292
value = re.sub(r'\bin\b', 'inch', value)
310293
value = value.strip()
311294

@@ -391,8 +374,6 @@ def python_to_latex(text):
391374

392375
# Replace all quoted strings (both single and double quotes) with unquoted sans-serif versions
393376
# Protect spaces within strings by temporarily replacing them
394-
import re
395-
# Handle double-quoted strings - replace spaces with placeholder
396377
def replace_string(match):
397378
content = match.group(1).replace(' ', '~')
398379
return '\\textsf{' + content + '}'
@@ -541,7 +522,6 @@ def process_if(text, type):
541522

542523
return latex_text
543524

544-
#%%
545525
def curly_brackets(fname, text):
546526

547527
while fname + '(' in text:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="ipycalc",
8-
version="0.0.72",
8+
version="0.0.73",
99
author="D. Craig Brinck, PE, SE",
1010
author_email="Building.Code@outlook.com",
1111
description="Clean looking engineering calculations for IPython",

0 commit comments

Comments
 (0)