def test_build_custom_class_default_constructable_with_name_and_arguments( self, _, __, ___): module_custom = importlib.import_module( "test_base.test_classes_for_factory.custom_class_default_constructable" ) module_simple = importlib.import_module( "test_base.test_classes_for_factory.simple_class_default_constructable" ) self._config["CustomClassDefaultConstructable"] = { "simple_class": "test (simple_class_default_constructable)" } self._config["Test"] = {"some_integer": "-3"} created = factory.build_from_config( self._config, "CustomClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module_custom.CustomClassDefaultConstructable) self.assertEqual(created.__class__.__name__, "CustomClassDefaultConstructable") self.assertIsInstance(created.simple_class, module_simple.SimpleClassDefaultConstructable) self.assertEqual(created.simple_class.__class__.__name__, "Test (SimpleClassDefaultConstructable)") self.assertEqual(created.simple_class.some_integer, -3) self.assertEqual( created.simple_class.some_float, module_simple. SimpleClassDefaultConstructable.default_values["some_float"]) self.assertEqual( created.simple_class.some_bool, module_simple. SimpleClassDefaultConstructable.default_values["some_bool"]) self.assertEqual( created.simple_class.some_string, module_simple. SimpleClassDefaultConstructable.default_values["some_string"]) # Tabs and several spaces are also allowed self._config["CustomClassDefaultConstructable"] = { "simple_class": "test \t (simple_class_default_constructable)" } self._config["Test"] = {"some_string": "-3"} created = factory.build_from_config( self._config, "CustomClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module_custom.CustomClassDefaultConstructable) self.assertEqual(created.__class__.__name__, "CustomClassDefaultConstructable") self.assertIsInstance(created.simple_class, module_simple.SimpleClassDefaultConstructable) self.assertEqual(created.simple_class.__class__.__name__, "Test (SimpleClassDefaultConstructable)") self.assertEqual( created.simple_class.some_integer, module_simple. SimpleClassDefaultConstructable.default_values["some_integer"]) self.assertEqual( created.simple_class.some_float, module_simple. SimpleClassDefaultConstructable.default_values["some_float"]) self.assertEqual( created.simple_class.some_bool, module_simple. SimpleClassDefaultConstructable.default_values["some_bool"]) self.assertEqual(created.simple_class.some_string, "-3")
def test_build_simple_class_not_default_constructable_missing_arguments_raises_error( self, _, __, ___): self._config["SimpleClassNotDefaultConstructable"] = { "some_integer": "0", "some_float": "31.231", "some_bool": "No" } with self.assertRaises(ConfigurationError): factory.build_from_config(self._config, "SimpleClassNotDefaultConstructable", "test_base.test_classes_for_factory")
def test_not_allowed_boolean_string_raises_error(self, _, __, ___): self._config["SimpleClassDefaultConstructable"] = {"some_bool": "of"} with self.assertRaises(TypeError): factory.build_from_config(self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory") self._config["SimpleClassDefaultConstructable"] = {"some_bool": "Tru"} with self.assertRaises(TypeError): factory.build_from_config(self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory")
def main() -> None: """ Use the command line arguments to run the JeLLyFysh application. First the command line arguments are parsed, and then the logging is set up. Afterwards, the configuration file specified in the command line is parsed. Based on the configuration file, the setting package is initialized and the mediator is constructed by the JeLLyFysh factory. The run method of the mediator is executed until an EndOfRun exception is raised. This invokes the post_run method of the mediator and ends the run of the application. """ print_start_message() args = parse_options(sys.argv[1:]) logger = set_up_logging(args) logger.info("Run identification hash: {0}".format(get_uuid())) logger.info( "Underlying platform (determined via platform.platform(aliased=True): {0}" .format(platform.platform(aliased=True))) logger.info( "Setting up the run based on the configuration file {0}.".format( args.config_file)) config = read_config(args.config_file) factory.build_from_config(config, to_camel_case(config.get("Run", "setting")), "jellyfysh.setting") mediator = factory.build_from_config( config, to_camel_case(config.get("Run", "mediator")), "jellyfysh.mediator") used_sections = factory.used_sections for section in config.sections(): if section not in used_sections and section != "Run": logger.warning( "The section {0} in the .ini file has not been used!".format( section)) logger.info("Running the event-chain Monte Carlo simulation.") start_time = time.time() try: mediator.run() except EndOfRun: logger.info("EndOfRun exception has been raised.") end_time = time.time() logger.info("Running the post_run method.") mediator.post_run() logger.info("Runtime of the simulation: --- %s seconds ---" % (end_time - start_time))
def test_build_custom_class_with_abstract_class(self, _, __, ___): module_custom = importlib.import_module( "test_base.test_classes_for_factory.custom_class_with_abstract_class" ) module_simple = importlib.import_module( "test_base.test_classes_for_factory.simple_abstract_class" ".simple_inheriting_class") self._config["CustomClassWithAbstractClass"] = { "abstract_class": "simple_inheriting_class" } self._config["SimpleInheritingClass"] = { "some_string": "some_string", "some_integer": "13" } created = factory.build_from_config( self._config, "CustomClassWithAbstractClass", "test_base.test_classes_for_factory") self.assertIsInstance(created, module_custom.CustomClassWithAbstractClass) self.assertEqual(created.__class__.__name__, "CustomClassWithAbstractClass") self.assertIsInstance(created.abstract_class, module_simple.SimpleInheritingClass) self.assertEqual(created.abstract_class.some_string, "some_string") self.assertEqual(created.abstract_class.some_integer, 13) self.assertEqual(created.abstract_class.__class__.__name__, "SimpleInheritingClass")
def test_build_custom_class_default_constructable_with_name( self, _, __, ___): module_custom = importlib.import_module( "test_base.test_classes_for_factory.custom_class_default_constructable" ) module_simple = importlib.import_module( "test_base.test_classes_for_factory.simple_class_default_constructable" ) # One can set the name of the simple class within the class self._config["CustomClassDefaultConstructable"] = { "simple_class": "test (simple_class_default_constructable)" } created = factory.build_from_config( self._config, "CustomClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module_custom.CustomClassDefaultConstructable) self.assertEqual(created.__class__.__name__, "CustomClassDefaultConstructable") self.assertIsInstance(created.simple_class, module_simple.SimpleClassDefaultConstructable) self.assertEqual(created.simple_class.__class__.__name__, "Test (SimpleClassDefaultConstructable)") self.assertEqual( created.simple_class.some_integer, module_simple. SimpleClassDefaultConstructable.default_values["some_integer"]) self.assertEqual( created.simple_class.some_float, module_simple. SimpleClassDefaultConstructable.default_values["some_float"]) self.assertEqual( created.simple_class.some_bool, module_simple. SimpleClassDefaultConstructable.default_values["some_bool"]) self.assertEqual( created.simple_class.some_string, module_simple. SimpleClassDefaultConstructable.default_values["some_string"])
def test_used_sections(self, _, __, ___): self._config["CustomClassWithAbstractClass"] = { "abstract_class": "simple_inheriting_class" } self._config["SimpleInheritingClass"] = { "some_string": "some_string", "some_integer": "13" } factory.build_from_config(self._config, "CustomClassWithAbstractClass", "test_base.test_classes_for_factory") factory.build_from_config(self._config, "SomeSectionName", "test_base.test_classes_for_factory", "SimpleClassDefaultConstructable") self.assertEqual(factory.used_sections, [ "CustomClassWithAbstractClass", "SimpleInheritingClass", "SomeSectionName" ])
def test_build_simple_class_in_package(self, _, __, ___): module = importlib.import_module( "test_base.test_classes_for_factory.simple_class_in_package" ".simple_class_in_package") created = factory.build_from_config( self._config, "SimpleClassInPackage", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.SimpleClassInPackage) self.assertEqual(created.__class__.__name__, "SimpleClassInPackage")
def test_build_simple_class_default_constructable_with_arguments( self, _, __, ___): module = importlib.import_module( "test_base.test_classes_for_factory.simple_class_default_constructable" ) self._config["SimpleClassDefaultConstructable"] = { "some_integer": "2", "some_float": "-1.2", "some_bool": "False", "some_string": "Test" } created = factory.build_from_config( self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.SimpleClassDefaultConstructable) self.assertEqual(created.__class__.__name__, "SimpleClassDefaultConstructable") self.assertEqual(created.some_integer, 2) self.assertEqual(created.some_float, -1.2) self.assertEqual(created.some_bool, False) self.assertEqual(created.some_string, "Test")
def test_build_simple_class_default_constructable(self, _, __, ___): module = importlib.import_module( "test_base.test_classes_for_factory.simple_class_default_constructable" ) # Since the class is default constructable, the section has not to be inside of the config file created = factory.build_from_config( self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.SimpleClassDefaultConstructable) self.assertEqual(created.__class__.__name__, "SimpleClassDefaultConstructable") self.assertEqual( created.some_integer, module.SimpleClassDefaultConstructable. default_values["some_integer"]) self.assertEqual( created.some_float, module.SimpleClassDefaultConstructable. default_values["some_float"]) self.assertEqual( created.some_bool, module.SimpleClassDefaultConstructable.default_values["some_bool"]) self.assertEqual( created.some_string, module.SimpleClassDefaultConstructable. default_values["some_string"])
def test_build_simple_class_default_constructable_with_name( self, _, __, ___): module = importlib.import_module( "test_base.test_classes_for_factory.simple_class_default_constructable" ) created = factory.build_from_config( self._config, "SomeSectionName", "test_base.test_classes_for_factory", "SimpleClassDefaultConstructable") self.assertIsInstance(created, module.SimpleClassDefaultConstructable) self.assertEqual(created.__class__.__name__, "SomeSectionName (SimpleClassDefaultConstructable)") self.assertEqual( created.some_integer, module.SimpleClassDefaultConstructable. default_values["some_integer"]) self.assertEqual( created.some_float, module.SimpleClassDefaultConstructable. default_values["some_float"]) self.assertEqual( created.some_bool, module.SimpleClassDefaultConstructable.default_values["some_bool"]) self.assertEqual( created.some_string, module.SimpleClassDefaultConstructable. default_values["some_string"])
def test_non_existing_package_raises_error(self, _, __, ___): with self.assertRaises(ImportError): factory.build_from_config(self._config, "SimpleClassDefaultConstructable", "wrong.package")
def test_build_class_with_list_of_simple_type(self, _, __, ___): module = importlib.import_module( "test_base.test_classes_for_factory.class_with_list_of_simple_type" ) self._config["ClassWithListOfSimpleType"] = {"some_list": "0, 1, 2"} created = factory.build_from_config( self._config, "ClassWithListOfSimpleType", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.ClassWithListOfSimpleType) self.assertEqual(created.__class__.__name__, "ClassWithListOfSimpleType") self.assertEqual(created.some_list, [0, 1, 2]) # Spaces in comma separated list are optional self._config["ClassWithListOfSimpleType"] = {"some_list": "0,1,2"} created = factory.build_from_config( self._config, "ClassWithListOfSimpleType", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.ClassWithListOfSimpleType) self.assertEqual(created.__class__.__name__, "ClassWithListOfSimpleType") self.assertEqual(created.some_list, [0, 1, 2]) # Several spaces are also allowed self._config["ClassWithListOfSimpleType"] = { "some_list": "0, 1, 2" } created = factory.build_from_config( self._config, "ClassWithListOfSimpleType", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.ClassWithListOfSimpleType) self.assertEqual(created.__class__.__name__, "ClassWithListOfSimpleType") self.assertEqual(created.some_list, [0, 1, 2]) # Tabulators are also fine self._config["ClassWithListOfSimpleType"] = {"some_list": "0,\t1,\t2"} created = factory.build_from_config( self._config, "ClassWithListOfSimpleType", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.ClassWithListOfSimpleType) self.assertEqual(created.__class__.__name__, "ClassWithListOfSimpleType") self.assertEqual(created.some_list, [0, 1, 2]) # Line Breaks are supported as well self._config["ClassWithListOfSimpleType"] = {"some_list": "0,\n1,\n2"} created = factory.build_from_config( self._config, "ClassWithListOfSimpleType", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.ClassWithListOfSimpleType) self.assertEqual(created.__class__.__name__, "ClassWithListOfSimpleType") self.assertEqual(created.some_list, [0, 1, 2]) # Some arbitrary mixture of all this is fine self._config["ClassWithListOfSimpleType"] = { "some_list": "0,\n\t 1, \t \n2" } created = factory.build_from_config( self._config, "ClassWithListOfSimpleType", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.ClassWithListOfSimpleType) self.assertEqual(created.__class__.__name__, "ClassWithListOfSimpleType") self.assertEqual(created.some_list, [0, 1, 2])
def test_allowed_boolean_strings(self, _, __, ___): module = importlib.import_module( "test_base.test_classes_for_factory.simple_class_default_constructable" ) self._config["SimpleClassDefaultConstructable"] = {"some_bool": "1"} created = factory.build_from_config( self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.SimpleClassDefaultConstructable) self.assertEqual(created.some_bool, True) self._config["SimpleClassDefaultConstructable"] = {"some_bool": "YeS"} created = factory.build_from_config( self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.SimpleClassDefaultConstructable) self.assertEqual(created.some_bool, True) self._config["SimpleClassDefaultConstructable"] = {"some_bool": "true"} created = factory.build_from_config( self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.SimpleClassDefaultConstructable) self.assertEqual(created.some_bool, True) self._config["SimpleClassDefaultConstructable"] = {"some_bool": "ON"} created = factory.build_from_config( self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.SimpleClassDefaultConstructable) self.assertEqual(created.some_bool, True) self._config["SimpleClassDefaultConstructable"] = {"some_bool": "0"} created = factory.build_from_config( self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.SimpleClassDefaultConstructable) self.assertEqual(created.some_bool, False) self._config["SimpleClassDefaultConstructable"] = {"some_bool": "nO"} created = factory.build_from_config( self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.SimpleClassDefaultConstructable) self.assertEqual(created.some_bool, False) self._config["SimpleClassDefaultConstructable"] = { "some_bool": "FALSE" } created = factory.build_from_config( self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.SimpleClassDefaultConstructable) self.assertEqual(created.some_bool, False) self._config["SimpleClassDefaultConstructable"] = {"some_bool": "off"} created = factory.build_from_config( self._config, "SimpleClassDefaultConstructable", "test_base.test_classes_for_factory") self.assertIsInstance(created, module.SimpleClassDefaultConstructable) self.assertEqual(created.some_bool, False)