singleton.py :  » UML » Python-UML-Tool » pyut-1.4.0 » src » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » UML » Python UML Tool 
Python UML Tool » pyut 1.4.0 » src » singleton.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

__version__ = "$Revision: 1.3 $"
__author__ = "EI5, eivd, Group Burgbacher - Waelti"
__date__ = "2002-02-21"

from types import MethodType

class Singleton(object):
    """
    Base class for singleton classes.

    Any class derived from this one is a singleton. You can call its
    constructor multiple times, you'll get only one instance.

    Note that `__init__` must not be defined. Use `init` instead.
    This is because `__init__` will always be called, thus reinitializing the
    state of your singleton each time you try to instanciate it.
    On the contrary, `init` will be called just one time.

    To be sure that the `__init__` method won't be inadvertantly defined,
    the singleton will check for it at first instanciation and raise an
    `AssertionError` if it finds it.

    Example::

        # This class is OK
        class A(Singleton):
            def init(self, val):
                self.val = val

        # This class will raise AssertionError at first instanciation, because
        # of the __init__ method.
        class B(Singleton):
            def __init__(self, val):
                self.val = val

    :author: Laurent Burgbacher
    :contact: <lb@alawa.ch>
    :version: $Revision: 1.3 $
    """
    def __new__(cls, *args, **kwds):
        """
        New operator of a singleton class.
        Will return the only instance, or create it if needed.

        @since 1.0
        @author Laurent Burgbacher <lb@alawa.ch>
        """
        instance = cls.__dict__.get("__instance__")
        if instance is None:
            instance = object.__new__(cls)
            assert type(instance.__init__) != MethodType, \
                "Error, your singleton class %s cannot contain a __init__ " \
                "method." % cls
            try:
                instance.init(*args, **kwds)
            except:
                raise
            cls.__instance__ = instance
        return instance

    def init(self, *args, **kwds):
        """
        Constructor of a singleton class.

        @since 1.0
        @author Laurent Burgbacher <lb@alawa.ch>
        """
        pass

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.