From e840e11c3696afe7ecbf0f22b680c77999f841c0 Mon Sep 17 00:00:00 2001 From: Ruben Verweij Date: Tue, 18 Jul 2017 17:06:21 +0200 Subject: [PATCH] Fix bytes compatibility for Python 2 --- nd2reader/artificial.py | 4 ++-- tests/test_raw_metadata.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/nd2reader/artificial.py b/nd2reader/artificial.py index ac7ec8c..674e1e7 100644 --- a/nd2reader/artificial.py +++ b/nd2reader/artificial.py @@ -177,7 +177,7 @@ class ArtificialND2(object): elif isinstance(data, float): raw_data = struct.pack('d', data) elif isinstance(data, str): - raw_data = bytes(data, 'utf-8') + raw_data = six.b(data) return raw_data @@ -198,7 +198,7 @@ class ArtificialND2(object): for data_key in data.keys(): # names have always one character extra and are padded in zero bytes??? - b_data_key = b''.join([struct.pack('cx', bytes(s, 'utf-8')) for s in data_key]) + struct.pack('xx') + b_data_key = six.b('').join([struct.pack('cx', six.b(s)) for s in data_key]) + struct.pack('xx') # header consists of data type and length of key name, it is represented by 2 unsigned chars raw_data += struct.pack('BB', self._get_data_type(data[data_key]), len(data_key) + 1) diff --git a/tests/test_raw_metadata.py b/tests/test_raw_metadata.py index 8703980..75efc75 100644 --- a/tests/test_raw_metadata.py +++ b/tests/test_raw_metadata.py @@ -1,4 +1,5 @@ import unittest +import six from nd2reader.artificial import ArtificialND2 from nd2reader.label_map import LabelMap @@ -39,7 +40,7 @@ class TestRawMetadata(unittest.TestCase): def _assert_dicts_equal(self, parsed_dict, original_dict): for attribute in original_dict.keys(): - parsed_key = bytes(attribute, 'utf-8') + parsed_key = six.b(attribute) self.assertIn(parsed_key, parsed_dict.keys()) if isinstance(parsed_dict[parsed_key], dict):