Browse Source

Fix bytes compatibility for Python 2

zolfa-add_slices_loading
Ruben Verweij 7 years ago
parent
commit
5214180e70
2 changed files with 4 additions and 3 deletions
  1. +2
    -2
      nd2reader/artificial.py
  2. +2
    -1
      tests/test_raw_metadata.py

+ 2
- 2
nd2reader/artificial.py View File

@ -177,7 +177,7 @@ class ArtificialND2(object):
elif isinstance(data, float): elif isinstance(data, float):
raw_data = struct.pack('d', data) raw_data = struct.pack('d', data)
elif isinstance(data, str): elif isinstance(data, str):
raw_data = bytes(data, 'utf-8')
raw_data = six.b(data)
return raw_data return raw_data
@ -198,7 +198,7 @@ class ArtificialND2(object):
for data_key in data.keys(): for data_key in data.keys():
# names have always one character extra and are padded in zero bytes??? # 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 # 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) raw_data += struct.pack('BB', self._get_data_type(data[data_key]), len(data_key) + 1)


+ 2
- 1
tests/test_raw_metadata.py View File

@ -1,4 +1,5 @@
import unittest import unittest
import six
from nd2reader.artificial import ArtificialND2 from nd2reader.artificial import ArtificialND2
from nd2reader.label_map import LabelMap from nd2reader.label_map import LabelMap
@ -39,7 +40,7 @@ class TestRawMetadata(unittest.TestCase):
def _assert_dicts_equal(self, parsed_dict, original_dict): def _assert_dicts_equal(self, parsed_dict, original_dict):
for attribute in original_dict.keys(): for attribute in original_dict.keys():
parsed_key = bytes(attribute, 'utf-8')
parsed_key = six.b(attribute)
self.assertIn(parsed_key, parsed_dict.keys()) self.assertIn(parsed_key, parsed_dict.keys())
if isinstance(parsed_dict[parsed_key], dict): if isinstance(parsed_dict[parsed_key], dict):


Loading…
Cancel
Save