Created
June 14, 2019 11:26
-
-
Save abloch/c8997b423da3bef33f645a0ec84ab152 to your computer and use it in GitHub Desktop.
a tool to create an object from a reader string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections.abc import Iterable | |
from util.tree.list import List | |
def tree_to_obj(tree): | |
if hasattr(tree, 'head') and hasattr(tree, 'children'): | |
return { | |
tree.head: tree_to_obj(tree.children) | |
} | |
if isinstance(tree, str): | |
return tree | |
if isinstance(tree, type(List)): | |
return [tree_to_obj(o) for o in tree.children] | |
if isinstance(tree, Iterable): | |
return [tree_to_obj(o) for o in tree] | |
return tree | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment