# Path: hurray/msgpack_ext.py
# def encode(obj):
#     """
#     Encode numpy arrays and slices
#     :param obj: object to serialize
#     :return: dictionary with encoded array or slice
#     """
#     if isinstance(obj, np.ndarray):
#         arr = header_data_from_array_1_0(obj)
#         arr['arraydata'] = obj.tostring()
#         arr['__ndarray__'] = True
#         return arr
#     elif isinstance(obj, slice):
#         return {
#             '__slice__': (obj.start, obj.stop, obj.step)
#         }
#     elif isclass(obj) and issubclass(obj, np.number):
#         # make sure numpy type classes such as np.float64 (used, e.g., as dtype
#         # arguments) are serialized to strings
#         return obj().dtype.name
#     elif isinstance(obj, np.dtype):
#         return obj.name
#     elif isinstance(obj, np.number):
#         # convert to Python scalar
#         return np.asscalar(obj)
#     elif isinstance(obj, File):
#         data = {
#             RESPONSE_H5FILE: obj.file,
#             RESPONSE_NODE_TYPE: NODE_TYPE_FILE,
#             RESPONSE_NODE_PATH: obj.path,
#         }
#         return data
#     elif isinstance(obj, Group):
#         # TODO include attrs?
#         data = {
#             RESPONSE_H5FILE: obj.file,
#             RESPONSE_NODE_TYPE: NODE_TYPE_GROUP,
#             RESPONSE_NODE_PATH: obj.path,
#         }
#         return data
#     elif isinstance(obj, Dataset):
#         # TODO include attrs?
#         data = {
#             RESPONSE_H5FILE: obj.file,
#             RESPONSE_NODE_TYPE: NODE_TYPE_DATASET,
#             RESPONSE_NODE_PATH: obj.path,
#             RESPONSE_NODE_SHAPE: obj.shape,
#             RESPONSE_NODE_DTYPE: obj.dtype,
#         }
#         return data
# 
#     return obj
# 
# def decode(obj):
#     """
#     Decode numpy arrays and slices
#     :param obj: object to decode
#     :return: numpy array or slice
#     """
# 
#     if '__ndarray__' in obj:
#         arr = np.fromstring(obj['arraydata'], dtype=np.dtype(obj['descr']))
#         shape = obj['shape']
#         arr.shape = shape
#         if obj['fortran_order']:
#             arr.shape = shape[::-1]
#             arr = arr.transpose()
#         return arr
#     elif '__slice__' in obj:
#         return slice(*obj['__slice__'])
# 
#     return obj


import unittest
import msgpack
import numpy as np
from hurray.msgpack_ext import encode, decode
from numpy.testing import assert_array_equal



class MsgPackTestCase(unittest.TestCase):
    def test_ndarray(self):
        data_in = np.random.randint(0, 255, size=(5, 10)).astype('uint8')

        packed_nparray = msgpack.packb(data_in, default=encode,
                                       use_bin_type=True)
        unpacked_nparray = msgpack.unpackb(packed_nparray,
                                           object_hook=decode,
                                           encoding='utf-8')

        assert_array_equal(data_in, unpacked_nparray)

    def test_slice(self):
        slice_in = slice(0, 1, 0)

        packed_slice = msgpack.packb(slice_in, default=encode,
                                     use_bin_type=True)
        unpacked_slice = msgpack.unpackb(packed_slice,

# Path: hurray/msgpack_ext.py
# def encode(obj):
#     """
#     Encode numpy arrays and slices
#     :param obj: object to serialize
#     :return: dictionary with encoded array or slice
#     """
#     if isinstance(obj, np.ndarray):
#         arr = header_data_from_array_1_0(obj)
#         arr['arraydata'] = obj.tostring()
#         arr['__ndarray__'] = True
#         return arr
#     elif isinstance(obj, slice):
#         return {
#             '__slice__': (obj.start, obj.stop, obj.step)
#         }
#     elif isclass(obj) and issubclass(obj, np.number):
#         # make sure numpy type classes such as np.float64 (used, e.g., as dtype
#         # arguments) are serialized to strings
#         return obj().dtype.name
#     elif isinstance(obj, np.dtype):
#         return obj.name
#     elif isinstance(obj, np.number):
#         # convert to Python scalar
#         return np.asscalar(obj)
#     elif isinstance(obj, File):
#         data = {
#             RESPONSE_H5FILE: obj.file,
#             RESPONSE_NODE_TYPE: NODE_TYPE_FILE,
#             RESPONSE_NODE_PATH: obj.path,
#         }
#         return data
#     elif isinstance(obj, Group):
#         # TODO include attrs?
#         data = {
#             RESPONSE_H5FILE: obj.file,
#             RESPONSE_NODE_TYPE: NODE_TYPE_GROUP,
#             RESPONSE_NODE_PATH: obj.path,
#         }
#         return data
#     elif isinstance(obj, Dataset):
#         # TODO include attrs?
#         data = {
#             RESPONSE_H5FILE: obj.file,
#             RESPONSE_NODE_TYPE: NODE_TYPE_DATASET,
#             RESPONSE_NODE_PATH: obj.path,
#             RESPONSE_NODE_SHAPE: obj.shape,
#             RESPONSE_NODE_DTYPE: obj.dtype,
#         }
#         return data
# 
#     return obj
# 
# def decode(obj):
#     """
#     Decode numpy arrays and slices
#     :param obj: object to decode
#     :return: numpy array or slice
#     """
# 
#     if '__ndarray__' in obj:
#         arr = np.fromstring(obj['arraydata'], dtype=np.dtype(obj['descr']))
#         shape = obj['shape']
#         arr.shape = shape
#         if obj['fortran_order']:
#             arr.shape = shape[::-1]
#             arr = arr.transpose()
#         return arr
#     elif '__slice__' in obj:
#         return slice(*obj['__slice__'])
# 
#     return obj


# Path: tests/msgpack_ext.py
import unittest
import msgpack
import numpy as np
from hurray.msgpack_ext import encode, decode
from numpy.testing import assert_array_equal




class MsgPackTestCase(unittest.TestCase):
    def test_ndarray(self):
        data_in = np.random.randint(0, 255, size=(5, 10)).astype('uint8')

        packed_nparray = msgpack.packb(data_in, default=encode,
                                       use_bin_type=True)
        unpacked_nparray = msgpack.unpackb(packed_nparray,
                                           object_hook=decode,
                                           encoding='utf-8')

        assert_array_equal(data_in, unpacked_nparray)

    def test_slice(self):
        slice_in = slice(0, 1, 0)

        packed_slice = msgpack.packb(slice_in, default=encode,
                                     use_bin_type=True)
        unpacked_slice = msgpack.unpackb(packed_slice,

                                         object_hook=decode,