def test_Tree_Value(): value = Tree.Value() assert value.name is None assert value.key is None assert value.payload is None d = { 'A|B|C': { 'arg1': {}, 'arg2': {}, 'arg3': {}, } } value = Tree.Value(name='C', key=set(d.keys()).pop(), d=d) assert value.name == 'C' assert value.key == 'A|B|C' assert value.payload == d['A|B|C']
def to_tree(d, root_name=None): assert isinstance(d, dict) root_name = 'root' if root_name is None else root_name nodes = map(lambda key: Key(key), d.keys()) nodes = list(filter(lambda x: not x.is_empty(), nodes)) def _to_tree(x): if isinstance(x, list) and len(x)==1 and x[0].is_leaf(): x = x[0] return Tree( Tree.Value(name=x.value.head(), key=x.key, d=d) ) else: name = set(map(lambda x: x.value.head(), x)) assert len(name)==1 name = name.pop() o = [] shifted = list(map(lambda x: x << 1, x)) # optional arguments added to node itself tree = None empty = list(filter(lambda x: x.is_empty(), shifted)) assert len(empty) <= 1 if len(empty): x = empty[0] tree = Tree( Tree.Value(name = name, key=x.key, d=d) ) # arguments added to child nodes children = list(filter(lambda x: not x.is_empty(), shifted)) child_names = set(map(lambda x: x.value.head(), children)) for child_name in child_names: child = list(filter(lambda x: x.value.head() == child_name, children)) o += [_to_tree(child)] if tree is not None: tree.children = o else: tree = Tree(Tree.Value(name=name, key=None), children=o) return tree o = [] for name in set(map(lambda x: x.value.head(), nodes)): node = list(filter(lambda x: x.value.head()==name, nodes)) o += [_to_tree(node)] return Tree(root_name, children=o)
def a_tree(): return Tree( 'A', children=[ Tree('B', []), Tree('BB', children=[Tree('C', []), Tree('CC', []), Tree('CCC', [])]), Tree('BBB', []), ])
def test_is_empty_Tree(): assert is_empty(None) assert is_empty(Tree())
def test_Tree_is_empty(): tree = Tree() assert tree.is_empty()
def test_Tree(): tree = Tree() assert tree.value is None assert len(tree.children) == 0