OzoneAsai commited on
Commit
b1b82b2
·
verified ·
1 Parent(s): 298518f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -1,5 +1,5 @@
1
  from flask import Flask, request, render_template_string
2
- from decimal import Decimal, ROUND_HALF_UP
3
  import re
4
  from datetime import datetime
5
 
@@ -38,11 +38,16 @@ def log(message):
38
  def parse_value_unit(value_unit_str):
39
  """数値と単位をパースして、共通単位に変換"""
40
  log(f"Parsing: {value_unit_str}")
41
- match = re.match(r"([\d.]+)\s*\[([a-zA-Zµ^2]+)\]", value_unit_str)
42
  if match:
43
  value, unit = match.groups()
44
  log(f"Parsed value: {value}, unit: {unit}")
45
- return Decimal(value), unit, len(value.split('.')[1]) if '.' in value else 0
 
 
 
 
 
46
  else:
47
  raise ValueError("Invalid format. The input should be in the form 'value[unit]'.")
48
 
@@ -69,7 +74,7 @@ def convert_to_common_unit(value, from_unit, to_unit):
69
  def calculate_expression(expression):
70
  """複数の数値を処理する計算(加算、減算、乗算、除算に対応)"""
71
  log(f"Calculating expression: {expression}")
72
- pattern = r"([\d.]+\s*\[[^\]]+\])"
73
  terms = re.findall(pattern, expression)
74
  log(f"Found terms: {terms}")
75
 
@@ -103,9 +108,13 @@ def calculate_expression(expression):
103
  # 最も少ない有効数字に基づいて丸める
104
  min_sig_figs = min(sig_figs_list)
105
  rounded_result = total_value.quantize(Decimal('1e{0}'.format(-(min_sig_figs - 1))), rounding=ROUND_HALF_UP)
106
- log(f"Final result: {rounded_result} [{total_unit}]")
107
 
108
- return f"{rounded_result} [{total_unit}]"
 
 
 
 
 
109
 
110
  # HTMLテンプレート(バイト列でエンコード)
111
  template = b"""
@@ -190,6 +199,7 @@ def calculate():
190
  result = calculate_expression(expression)
191
  return result
192
  except Exception as e:
 
193
  return f"Error: {str(e)}", 400
194
 
195
  if __name__ == '__main__':
 
1
  from flask import Flask, request, render_template_string
2
+ from decimal import Decimal, ROUND_HALF_UP, InvalidOperation
3
  import re
4
  from datetime import datetime
5
 
 
38
  def parse_value_unit(value_unit_str):
39
  """数値と単位をパースして、共通単位に変換"""
40
  log(f"Parsing: {value_unit_str}")
41
+ match = re.match(r"([\d.eE+-]+)\s*\[([a-zA-Zµ^2]+)\]", value_unit_str)
42
  if match:
43
  value, unit = match.groups()
44
  log(f"Parsed value: {value}, unit: {unit}")
45
+ try:
46
+ decimal_value = Decimal(value)
47
+ except InvalidOperation:
48
+ raise ValueError(f"Invalid numeric value: {value}")
49
+ sig_figs = len(value.split('.')[1]) if '.' in value else 0
50
+ return decimal_value, unit, sig_figs
51
  else:
52
  raise ValueError("Invalid format. The input should be in the form 'value[unit]'.")
53
 
 
74
  def calculate_expression(expression):
75
  """複数の数値を処理する計算(加算、減算、乗算、除算に対応)"""
76
  log(f"Calculating expression: {expression}")
77
+ pattern = r"([\d.eE+-]+\s*\[[^\]]+\])"
78
  terms = re.findall(pattern, expression)
79
  log(f"Found terms: {terms}")
80
 
 
108
  # 最も少ない有効数字に基づいて丸める
109
  min_sig_figs = min(sig_figs_list)
110
  rounded_result = total_value.quantize(Decimal('1e{0}'.format(-(min_sig_figs - 1))), rounding=ROUND_HALF_UP)
 
111
 
112
+ # 小数点以下のゼロを省略せずに表示
113
+ rounded_result_str = format(rounded_result, 'f')
114
+
115
+ log(f"Final result: {rounded_result_str} [{total_unit}]")
116
+
117
+ return f"{rounded_result_str} [{total_unit}]"
118
 
119
  # HTMLテンプレート(バイト列でエンコード)
120
  template = b"""
 
199
  result = calculate_expression(expression)
200
  return result
201
  except Exception as e:
202
+ log(f"Error: {str(e)}")
203
  return f"Error: {str(e)}", 400
204
 
205
  if __name__ == '__main__':