mailr6515 - /1.3/relax_io.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by edward on June 28, 2008 - 18:05:
Author: bugman
Date: Sat Jun 28 18:05:14 2008
New Revision: 6515

URL: http://svn.gna.org/viewcvs/relax?rev=6515&view=rev
Log:
Modifications to the DummyFileObject class to better act as a file object.


Modified:
    1.3/relax_io.py

Modified: 1.3/relax_io.py
URL: 
http://svn.gna.org/viewcvs/relax/1.3/relax_io.py?rev=6515&r1=6514&r2=6515&view=diff
==============================================================================
--- 1.3/relax_io.py (original)
+++ 1.3/relax_io.py Sat Jun 28 18:05:14 2008
@@ -444,15 +444,20 @@
 
 class DummyFileObject:
     def __init__(self):
-        """Initialise an object for adding the string from all write calls 
to."""
-
+        """Set up the dummy object to act as a file object."""
+
+        # Initialise an object for adding the string from all write calls to.
         self.data = ''
 
+        # Set the closed flag.
+        self.closed = False
+
 
     def close(self):
-        """A method for deleting the contents of this object."""
-
-        del self.data
+        """A method for 'closing' this object."""
+
+        # Set the closed flag.
+        self.closed = True
 
 
     def write(self, str):
@@ -462,19 +467,33 @@
         @type str:      str
         """
 
+        # Check if the file is closed.
+        if self.closed:
+            raise ValueError, 'I/O operation on closed file'
+
         # Append the string to the data object.
         self.data = self.data + str
 
 
     def readlines(self):
         """Mimic the file object readlines() method.
+        
+        This method works even if this dummy file object is closed!
+
 
         @return:    The contents of the file object separated by newline 
characters.
         @rtype:     list of str
         """
 
-        # Return the split up string.
-        return split(self.data, '\n')
+        # Split up the string.
+        lines = split(self.data, '\n')
+
+        # Loop over the lines, re-adding the newline character to match the 
file object readlines() method.
+        for i in xrange(len(lines)):
+            lines[i] = lines[i] + '\n'
+
+        # Return the file lines.
+        return lines
 
 
 




Related Messages


Powered by MHonArc, Updated Sat Jun 28 18:20:13 2008