mathjson-solver 1.12.0__tar.gz → 1.13.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: mathjson-solver
3
- Version: 1.12.0
3
+ Version: 1.13.0
4
4
  Summary: Utilities for MathJSON evaluation
5
5
  Home-page: https://github.com/LongenesisLtd/mathjson-solver
6
6
  Author: Martins Mednis
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = mathjson-solver
3
- version = 1.12.0
3
+ version = 1.13.0
4
4
  author = Martins Mednis
5
5
  author_email = mrt@mednis.info
6
6
  description = Utilities for MathJSON evaluation
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setuptools.setup(
7
7
  name="mathjson-solver",
8
- version="1.12.0",
8
+ version="1.13.0",
9
9
  author="Martins Mednis",
10
10
  author_email="mrt@mednis.info",
11
11
  description="Utilities for MathJSON evaluation",
@@ -1,5 +1,4 @@
1
1
  import numbers
2
- from types import NoneType
3
2
  from typing import Union
4
3
  from functools import reduce
5
4
  import math
@@ -9,6 +8,8 @@ import logging
9
8
  import traceback
10
9
  import datetime
11
10
 
11
+ NoneType = type(None)
12
+
12
13
 
13
14
  class MathJSONException(Exception):
14
15
  """Exception for MathJSON processing issues"""
@@ -253,6 +254,20 @@ def create_mathjson_solver(solver_parameters):
253
254
  raise ValueError(
254
255
  "Case of 'Switch' should have exactly two parameters"
255
256
  )
257
+ if comparison_safe_converter(
258
+ expression
259
+ ) == comparison_safe_converter(f(x[0], c)):
260
+ return f(x[1], c)
261
+ else:
262
+ return f(s[2], c)
263
+
264
+ def StrictSwitch(s):
265
+ expression = f(s[1], c)
266
+ for x in s[3:]:
267
+ if len(x) != 2:
268
+ raise ValueError(
269
+ "Case of 'StrictSwitch' should have exactly two parameters"
270
+ )
256
271
  if expression == f(x[0], c):
257
272
  return f(x[1], c)
258
273
  else:
@@ -431,6 +446,7 @@ def create_mathjson_solver(solver_parameters):
431
446
  ),
432
447
  "Constants": Constants,
433
448
  "Switch": Switch,
449
+ "StrictSwitch": StrictSwitch,
434
450
  "If": If,
435
451
  "Multiply": lambda s: reduce(
436
452
  lambda a, b: float(a) * float(b), [f(x, c) for x in s[1:]]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: mathjson-solver
3
- Version: 1.12.0
3
+ Version: 1.13.0
4
4
  Summary: Utilities for MathJSON evaluation
5
5
  Home-page: https://github.com/LongenesisLtd/mathjson-solver
6
6
  Author: Martins Mednis
@@ -132,6 +132,16 @@ from mathjson_solver import create_solver, MathJSONException, extract_variables
132
132
  # ],
133
133
  # 13
134
134
  # ),
135
+
136
+
137
+ # The next tests check the type-forgiving nature of Switch.
138
+ ({"value": 2}, ["Switch", "value", 0, ["1", 11], ["2", 22]], 22),
139
+ ({"value": "2"}, ["Switch", "value", 0, [1, 11], [2, 22]], 22),
140
+
141
+ # The next tests verify that the StrictSwitch is not forgiving.
142
+ ({"value": 2}, ["StrictSwitch", "value", 0, ["1", 11], ["2", 22]], 0),
143
+ ({"value": "2"}, ["StrictSwitch", "value", 0, [1, 11], [2, 22]], 0),
144
+
135
145
  ({"color": "red"}, ["Switch", "color", 0, ["blue", 10], ["red", 30]], 30),
136
146
  ({"color": "green"}, ["Switch", "color", 0, ["blue", 10], ["red", 30]], 0),
137
147
  ({"color": "green"}, ["Switch", "undefined", 0, ["blue", 10], ["red", 30]], 0),