Пример #1
0
def test_Key_is_leaf():
    assert Key('A').is_leaf()

    assert not Key('A|B').is_leaf()
    assert (Key('A|B') << 1).is_leaf()

    d = {
        'A': {
            'arg1': {},
            'arg2': {},
            'arg3': {},
        },
        'A|B': {
            'arg1': {},
            'arg2': {},
            'arg3': {},
        },
        'A|B|C': {
            'arg1': {},
            'arg2': {},
            'arg3': {},
        },
    }

    keys = list(map(lambda key: Key(key), list(d.keys())))
    leaves = list(filter(lambda key: key.is_leaf(), keys))
    assert len(leaves) == 1 and leaves[0] == Key('A')

    leaves = list(filter(lambda key: (key << 1).is_leaf(), keys))
    assert len(leaves) == 1 and leaves[0] == Key('A|B')

    leaves = list(filter(lambda key: (key << 2).is_leaf(), keys))
    assert len(leaves) == 1 and leaves[0] == Key('A|B|C')
Пример #2
0
    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)
Пример #3
0
def test_Key_shift():
    # because the Key class preserves the key captured at object
    # instantiation you have to deliberately tamper with the Key's value
    # to make it equal to the shifted Node

    key = Key('A|B')
    key.value = Node(head='B', tail=[])

    assert (Key('A|B') << 1) == key

    key.value = Node(head=None, tail=[])
    assert (Key('A|B') << 2) == key
Пример #4
0
def test_Key_has_children():
    assert not Key().has_children()
    assert not Key('A').has_children()

    assert Key('A|B').has_children()
Пример #5
0
def test_Key_is_empty():
    assert Key().is_empty()
    assert not Key('A').is_empty()
Пример #6
0
def test_Key_Node_from_key():
    assert Key.Node_from_key('A|B|C') == Node(head='A', tail=['B', ['C']])
Пример #7
0
def test_Key_unflatten():
    key = 'A|B|C'
    assert Key.unflatten(Key.split(key)) == ['A', ['B', ['C']]]
Пример #8
0
def test_Key_split():
    key = 'A|B|C'
    assert Key.split(key) == ['A', '|', 'B', '|', 'C']
Пример #9
0
def test_Key():
    key = Key()
    assert key.key is None
    assert key.value == Node()
    assert key.payload is None