--- title: Config keywords: fastai sidebar: home_sidebar summary: "A configurator module." description: "A configurator module." nb_path: "nbs/utils/config.ipynb" ---
{% raw %}
{% endraw %}

Open In Colab

{% raw %}
{% endraw %} {% raw %}

class Configurator[source]

Configurator(config_file, default_section='default')

A configurator class. This class can read arguments from ini-style configuration file and parse arguments from command line simultaneously. This class can also convert the argument value from str to int, float, bool, list and None automatically. The priority of arguments from command line is higher than that from configuration file. That is, if there are same argument name in configuration file and command line, the value in the former will be overwritten by that in the latter. Moreover:

  • Command line: The format of arguments is --arg_name=arg_value, there cannot be any space in the inner of an argument string. For example:: python main.py --model=Pop --num_thread=128 --group_view=[10,30,50,100]
  • Configuration file: This file must be ini-style. If there is only one section and whatever the name is, this class will read arguments from that section. If there are more than one sections, this class will read arguments from the section named default_section. After initialization successful, the objective of this class can be used as a dictionary:: config = Configurator("./NeuRec.properties") num_thread = config["num_thread"] group_view = config["group_view"] Here, the types of num_thread and group_view are int and list, respectively.
{% endraw %} {% raw %}
{% endraw %}

{% raw %}
import unittest


class TestConfigurator(unittest.TestCase):
    def setUp(self):
        self.conf_default = Configurator("config.properties", default_section="default")
        self.conf_mlp = Configurator("config.properties", default_section="mlp")

    def testRecommenderName(self):
        self.assertEqual(self.conf_default["recommender"], "config_MF")
    
    def testRecommenderNameType(self):
        self.assertEqual(str(type(self.conf_default["recommender"])), "<class 'str'>")

    def testIntegerType(self):
        self.assertEqual(str(type(self.conf_default["epochs"])), "<class 'int'>")

    def testListType(self):
        self.assertEqual(str(type(self.conf_mlp["layers"])), "<class 'list'>")
    
    def testUnwantedTypeConversion(self):
        self.assertEqual(str(type(self.conf_default["batch_size"])), "<class 'str'>")

    def testDefaultSectionValue(self):
        self.assertEqual(self.conf_default["epochs"], 300)

    def testCustomSectionValue(self):
        self.assertEqual(self.conf_mlp["epochs"], 100)
    
    def testListValue(self):
        self.assertEqual(self.conf_mlp["layers"], [32, 64, 1])


unittest.main(argv=[''], verbosity=2, exit=False)
testCustomSectionValue (__main__.TestConfigurator) ... ok
testDefaultSectionValue (__main__.TestConfigurator) ... ok
testIntegerType (__main__.TestConfigurator) ... ok
testListType (__main__.TestConfigurator) ... ok
testListValue (__main__.TestConfigurator) ... ok
testRecommenderName (__main__.TestConfigurator) ... ok
testRecommenderNameType (__main__.TestConfigurator) ... ok
testUnwantedTypeConversion (__main__.TestConfigurator) ... ok

----------------------------------------------------------------------
Ran 8 tests in 0.038s

OK
<unittest.main.TestProgram at 0x7f4771c904d0>
{% endraw %}

{% raw %}
Author: Sparsh A.

Last updated: 2021-12-05 09:19:00

Compiler    : GCC 7.5.0
OS          : Linux
Release     : 5.4.104+
Machine     : x86_64
Processor   : x86_64
CPU cores   : 2
Architecture: 64bit

sys    : 3.7.12 (default, Sep 10 2021, 00:21:48) 
[GCC 7.5.0]
IPython: 5.5.0

{% endraw %}