Skip to content

Commit

Permalink
Convert range() objects using 'integer_interval' from cd 'interval1'
Browse files Browse the repository at this point in the history
This issue adds translation for the range() class to the
DefaultConverter. Note that a python range(start, stop) does not include
the value of stop, whereas the 'integer_interval' symbol does. The
converter thus has to add or subtract one.

Fixes OpenMath#29.
  • Loading branch information
tkw1536 committed May 21, 2020
1 parent 4906aa9 commit f6f25c9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions openmath/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class BasicPythonConverter(Converter):
- complex numbers (recursively),
- strings,
- bytes,
- ranges,
- lists (recursively),
- sets (recursively).
"""
Expand All @@ -278,6 +279,8 @@ def __init__(self):
r('set1', 'set', lambda *args: set(args))
r('list1', 'list', lambda *args: list(args))
r('complex1', 'complex_cartesian', complex) # this does not work if the arguments are not numbers
r('interval1', 'integer_interval', lambda x,y: range(x, y + 1, 1))

# literals
s = self.register_to_python_class
s(om.OMInteger, lambda o: o.integer)
Expand Down Expand Up @@ -311,6 +314,12 @@ def do_set(s):
else:
return oms('emptyset', cd='set1')
t(set, do_set)
def do_range(r):
if r.step != 1:
raise ValueError('Cannot convert %r to OpenMath: Range must have a step of 1. ' % obj)
return om.OMApplication(oms('integer_interval', 'interval1'), [om.OMInteger(r.start), om.OMInteger(r.stop - 1)])
t(range, do_range)



# A default converter instance for convenience
Expand Down
1 change: 1 addition & 0 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_py_om_py(self):
"", "test",
[], [1,2,3],
set(), set([1,2,3]),
range(1, 12)
]
for obj in testcases:
conv = DefaultConverter.to_python(DefaultConverter.to_openmath(obj))
Expand Down

0 comments on commit f6f25c9

Please sign in to comment.