iTayb
iTayb

Reputation: 12743

How to save configuration data in a file

I do have the module config.py which has vars in it that indicates configuration data.

For example:

logfile_enable = True
logfile_name = "debug.log"
logfile_maxsize = 100*1024 # 100KB
logfile_backupCount = 1

Then I can just import config in every project script, and access data through config.VAR (config.maxsize for example).

I do want to save the changes to the module once the application is closed. I thought about pickling the __dict__ of the module and somehow set it back on startup, but I seem to fail doing it, as you can't call __dict__ from the module itself.

I don't want to use a class and a dict in it. I think that the config.VAR syntax is much clearer in the script, and I don't want to initialize a new class object in every import.

What can I do?

Upvotes: 1

Views: 435

Answers (1)

Nick Craig-Wood
Nick Craig-Wood

Reputation: 54089

I've used the standard library ConfigParser module for this purpose, and then had a global config object, so in all your files you say from config import config where config is an instantiated ConfigParser object which you can read stuff from load from a file and save to a file.

Upvotes: 3

Related Questions