Exemple #1
0
 def test_node_from_path_with_missing_failing(self):
     root = TreeNode()
     with self.assertRaisesRegex(
             KeyError,
             re.escape(
                 r"Node with key 'hello' in path 'hello/world' does not exist."
             )):
         root.node_from_path(PurePosixPath("./hello/world"))
Exemple #2
0
 def test_node_from_path_with_missing_integer_key(self):
     root = TreeNode(default=lambda: None)
     self.assertEqual(
         str(root.node_from_path(PurePosixPath("./hello/[0]/world"))),
         "<TreeNode path='/hello/[0]/world' key='world' value='None'>")
     zero_node = root.node_from_path(PurePosixPath("./hello/[0]"))
     self.assertEqual(str(zero_node),
                      "<TreeNode path='/hello/[0]' key='0' value='None'>")
     self.assertIsInstance(zero_node.key, int)
Exemple #3
0
    def test_object_hierarchy(self):
        root = TreeNode(value=111)
        child1 = TreeNode(key="foo", parent=root)
        child2 = TreeNode(value=111, key=0, parent=child1)
        child3 = TreeNode(value=222, key=1, parent=child1)
        child4 = TreeNode(key=2, parent=child1)

        child5 = TreeNode(key="bar", parent=child4)
        child6 = TreeNode(key="quux", parent=child5)
        child7 = TreeNode(value=333, key="quuz", parent=child6)

        correct_tree = {'foo': [111, 222, {'bar': {'quux': {'quuz': 333}}}]}

        errors = object_hierarchy_equals(root.object_hierarchy(), correct_tree)
        self.assertEqual(len(errors), 0)
Exemple #4
0
 def test_node_from_path_integer_keys(self):
     root = TreeNode()
     child1 = TreeNode(key="foo", parent=root)
     child2 = TreeNode(value=333, key=0, parent=child1)
     child3 = TreeNode(value=333, key=1, parent=child1)
     child4 = TreeNode(value=333, key=2, parent=child1)
     correct_tree = {'foo': [333, 333, 333]}
     errors = object_hierarchy_equals(root.object_hierarchy(), correct_tree)
     self.assertEqual(len(errors), 0)
Exemple #5
0
    def test_path_property(self):
        root = TreeNode(value=111)
        child1 = TreeNode(key="foo", parent=root)
        child2 = TreeNode(key="bar", parent=child1)
        child3 = TreeNode(key="baz", parent=child2)
        child4 = TreeNode(value=333, key="qux", parent=child3)

        child5 = TreeNode(key="qux", parent=child2)
        child6 = TreeNode(key="quux", parent=child5)
        child7 = TreeNode(value=333, key="quuz", parent=child6)

        self.assertEqual(PurePosixPath("/foo/bar/baz/qux"), child4.path)
        self.assertEqual(PurePosixPath("/foo/bar/qux/quux/quuz"), child7.path)
Exemple #6
0
def tables_to_object_hierarchy(tables):
    """
    Transforms a dictionary of tables into a object hierarchy.

    List elements with the same name ending with trailing integers separated
    by underscores (example `name_0_1`) are combined into a single
    :class:`numpy.ndarray` with n+1 dimensions, where n is the number of trailing
    integers. The first dimension's size is the same as the length of the lists.
    The trailing integers then define the index of the higher dimensions where
    the data will be inserted to.

    Elements have be grouped in sub-dictionaries. If the key of the dictionary
    is a string it will be appended to the front of the child elements name.
    It can thus be used to define namespaces. Otherwise the key is ignored.

    Example
    .. code-block:: python

       inp = {
           4: {
               'foo.quux_0_0': np.array([9, 11, 14, 17]),
               'foo.quux_1_0': np.array([10, 12, 15, 18]),
               'foo.quux_2_0': np.array([11, 13, 16, 19])
           },
           "bar": {
               "bas": 123
           }
       }

       {
           'foo': {
               'quux': np.array([[[9.],  [10.], [11.]],
                                 [[11.], [12.], [13.]],
                                 [[14.], [15.], [16.]],
                                 [[17.], [18.], [19.]]])
           },
           "bar": {
               "bas": 123
           }
       }

    :param tables: Dictionary containing dictionaries whose key is the path
       to the element in the resulting object hierarchy.
    :return: Object hierarchy
    """
    data_tables = flatten_tables(tables)
    process_ndarrays(data_tables)
    root_node = TreeNode.from_path_value_pairs(data_tables)
    root_obj = root_node.object_hierarchy()
    return root_obj
Exemple #7
0
    def test_from_path_dict(self):
        path_value_pairs = [
            (PurePosixPath("hello/world"), "!!!"),
            (PurePosixPath("foo/[0]"), "spam"),
            (PurePosixPath("/foo/[2]"), "eggs"),
            (PurePosixPath("foo/[1]"), "ham"),
        ]

        root = TreeNode.from_dict(dict(path_value_pairs))
        correct_tree = {
            'foo': ['spam', 'ham', 'eggs'],
            'hello': {
                'world': '!!!'
            }
        }
        errors = object_hierarchy_equals(root.object_hierarchy(), correct_tree)
        self.assertEqual(len(errors), 0)
Exemple #8
0
 def test_node_from_path_with_missing(self):
     root = TreeNode(default=lambda: None)
     self.assertEqual(
         str(root.node_from_path(PurePosixPath("./hello/world"))),
         "<TreeNode path='/hello/world' key='world' value='None'>")
Exemple #9
0
    def test_node_from_path(self):
        root = TreeNode(value=111)
        child1 = TreeNode(key="foo", parent=root)
        child2 = TreeNode(key="bar", parent=child1)
        child3 = TreeNode(key="baz", parent=child2)
        child4 = TreeNode(value=333, key="qux", parent=child3)

        child5 = TreeNode(key="qux", parent=child2)
        child6 = TreeNode(key="quux", parent=child5)
        child7 = TreeNode(value=333, key="quuz", parent=child6)

        self.assertEqual(str(root.node_from_path(PurePosixPath())),
                         "<TreeNode path='/' key='' value='111'>")
        self.assertEqual(str(root.node_from_path(PurePosixPath(".."))),
                         "<TreeNode path='/' key='' value='111'>")
        self.assertEqual(str(child1.node_from_path(PurePosixPath(".."))),
                         "<TreeNode path='/' key='' value='111'>")
        self.assertEqual(
            str(child4.node_from_path(PurePosixPath("../../../.."))),
            "<TreeNode path='/' key='' value='111'>")

        self.assertEqual(
            str(root.node_from_path(PurePosixPath("/foo/bar/baz/.."))),
            "<TreeNode path='/foo/bar' key='bar' value='None'>")
        self.assertEqual(
            str(child4.node_from_path(PurePosixPath("../../qux/quux/quuz"))),
            "<TreeNode path='/foo/bar/qux/quux/quuz' key='quuz' value='333'>")

        self.assertEqual(str(child6.node_from_path(PurePosixPath("/foo/bar"))),
                         "<TreeNode path='/foo/bar' key='bar' value='None'>")