Ejemplo n.º 1
0
    def test_invert_dictionary(self):
        # Signature: name(dct)
                # Takes a dictionary mapping (keys => values) and returns a
                # new dictionry mapping (values => keys).
                # i.e. given a dictionary::
                #
                #     {k1:v1, k2:v2, k3:v3, ...}
                #
                # it returns a dictionary::
                #
                #     {v1:k1, v2:k2, v3:k3, ...}
                #
                # It checks to make sure that no values are duplicated before converting.
        from nineml.utility import invert_dictionary
        from nineml.exceptions import NineMLRuntimeError

        # Good cases:
        self.assertEqual(
            invert_dictionary({}),
            {},
        )

        self.assertEqual(
            invert_dictionary({1: True, 2: False}),
            {True: 1, False: 2},
        )
        self.assertEqual(
            invert_dictionary({2: True, 1: False}),
            {True: 2, False: 1},
        )

        self.assertEqual(
            invert_dictionary({'red': 'RED', 'blue': 'BLUE'}),
            {'BLUE': 'blue', 'RED': 'red'},
        )

        # Bad cases (Duplicates in values):
        self.assertRaises(
            NineMLRuntimeError,
            invert_dictionary, {'RED': 1, 'BLUE': 1},
        )

        self.assertRaises(
            NineMLRuntimeError,
            invert_dictionary, {True: None, False: None},
        )

        # Unhashable values:
        self.assertRaises(
            NineMLRuntimeError,
            invert_dictionary, {True: None, False: {1: None}},
        )
Ejemplo n.º 2
0
 def get_node_addr(self):
     """Get the namespace address of this component"""
     parent = self.get_parent_model()
     if not parent:
         return NamespaceAddress.create_root()
     else:
         contained_namespace = invert_dictionary(parent.subnodes)[self]
         return parent.get_node_addr().get_subns_addr(contained_namespace)