mailr17701 - /trunk/relax_io.py


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

Header


Content

Posted by edward on October 05, 2012 - 18:42:
Author: bugman
Date: Fri Oct  5 18:42:15 2012
New Revision: 17701

URL: http://svn.gna.org/viewcvs/relax?rev=17701&view=rev
Log:
Created the relax_io.readlines method for handling the bz2 and gzip module 
byte strings in Python 3.

The bz2.BZ2File and gzip.GzipFile classes now return lists of bytes strings 
from their readlines()
methods in Python 3.  This causes failures all over relax.  This behaviour is 
probably a bug in
these classes.  The relax_io.readlines method is a workaround for this 
problem, to have a list of
standard strings returned.


Modified:
    trunk/relax_io.py

Modified: trunk/relax_io.py
URL: 
http://svn.gna.org/viewcvs/relax/trunk/relax_io.py?rev=17701&r1=17700&r2=17701&view=diff
==============================================================================
--- trunk/relax_io.py (original)
+++ trunk/relax_io.py Fri Oct  5 18:42:15 2012
@@ -665,6 +665,34 @@
         raise RelaxError("No corresponding data could be found within the 
file.")
 
 
+def readlines(file_path):
+    """Open the file given by the file path and returning a list of strings 
for each line.
+
+    The method is needed as bz2 compressed files return lists of byte 
strings and no longer normal
+    strings in Python 3!  This might be a temporary workaround to a 
temporary bug.
+
+
+    @param file_path:   The path of the file to open and read.
+    @type file_path:    str
+    @return:            The list of lines.
+    @rtype:             list of str
+    """
+
+    # Open the file.
+    file = open_read_file(file_path)
+    lines = file.readlines()
+    file.close()
+
+    # Convert the data from byte strings if needed.
+    if len(lines) and isinstance(lines[0], bytes):
+        for i in range(len(lines)):
+            print(dir(lines[i]))
+            lines[i] = lines[i].decode()
+
+    # Return the list of strings.
+    return lines
+
+
 def strip(data, comments=True):
     """Remove all comment and empty lines from the file data structure.
 




Related Messages


Powered by MHonArc, Updated Fri Oct 05 19:00:02 2012