Help with Singleton SafeConfigParser

Discussion in 'Python' started by Josh English, Dec 8, 2012.

  1. Josh English

    Josh English Guest

    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
    Josh English, Dec 8, 2012
    #1
    1. Advertising

  2. Josh English

    Peter Otten Guest

    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.
    Peter Otten, Dec 8, 2012
    #2
    1. Advertising

  3. Josh English

    Josh English Guest

    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
    Josh English, Dec 8, 2012
    #3
  4. Josh English

    Josh English Guest

    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
    Josh English, Dec 8, 2012
    #4
  5. 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.
    Mark Lawrence, Dec 8, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,212
    Proton Projects - Moin
    Mar 27, 2007
  2. Hamish Moffatt

    SafeConfigParser can set unsafe values

    Hamish Moffatt, Jul 10, 2007, in forum: Python
    Replies:
    7
    Views:
    478
    Hamish Moffatt
    Jul 11, 2007
  3. Márcio Faustino

    Using percent signs with SafeConfigParser

    Márcio Faustino, Apr 11, 2009, in forum: Python
    Replies:
    5
    Views:
    424
    Peter Otten
    Apr 12, 2009
  4. Wilhelm
    Replies:
    1
    Views:
    153
  5. Trans
    Replies:
    12
    Views:
    265
    Robert Klemme
    Sep 14, 2007
Loading...

Share This Page