Author: macraild Date: Thu Mar 15 17:58:23 2007 New Revision: 3200 URL: http://svn.gna.org/viewcvs/relax?rev=3200&view=rev Log: Alternative implimentation of the data object as a singleton. As proposed here: https://mail.gna.org/public/relax-devel/2007-03/msg00013.html and agreed here: https://mail.gna.org/public/relax-devel/2007-03/msg00058.html# Modified: 1.3/data/__init__.py 1.3/test_suite/unit_tests/data/test___init__.py Modified: 1.3/data/__init__.py URL: http://svn.gna.org/viewcvs/relax/1.3/data/__init__.py?rev=3200&r1=3199&r2=3200&view=diff ============================================================================== --- 1.3/data/__init__.py (original) +++ 1.3/data/__init__.py Thu Mar 15 17:58:23 2007 @@ -37,28 +37,8 @@ class Data(dict): """The relax data storage object.""" - # Singleton initialisation, the reference to the single instance of this class. - __instance = None - # The current data pipe. current_pipe = None - - - def __new__(self, *args, **kargs): - """Method for implementing the singleton design pattern. - - If no other class instance currently exists, create a new instance of this class. Otherwise - return the class instance. See http://en.wikipedia.org/wiki/Singleton_pattern for a - description of this design pattern. - """ - - # Create a new instance if none exists. - if self.__instance is None: - self.__instance = dict.__new__(self, *args, **kargs) - - # Return the class instance. - return self.__instance - def __repr__(self): """The string representation of the object. @@ -145,3 +125,6 @@ # Change the current data pipe. self.current_pipe = pipe_name +# Rebind the name Data with an instance to prevent accidental creation +# of multiple instances of the Data class +Data = Data() Modified: 1.3/test_suite/unit_tests/data/test___init__.py URL: http://svn.gna.org/viewcvs/relax/1.3/test_suite/unit_tests/data/test___init__.py?rev=3200&r1=3199&r2=3200&view=diff ============================================================================== --- 1.3/test_suite/unit_tests/data/test___init__.py (original) +++ 1.3/test_suite/unit_tests/data/test___init__.py Thu Mar 15 17:58:23 2007 @@ -27,9 +27,12 @@ from data import Data -class NewStore(Data): - """Subclass the relax data storage object for the isolation and creation of a new singleton.""" - +class NewStore(dict): + """Dict subclass to act as proxy for the Singleton Data object.""" + def __getattr__(self, attr): + """Delegate to the Data class to get methods for testing""" + return getattr(Data.__class__, attr) + class Empty_container: """An empty data container.""" @@ -53,15 +56,11 @@ # Add an object to the data store object. self.data_store.test = 1 - # Create a new reference. - self.new_ref = NewStore() - def tearDown(self): """Destroy the subclassed data store.""" # Delete all references (which should decrement the singleton's ref counter to 0, hence destroying it). - del self.new_ref del self.data_store @@ -104,18 +103,3 @@ self.assert_(hasattr(self.data_store, 'current_pipe')) - def test_singleton(self): - """Test that the relax data storage object is functioning as a singleton.""" - - # Test that the new reference to NewStore is the singleton instance reference. - self.assertEqual(self.data_store, self.new_ref) - - # Delete all references (which should decrement the singleton's ref counter to 0, hence destroying it). - del self.new_ref - del self.data_store - - # Create a new singleton. - new = NewStore() - - # Test that the object 'test' from the original singleton does not exist. - self.assert_(not hasattr(new, 'test'))