Skip to content

Instantly share code, notes, and snippets.

@abloch
Created June 14, 2019 11:26
Show Gist options
  • Save abloch/c8997b423da3bef33f645a0ec84ab152 to your computer and use it in GitHub Desktop.
Save abloch/c8997b423da3bef33f645a0ec84ab152 to your computer and use it in GitHub Desktop.
a tool to create an object from a reader string
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