Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help with Singleton SafeConfigParser

Reply
Thread Tools

Help with Singleton SafeConfigParser

 
 
Josh English
Guest
Posts: n/a
 
      12-08-2012
I am trying to create a Singleton SafeConfigParser object to use across all the various scripts in this application.

I tried a Singleton pattern found on the web:

<pre>
class Singleton(object):
def __new__(cls):
if not hasattr(cls, '_inst'):
print "Creating Singleton Object"
cls._inst = super(Singleton, cls).__new__(cls)
else:
print "Returning established Singleton"

return cls._inst

import ConfigParser

class Options(ConfigParser.SafeConfigParser, Singleton):
def __init__(self):
print "Initialing Options"
ConfigParser.SafeConfigParser.__init__(self)
self.add_section('test')
</pre>

And this doesn't work because it calls the __init__ method every time I create the Options object:

<pre>

O = Options()
print O
O.set('test','start','True')
print "from O", O.options('test')


P = Options()
print P
print "from P", P.options('test')
</pre>

This results in:

<pre>
Creating Singleton Object
Initialing Options
<__main__.Options object at 0x02BF4C50>
from O ['start']
Returning established Singleton
Initialing Options
<__main__.Options object at 0x02BF4C50>
from P []
</pre>


I have seen older posts in this group that talk about using modules as singletons, but this, unless I misunderstand, requires me to code the entire API for SafeConfigParser in the module:

<pre>
import ConfigParser


class Options(ConfigParser.SafeConfigParser):
def __init__(self):
ConfigParser.SafeConfigParser.__init__(self)
self.readfp(open('defaults.cfg'))
self.read(['local.txt', 'local.cfg'])

def save(self):
with open('local.txt','w') as f:
self.write(f)

__options = Options()

def set(section, name, value):
return self.__options.set(section, name, value)

def options(section):
return self.__options.options

# And so on
</pre>

This seems incredibly wasteful, and to introspect my options I get a module, not a SafeConfigParser object, so I'm wondering if there is a different way to handle this?

Josh
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-08-2012
Josh English wrote:

> I have seen older posts in this group that talk about using modules as

singletons, but this, unless I misunderstand, requires me to code the entire
API for SafeConfigParser in the module:
>
> <pre>
> import ConfigParser
>
>
> class Options(ConfigParser.SafeConfigParser):
> def __init__(self):
> ConfigParser.SafeConfigParser.__init__(self)
> self.readfp(open('defaults.cfg'))
> self.read(['local.txt', 'local.cfg'])
>
> def save(self):
> with open('local.txt','w') as f:
> self.write(f)
>
> __options = Options()
>
> def set(section, name, value):
> return self.__options.set(section, name, value)
>
> def options(section):
> return self.__options.options
>
> # And so on
> </pre>
>
> This seems incredibly wasteful, and to introspect my options I get a

module, not a SafeConfigParser object, so I'm wondering if there is a
different way to handle this?

Two underscores trigger name mangling only in a class, not in a module.
Don't try to hide the Options instance:

# module config.py
import ConfigParser

class Options(ConfigParser.SafeConfigParser):
... # as above

options = Options()

Then use it elsewhere:

from config import options

options.set("mysection", "myoption", "myvalue")

All but the first import will find the module in the cache (sys.modules) and
therefore the same Options instance will be used. VoilĆ* your no-nonsense
singleton.

 
Reply With Quote
 
 
 
 
Josh English
Guest
Posts: n/a
 
      12-08-2012
On Saturday, December 8, 2012 9:40:07 AM UTC-8, Peter Otten wrote:
>
>
>
> Two underscores trigger name mangling only in a class, not in a module.
>
> Don't try to hide the Options instance:
>
>
>
> # module config.py
>
> import ConfigParser
>
>
>
> class Options(ConfigParser.SafeConfigParser):
>
> ... # as above
>
>
>
> options = Options()
>
>
>
> Then use it elsewhere:
>
> from config import options
>
> options.set("mysection", "myoption", "myvalue")
>
>
>
> All but the first import will find the module in the cache (sys.modules) and
>
> therefore the same Options instance will be used. Voilą your no-nonsense
>
> singleton.



Ah. I was over-thinking again. I couldn't find an example of this anywhere,and when I saw the tirades against Singletons they mentioned "use modules"but, well, I haven't had my morning coffee yet. I shouldn't even be tryingthis sort of thing until then.

Thank you for the simple answer.

Josh
 
Reply With Quote
 
Josh English
Guest
Posts: n/a
 
      12-08-2012
On Saturday, December 8, 2012 9:40:07 AM UTC-8, Peter Otten wrote:
>
>
>
> Two underscores trigger name mangling only in a class, not in a module.
>
> Don't try to hide the Options instance:
>
>
>
> # module config.py
>
> import ConfigParser
>
>
>
> class Options(ConfigParser.SafeConfigParser):
>
> ... # as above
>
>
>
> options = Options()
>
>
>
> Then use it elsewhere:
>
> from config import options
>
> options.set("mysection", "myoption", "myvalue")
>
>
>
> All but the first import will find the module in the cache (sys.modules) and
>
> therefore the same Options instance will be used. Voilą your no-nonsense
>
> singleton.



Ah. I was over-thinking again. I couldn't find an example of this anywhere,and when I saw the tirades against Singletons they mentioned "use modules"but, well, I haven't had my morning coffee yet. I shouldn't even be tryingthis sort of thing until then.

Thank you for the simple answer.

Josh
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      12-08-2012
On 08/12/2012 17:48, Josh English wrote:
> On Saturday, December 8, 2012 9:40:07 AM UTC-8, Peter Otten wrote:
>>
>> Two underscores trigger name mangling only in a class, not in a module.
>>
>> Don't try to hide the Options instance:
>>
>> # module config.py
>>
>> import ConfigParser
>>
>> class Options(ConfigParser.SafeConfigParser):
>>
>> ... # as above
>>
>> options = Options()
>>
>> Then use it elsewhere:
>>
>> from config import options
>>
>> options.set("mysection", "myoption", "myvalue")
>>
>> All but the first import will find the module in the cache (sys.modules) and
>>
>> therefore the same Options instance will be used. Voilą your no-nonsense
>>
>> singleton.

>
>
> Ah. I was over-thinking again. I couldn't find an example of this anywhere, and when I saw the tirades against Singletons they mentioned "use modules" but, well, I haven't had my morning coffee yet. I shouldn't even be trying this sort of thing until then.
>
> Thank you for the simple answer.
>
> Josh
>


For the benefit of the OP and others, if you want to gain more knowledge
about patterns in Python such as the Singleton, I suggest you use your
favourite search engine to find "Alex Martelli Python patterns".

--
Cheers.

Mark Lawrence.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using percent signs with SafeConfigParser Mįrcio Faustino Python 5 04-12-2009 07:57 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
SafeConfigParser can set unsafe values Hamish Moffatt Python 7 07-11-2007 10:59 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM



Advertisments