Commit 8d61da94 authored by Michael Droettboom's avatar Michael Droettboom Committed by GitHub

Merge pull request #325 from mdboom/as_nested_list

Fix #316: Add convenience function for converting from nested Arrays
parents 63b6ba23 d5493955
......@@ -42,6 +42,21 @@ some preprocessing on the Python code first.
Either the resulting object or `None`.
### pyodide.as_nested_list(obj)
Converts Javascript nested arrays to Python nested lists. This conversion can not
be performed automatically, because Javascript Arrays and Objects can be combined
in ways that are ambiguous.
*Parameters*
| name | type | description |
|--------|-------|-----------------------|
| *obj* | JS Object | The object to convert |
*Returns*
The object as nested Python lists.
## Javascript API
......
......@@ -67,4 +67,16 @@ def find_imports(code):
return list(imports)
__all__ = ['open_url', 'eval_code', 'find_imports']
def as_nested_list(obj):
"""
Assumes a Javascript object is made of (possibly nested) arrays and
converts them to nested Python lists.
"""
try:
it = iter(obj)
return [as_nested_list(x) for x in it]
except TypeError:
return obj
__all__ = ['open_url', 'eval_code', 'find_imports', 'as_nested_list']
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment