Example #1
0
    def test_custom_kw_config_data_is_null(self):
        data_1 = {"VALUE_1": 123453.3, "VALUE_2": 0.234234}

        data_2 = {"VALUE_1": 965689, "VALUE_3": 1.1222}

        with TestAreaContext(
                "python/enkf/data/custom_kw_null_element") as test_area:

            self.createResultFile("result_file_1", data_1)
            self.createResultFile("result_file_2", data_2)

            custom_kw_config = CustomKWConfig("CUSTOM_KW", "result_file")

            custom_kw_1 = CustomKW(custom_kw_config)
            custom_kw_1.fload("result_file_1")

            custom_kw_2 = CustomKW(custom_kw_config)
            custom_kw_2.fload("result_file_2")

            index_1 = custom_kw_config.indexOfKey("VALUE_1")
            index_2 = custom_kw_config.indexOfKey("VALUE_2")

            self.assertEqual(custom_kw_1["VALUE_1"], data_1["VALUE_1"])
            self.assertEqual(custom_kw_2["VALUE_1"], data_2["VALUE_1"])

            self.assertIsNone(custom_kw_2["VALUE_2"])
            self.assertFalse("VALUE_3" in custom_kw_config)
Example #2
0
    def test_custom_kw_creation(self):
        data = {
            "VALUE_1": 2345.234,
            "VALUE_2": 0.001234,
            "VALUE_3": "string_1",
            "VALUE_4": "string_2"
        }

        with TestAreaContext(
                "python/enkf/data/custom_kw_creation") as test_area:

            self.createResultFile("result_file", data)

            custom_kw_config = CustomKWConfig("CUSTOM_KW", "result_file")

            self.assertEqual(len(custom_kw_config), 0)

            custom_kw = CustomKW(custom_kw_config)

            custom_kw.fload("result_file")

            self.assertEqual(len(custom_kw_config), 4)

            for key in data:
                index = custom_kw_config.indexOfKey(key)
                self.assertEqual(data[key], custom_kw[key])

            with self.assertRaises(KeyError):
                value = custom_kw["VALUE_5"]
Example #3
0
    def createCustomKWConfig(self, name, data):
        with TestAreaContext(
                "python/enkf/custom_kw_config_set_config") as test_area:
            self.createResultFile("result_file", data)

            config = CustomKWConfig(name, "")
            config.parseResultFile("result_file", StringList())

        return config
Example #4
0
    def addDefinedCustomKW(self, group_name, definition):
        """ @rtype: EnkfConfigNode """
        if not group_name in self:
            type_hash = CustomKWConfig.convertDefinition(definition)
            self._add_defined_custom_kw(group_name, type_hash)

        return self[group_name]
Example #5
0
    def test_fwrite_and_fread(self):
        with TestAreaContext(
                "python/enkf/custom_kw_config_set_fwrite") as test_area:
            trees_config = self.createCustomKWConfig("TREES", {
                "OAK": 0.1,
                "SPRUCE": 5,
                "FIR": "pines",
                "PALM": "coconut"
            })
            insects_config = self.createCustomKWConfig("INSECTS", {
                "MOSQUITO": "annoying",
                "FLY": 3.14,
                "BEETLE": 0.5
            })

            config_set = CustomKWConfigSet()
            config_set.addConfig(trees_config)
            config_set.addConfig(insects_config)

            self.assertItemsEqual(config_set.getStoredConfigKeys(),
                                  ["TREES", "INSECTS"])

            config_set.fwrite("config_set")

            self.assertTrue(os.path.exists("config_set"))

            config_set = CustomKWConfigSet("config_set")

            self.assertItemsEqual(config_set.getStoredConfigKeys(),
                                  ["TREES", "INSECTS"])

            trees_config_from_file = CustomKWConfig("TREES", None)
            config_set.updateConfig(trees_config_from_file)

            for key in ["OAK", "SPRUCE", "FIR", "PALM"]:
                self.assertEqual(trees_config_from_file.indexOfKey(key),
                                 trees_config.indexOfKey(key))
                self.assertTrue(
                    trees_config_from_file.keyIsDouble(key) ==
                    trees_config.keyIsDouble(key))

            insects_config_from_file = CustomKWConfig("INSECTS", None)
            config_set.updateConfig(insects_config_from_file)

            for key in ["MOSQUITO", "FLY", "BEETLE"]:
                self.assertEqual(insects_config_from_file.indexOfKey(key),
                                 insects_config.indexOfKey(key))
                self.assertTrue(
                    insects_config_from_file.keyIsDouble(key) ==
                    insects_config.keyIsDouble(key))
Example #6
0
    def test_custom_kw_set_values(self):
        definition = {"STRING": str, "FLOAT": float, "INT": float}

        ckwc = CustomKWConfig("Test", None, definition=definition)

        ckw = CustomKW(ckwc)
        with self.assertRaises(KeyError):
            ckw["ANOTHER_STRING"] = "another string"

        ckw["STRING"] = "string"
        ckw["FLOAT"] = 3.1415
        ckw["INT"] = 1

        self.assertEqual(ckw["STRING"], "string")
        self.assertEqual(ckw["FLOAT"], 3.1415)
        self.assertEqual(ckw["INT"], 1)
Example #7
0
 def getCustomKeywordModelConfig(self):
     """ @rtype: CustomKWConfig """
     return CustomKWConfig.createCReference(self._get_ref(), parent=self)