Author: dmeyer
Date: Tue Jan 23 13:17:57 2007
New Revision: 2438

Modified:
   trunk/base/src/utils.py

Log:
add Singleton class

Modified: trunk/base/src/utils.py
==============================================================================
--- trunk/base/src/utils.py     (original)
+++ trunk/base/src/utils.py     Tue Jan 23 13:17:57 2007
@@ -183,3 +183,32 @@
     run.write(str(os.getpid()) + '\n')
     run.write(cmdline)
     run.close()
+
+
+class Singleton(object):
+    """
+    Create Singleton object from classref on demand.
+    """
+
+    class Memberfunction(object):
+        def __init__(self, singleton, name):
+            self._singleton = singleton
+            self._name = name
+
+        def __call__(self, *args, **kwargs):
+            return getattr(self._singleton(), self._name)(*args, **kwargs)
+        
+
+    def __init__(self, classref):
+        self._singleton = None
+        self._class = classref
+
+    def __call__(self):
+        if self._singleton is None:
+            self._singleton = self._class()
+        return self._singleton
+
+    def __getattr__(self, attr):
+        if self._singleton is None:
+            return Singleton.Memberfunction(self, attr)
+        return getattr(self, _singleton, attr)

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to