Tweaks to make things work in both python 3 and python 2

This commit is contained in:
Chris Kerr
2013-12-12 15:24:20 +00:00
parent 5c747fe404
commit c302ec9117
2 changed files with 9 additions and 7 deletions

View File

@@ -68,7 +68,7 @@ def MPTfile(file_or_path):
## make three lines. Every additional line is a comment line.
comments = [next(mpt_file) for i in range(nb_headers - 3)]
fieldnames = next(mpt_file).decode('ascii').strip().split('\t')
fieldnames = str3(next(mpt_file)).strip().split('\t')
record_type = np.dtype(list(map(fieldname_to_dtype, fieldnames)))
mpt_array = np.loadtxt(mpt_file, dtype=record_type)
@@ -89,7 +89,7 @@ def MPTfileCSV(file_or_path):
mpt_file = file_or_path
magic = next(mpt_file)
if magic != 'EC-Lab ASCII FILE\n':
if magic.rstrip() != 'EC-Lab ASCII FILE':
raise ValueError("Bad first line for EC-Lab file: '%s'" % magic)
nb_headers_match = re.match('Nb header lines : (\d+)\s*$', next(mpt_file))
@@ -248,8 +248,10 @@ class MPRfile:
raise ValueError("Unrecognised version for data module: %d" %
data_module['version'])
for empty_byte in remaining_headers:
assert(empty_byte == b'\x00')
if sys.version_info.major <= 2:
assert(all((b == '\x00' for b in remaining_headers)))
else:
assert(not any(remaining_headers))
self.dtype = VMPdata_dtype_from_colIDs(column_types)
self.data = np.fromstring(main_data, dtype=self.dtype)

View File

@@ -9,7 +9,7 @@ from numpy.testing import assert_array_almost_equal, assert_array_equal
from nose.tools import ok_, eq_, raises
from .. import MPTfile, MPRfile
from ..BioLogic import MPTfileCSV # not exported
from ..BioLogic import MPTfileCSV, str3 # not exported
testdata_dir = os.path.join(os.path.dirname(__file__), 'testdata')
@@ -93,7 +93,7 @@ def timestamp_from_comments(comments):
for line in comments:
time_match = re.match(b'Acquisition started on : ([0-9/]+ [0-9:]+)', line)
if time_match:
timestamp = datetime.strptime(str(time_match.group(1), encoding='ascii'),
timestamp = datetime.strptime(str3(time_match.group(1)),
'%m/%d/%Y %H:%M:%S')
return timestamp
raise AttributeError("No timestamp in comments")