Browse Source

Fix bytes compatibility for Python 2

master
Ruben Verweij 8 years ago
parent
commit
b143f85e3e
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):
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)


+ 2
- 1
tests/test_raw_metadata.py View File

@ -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):


Loading…
Cancel
Save