Dynamic entries in a settings module
<p>I'm writing a package that imports audio files, processes them, plots them etc., for research purposes.
At each stage of the pipeline, settings are pulled from a settings module as shown below. </p>

<p>I want to be able to update a global setting like <code>MODEL_NAME</code> and have it update in any dicts containing it too.</p>

<h2>settings.py</h2>

<pre><code>MODEL_NAME = 'Test1'
DAT_DIR = 'dir1/dir2/'
PROCESSING = {
    "key1":{
        "subkey2":0,
        "subkey3":1
    },
    "key2":{
        "subkey3":MODEL_NAME
    }
}
</code></pre>

<h2>run.py</h2>

<pre><code>import settings as s
wavs = import_wavs(s.DAT_DIR)
proc_wavs = proc_wavs(wavs,s.PROCESSING)
</code></pre>

<p>Some of the settings dicts I would like to contain <code>MODEL_NAME</code>, which works fine. The problem arises when I want to change <code>MODEL_NAME</code> during runtime. So if I do:</p>

<pre><code>import settings as s
wavs = import_wavs(s.DAT_DIR)
s.MODEL_NAME='test1'
proc_wavs1 = proc_wavs(wavs,s.PROCESSING)
s.MODEL_NAME='test2'
proc_wavs2 = proc_wavs(wavs,s.PROCESSING)
</code></pre>

<p>But obviously both the calls so <code>s.PROCESSING</code> will contain the <code>MODEL_NAME</code> originally assigned in the settings file. 
What is the best way to have it update?</p>

<p>Possible solutions I've thought of:</p>

<ul>
<li><p>Store the variables as a mutable type, then update it e.g.:</p>

<pre><code>s.MODEL_NAME[0] = ["test1"]
# do processing things
s.MODEL_NAME[0] = ["test2"]
</code></pre></li>
<li><p>Define each setting category as a function instead, so it is rerun on
each call e.g.: </p>

<pre><code>MODEL_NAME = 'test1' ..

def PROCESSING():
   return {
       "key1":{
           "subkey2":0,
           "subkey3":1
       },
       "key2":{
           "subkey3":MODEL_NAME
       }
   }
</code></pre>

<p>Then</p>

<pre><code>s.MODEL_NAME='test1'
proc_wavs1 = proc_wavs(wavs,s.PROCESSING())
s.MODEL_NAME='test2'
proc_wavs1 = proc_wavs(wavs,s.PROCESSING())
</code></pre>

<p>I thought this would work great, but then it's very difficult to
change any entries of the functions during runtime eg if I wanted to
update the value of subkey2 and run something else.</p></li>
</ul>

<p>Other thoughts maybe a class with an update method or something, does anyone have any better ideas?</p>

<p>You want to configure generic and specific settings structured in dictionaries for functions that perform waves analysis.</p>

<p>Start by defining a settings class, like :</p>

<pre><code>class Settings :

  data_directory = 'path/to/waves'

  def __init__(self, model):         
     self.parameters= {
       "key1":{
           "subkey1":0,
           "subkey2":0
       },
       "key2":{
           "subkey1":model
       }
     }

# create a new class based on model1
s1 = Settings('model1')
# attribute values to specific keys
s1.parameters["key1"]["subkey1"] = 3.1415926
s1.parameters["key1"]["subkey2"] = 42
# an other based on model2
s2 = Settings('model2')
s2.parameters["key1"]["subkey1"] = 360
s2.parameters["key1"]["subkey2"] = 1,618033989
# load the audio
wavs = openWaves(Settings.data_directory)
# process with the given parameters
results1 = processWaves(wavs,s1)
results2 = processWaves(wavs,s2)
</code></pre>

python dictionary configuration python-module