Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My idea, is really simple, <strong>use optional keyword arguments</strong>. Here is a little example, what I'm talking about:</p> <pre><code># collect, the constants to a dictionary, # with the proper key names d = dict(a=1, b=2, c=3) # create functions, where you want to use the constants # and use the same key-names as in dictionary, and also # add '**kwargs' at the end of the attribute list def func1(a, b, d=4, **kwargs): print a, b, d def func2(c, f=5, **kwargs): print c, f # now any time, you use the original dictionary # as an argument for one of these functions, this # will let the function select only those keywords # that are used later func1(**d) # 1 2 4 func2(**d) # 3 5 </code></pre> <p>This idea allow you to modify the list of constants only at one place at the time, in you original dictionary. So this is ported back to your idea:</p> <p>This is your <code>configuration.py</code>:</p> <pre><code># Your parsing, reading and storing functions goes here.. # Now create your dictionary constants = dict( host = DB_HOST, pnum = DB_PORT_NUMBER, num = DB_NUMBER, serv = IN_SERVICE, data = IN_DATA ) </code></pre> <p>Here is your <code>other_file.py</code>:</p> <pre><code>import configuration as cf def main(host, pnum, num, serv, data, **kwargs): intermediate_function( host, pnum, num, 'other', 'params', 'here' ) def intermediate_function(host, pnum, num, *args): pass # Now, you can call this function, with your # dictionary as keyword arguments if __name__ == '__main__': main(**cf.constants) </code></pre> <p><strong>Although this is working, I do not recommend this solution!</strong> Your code is going to be harder to maintain, since every time you call one of those functions, where you pass your dictionary of constants, you will only see "one" argument: the dictionary itself, which is not so verbose. So I belive, you should think about a greater architecture of your code, where you use more <strong>deterministic functions</strong> (returning "real" values), and use them chained to each other, so you don't have to pass all those constants all the time. But this is my opinion:)</p> <p><em><strong>EDIT:</em></strong></p> <p>If my solution, mentioned above is suits you, I also have a better idea on how to store and parse your configuration file, and turn it automatically into a dictionary. Use JSON instead of simple <code>.cfg</code> file:</p> <p>This will be your <code>conf.json</code>:</p> <pre><code>{ "host" : "DB_HOST", "pnum" : "DB_PORT_NUMBER", "num" : "DB_NUMBER", "serv" : "IN_SERVICE", "data" : "IN_DATA" } </code></pre> <p>And your <code>configuration.py</code> will look like this:</p> <pre><code>import json with open('conf.json') as conf: # JSON parser will convert it to dictionary constants = json.load(conf) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload