Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert range() objects using 'integer_interval' from cd 'interval1' #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ By default, a ``Converter`` only implements conversions for basic Python types:
- complex numbers,
- strings,
- bytes,
- ranges,
- lists (recursively),
- sets (recursively).

Expand Down
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