mirror of https://github.com/pyodide/pyodide.git
Merge pull request #325 from mdboom/as_nested_list
Fix #316: Add convenience function for converting from nested Arrays
This commit is contained in:
commit
8d61da9480
|
@ -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']
|
||||
|
|
Loading…
Reference in New Issue