Saving configuration details

I have a bunch of strings and integer constants that I use in various places in my application. I plan on putting them in a centralized location so that it is easier to change them in the future. I could think of the following approaches:

1) Have them as separate variables stored in the model

settings_title = "My Amazing App"
settings_ver = 2.0
settings_desc = "Moar and Moar cats"


2) Have them as dict, saved in

settings = { "title": "My Amazing App",
    "ver" = 2.0,
    "desc" = "Moar and Moar cats"


  • Can the model be used

    ? I have heard that it is being evaluated for every request. Could setting parameters there have a noticeable overhead?
  • Is there a difference between the two performance approaches?
  • Is there a better way to do this?

source to share

3 answers

Your model file will be executed on every request anyway, so adding multiple setting assignments to your code will result in negligible overhead. Instead of setting parameters in, you can create a separate model file for better organization. Note that model files are executed in alphabetical order, so if settings need to be available in subsequent model files, specify a settings file something like to make sure it runs before any other model files.

If you prefer, you can instead put the settings in a module (e.g. in your application / modules folder and import the settings object into your application code wherever you need it (in this case, the module will only be loaded once by the interpreter). If any of the settings need to be set dynamically based on the incoming request, you are probably better off not storing the settings in the model file.

Finally, instead of a standard dictionary, you can use a web2py object Storage

, which is like a dictionary but allows you to access values ​​as attributes and returns None rather than KeyError if you are trying to access a key / attribute that does not exist:

from import Storage
settings = Storage()
settings.title = 'My Amazing App'



settings = Storage({'title': 'My Amazing App'})


Note: the objects are web2py request

, response

and session

are all instances of the class Storage




You can directly put your config variable in a file .py

and use that file by importing in your module as used by django

. If you want to concatenate a variable on some basic sections, you can use ConfigParser

that can prepare a file .cfg




Django, for example, uses a file.

It's not a model, but just a bunch of variables of all types, strings / ints / dicts / whatever and you import settings

or from settings import *

every module that needs to access them.

Since it is not one model, there are no additional access costs.



All Articles