singleton objects with decorators

Uwe Mayer merkosh at hadiko.de
Tue Apr 12 06:31:30 EDT 2005


Tuesday 12 April 2005 10:01 am Steven Bethard wrote:
>> I am using a class to manage configuration settings in an application.
>> This object should only existe once so that when the user
>> changes a setting through a configuration dialog the change imminent in
>> all locations where access to config settings are needed.
 
> Ahh, I see.  I would typically just use a module in this situation,
> where the configuration settings were just names global to the module.
> Is there a benefit to using a singleton object over using just a module?

Basically I am using a module. The config file is stored in
$HOME/.<app>/<app>.conf where the user can go and edit it. It is a working
python program which globally declares variables.
I cannot simply import this module as it does not lie in the path and I am
not too fond of dynamically cluttering sys.path to my needs. My intend was
to make it look as little complicated as possible (for the user) while
retaining maximum flexibility.

Therefore I wrote a class, subclassing a dict, which executes the config
file and takes up the variables into its namespace. Afterwards I update the
dict with default-values (in case the user kicked some vars out) or
manipulate the read-in config variables, in case the format changed. 

When the application closes the Singleton is written back to the config file
in the user home by using the PrettyPrinter and substituting variables in a
rather longer descriptive text. That way the user always has a nicely
formatted, descriptive configuration file - I thought that was rather
straight forward. :/

Ciao
Uwe     



More information about the Python-list mailing list