Remove str3 compatibility function

This commit is contained in:
2020-02-16 09:59:46 +02:00
parent c2e7a1602f
commit 87825b7891
2 changed files with 8 additions and 15 deletions

View File

@@ -10,17 +10,10 @@ from os import SEEK_SET
import time
from datetime import date, datetime, timedelta
from collections import defaultdict, OrderedDict
import functools
import numpy as np
if sys.version_info.major <= 2:
str3 = str
else:
str3 = functools.partial(str, encoding='ascii')
def fieldname_to_dtype(fieldname):
"""Converts a column header from the MPT file into a tuple of
canonical name and appropriate numpy dtype"""
@@ -53,7 +46,7 @@ def comma_converter(float_text):
return float(float_text.translate(trans_table))
def MPTfile(file_or_path):
def MPTfile(file_or_path, encoding='ascii'):
"""Opens .mpt files as numpy record arrays
Checks for the correct headings, skips any comments and returns a
@@ -80,7 +73,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 = str3(next(mpt_file)).strip().split('\t')
fieldnames = next(mpt_file).decode(encoding).strip().split('\t')
record_type = np.dtype(list(map(fieldname_to_dtype, fieldnames)))
# Must be able to parse files where commas are used for decimal points
@@ -356,9 +349,9 @@ class MPRfile:
self.npts = n_data_points
try:
tm = time.strptime(str3(settings_mod['date']), '%m/%d/%y')
tm = time.strptime(settings_mod['date'].decode('ascii'), '%m/%d/%y')
except ValueError:
tm = time.strptime(str3(settings_mod['date']), '%m-%d-%y')
tm = time.strptime(settings_mod['date'].decode('ascii'), '%m-%d-%y')
self.startdate = date(tm.tm_year, tm.tm_mon, tm.tm_mday)
if maybe_loop_module:
@@ -374,9 +367,9 @@ class MPRfile:
if maybe_log_module:
log_module, = maybe_log_module
try:
tm = time.strptime(str3(log_module['date']), '%m/%d/%y')
tm = time.strptime(log_module['date'].decode('ascii'), '%m/%d/%y')
except ValueError:
tm = time.strptime(str3(log_module['date']), '%m-%d-%y')
tm = time.strptime(log_module['date'].decode('ascii'), '%m-%d-%y')
self.enddate = date(tm.tm_year, tm.tm_mon, tm.tm_mday)
# There is a timestamp at either 465 or 469 bytes

View File

@@ -9,7 +9,7 @@ from numpy.testing import assert_array_almost_equal, assert_array_equal
import pytest
from galvani import BioLogic, MPTfile, MPRfile
from galvani.BioLogic import MPTfileCSV, str3 # not exported
from galvani.BioLogic import MPTfileCSV # not exported
def test_open_MPT(testdata_dir):
@@ -103,7 +103,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(str3(time_match.group(1)),
timestamp = datetime.strptime(time_match.group(1).decode('ascii'),
'%m/%d/%Y %H:%M:%S')
return timestamp
raise AttributeError("No timestamp in comments")