Skip to content

Commit

Permalink
[Python] Add ability to pass YAML strings to dict_to_anymap
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Jan 19, 2022
1 parent c36cd33 commit 321b3d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 14 additions & 0 deletions interfaces/cython/cantera/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,17 @@ def test_unconvertible(self):

def test_unconvertible2(self):
self.check_raises([3+4j, 1-2j], ct.CanteraError, "Unable to convert")

def test_yaml(self):
out, held_type = _py_to_any_to_py("{a: 1, b: 2., c: eggs, d: True}")
self.assertEqual(out, {"a": 1, "b": 2., "c": "eggs", "d": True})
self.assertEqual(held_type, "AnyMap")

def test_mixed_yaml(self):
out, held_type = _py_to_any_to_py({"a": 1, "b": 2., "c": "{d: eggs}"})
self.assertEqual(out, {"a": 1, "b": 2., "c": {"d": "eggs"}})
self.assertEqual(held_type, "AnyMap")

def test_invalid_yaml(self):
# a regular string is returned as the input is not a valid YAML sequence
self.check_conversion("a: 1, b: 2., c: eggs, d: True", "string")
5 changes: 4 additions & 1 deletion interfaces/cython/cantera/utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ cdef CxxAnyValue python_to_anyvalue(item, name=None) except *:
else:
v = list_to_anyvalue(item)
elif isinstance(item, str):
v = stringify(item)
try:
v = AnyMapFromYamlString(stringify(item))
except BaseException:
v = stringify(item)
elif isinstance(item, bool):
v = <cbool>(item)
elif isinstance(item, int):
Expand Down

0 comments on commit 321b3d3

Please sign in to comment.