Merge pull request #325 from mdboom/as_nested_list

Fix #316: Add convenience function for converting from nested Arrays
This commit is contained in:
Michael Droettboom 2019-02-28 10:30:20 -05:00 committed by GitHub
commit 8d61da9480
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -42,6 +42,21 @@ some preprocessing on the Python code first.
Either the resulting object or `None`. 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 ## Javascript API

View File

@ -67,4 +67,16 @@ def find_imports(code):
return list(imports) 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']