# Path: numcodecs/compat.py
# def ensure_bytes(buf):
#     """Obtain a bytes object from memory exposed by `buf`."""
# 
#     if not isinstance(buf, bytes):
# 
#         # go via numpy, for convenience
#         arr = ensure_ndarray(buf)
# 
#         # check for object arrays, these are just memory pointers,
#         # actual memory holding item data is scattered elsewhere
#         if arr.dtype == object:
#             raise TypeError('object arrays are not supported')
# 
#         # create bytes
#         buf = arr.tobytes(order='A')
# 
#     return buf
# 
# def ensure_ndarray(buf):
#     """Convenience function to coerce `buf` to a numpy array, if it is not already a
#     numpy array.
# 
#     Parameters
#     ----------
#     buf : array-like or bytes-like
#         A numpy array or any object exporting a buffer interface.
# 
#     Returns
#     -------
#     arr : ndarray
#         A numpy array, sharing memory with `buf`.
# 
#     Notes
#     -----
#     This function will not create a copy under any circumstances, it is guaranteed to
#     return a view on memory exported by `buf`.
# 
#     """
# 
#     if isinstance(buf, np.ndarray):
#         # already a numpy array
#         arr = buf
# 
#     elif isinstance(buf, array.array) and buf.typecode in 'cu':
#         # Guard condition, do not support array.array with unicode type, this is
#         # problematic because numpy does not support it on all platforms. Also do not
#         # support char as it was removed in Python 3.
#         raise TypeError('array.array with char or unicode type is not supported')
# 
#     else:
# 
#         # N.B., first take a memoryview to make sure that we subsequently create a
#         # numpy array from a memory buffer with no copy
#         mem = memoryview(buf)
# 
#         # instantiate array from memoryview, ensures no copy
#         arr = np.array(mem, copy=False)
# 
#     return arr
# 
# Path: numcodecs/registry.py
# def get_codec(config):
#     """Obtain a codec for the given configuration.
# 
#     Parameters
#     ----------
#     config : dict-like
#         Configuration object.
# 
#     Returns
#     -------
#     codec : Codec
# 
#     Examples
#     --------
# 
#     >>> import numcodecs as codecs
#     >>> codec = codecs.get_codec(dict(id='zlib', level=1))
#     >>> codec
#     Zlib(level=1)
# 
#     """
#     config = dict(config)
#     codec_id = config.pop('id', None)
#     cls = codec_registry.get(codec_id)
#     if cls is None:
#         if codec_id in entries:
#             logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
#             cls = entries[codec_id].load()
#             register_codec(cls, codec_id=codec_id)
#     if cls:
#         return cls.from_config(config)
#     raise ValueError('codec not available: %r' % codec_id)


import array
import json as _json
import os
import numpy as np
import pytest
from glob import glob
from numpy.testing import assert_array_almost_equal, assert_array_equal
from numcodecs.compat import ensure_bytes, ensure_ndarray
from numcodecs.registry import get_codec
from numcodecs import *  # noqa
    for arr_fn in glob(os.path.join(fixture_dir, 'array.*.npy')):

        # setup
        i = int(arr_fn.split('.')[-2])
        arr = np.load(arr_fn, allow_pickle=True)
        arr_bytes = arr.tobytes(order='A')
        if arr.flags.f_contiguous:
            order = 'F'
        else:
            order = 'C'

        for j, codec in enumerate(codecs):

            if codec is None:
                pytest.skip("codec has been removed")

            # setup a directory to hold encoded data
            codec_dir = os.path.join(fixture_dir, 'codec.{:02d}'.format(j))
            if not os.path.exists(codec_dir):  # pragma: no cover
                os.makedirs(codec_dir)

            # file with codec configuration information
            codec_fn = os.path.join(codec_dir, 'config.json')
            # one time save config
            if not os.path.exists(codec_fn):  # pragma: no cover
                with open(codec_fn, mode='w') as cf:
                    _json.dump(codec.get_config(), cf, sort_keys=True, indent=4)
            # load config and compare with expectation
            with open(codec_fn, mode='r') as cf:
                config = _json.load(cf)

# Path: numcodecs/compat.py
# def ensure_bytes(buf):
#     """Obtain a bytes object from memory exposed by `buf`."""
# 
#     if not isinstance(buf, bytes):
# 
#         # go via numpy, for convenience
#         arr = ensure_ndarray(buf)
# 
#         # check for object arrays, these are just memory pointers,
#         # actual memory holding item data is scattered elsewhere
#         if arr.dtype == object:
#             raise TypeError('object arrays are not supported')
# 
#         # create bytes
#         buf = arr.tobytes(order='A')
# 
#     return buf
# 
# def ensure_ndarray(buf):
#     """Convenience function to coerce `buf` to a numpy array, if it is not already a
#     numpy array.
# 
#     Parameters
#     ----------
#     buf : array-like or bytes-like
#         A numpy array or any object exporting a buffer interface.
# 
#     Returns
#     -------
#     arr : ndarray
#         A numpy array, sharing memory with `buf`.
# 
#     Notes
#     -----
#     This function will not create a copy under any circumstances, it is guaranteed to
#     return a view on memory exported by `buf`.
# 
#     """
# 
#     if isinstance(buf, np.ndarray):
#         # already a numpy array
#         arr = buf
# 
#     elif isinstance(buf, array.array) and buf.typecode in 'cu':
#         # Guard condition, do not support array.array with unicode type, this is
#         # problematic because numpy does not support it on all platforms. Also do not
#         # support char as it was removed in Python 3.
#         raise TypeError('array.array with char or unicode type is not supported')
# 
#     else:
# 
#         # N.B., first take a memoryview to make sure that we subsequently create a
#         # numpy array from a memory buffer with no copy
#         mem = memoryview(buf)
# 
#         # instantiate array from memoryview, ensures no copy
#         arr = np.array(mem, copy=False)
# 
#     return arr
# 
# Path: numcodecs/registry.py
# def get_codec(config):
#     """Obtain a codec for the given configuration.
# 
#     Parameters
#     ----------
#     config : dict-like
#         Configuration object.
# 
#     Returns
#     -------
#     codec : Codec
# 
#     Examples
#     --------
# 
#     >>> import numcodecs as codecs
#     >>> codec = codecs.get_codec(dict(id='zlib', level=1))
#     >>> codec
#     Zlib(level=1)
# 
#     """
#     config = dict(config)
#     codec_id = config.pop('id', None)
#     cls = codec_registry.get(codec_id)
#     if cls is None:
#         if codec_id in entries:
#             logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
#             cls = entries[codec_id].load()
#             register_codec(cls, codec_id=codec_id)
#     if cls:
#         return cls.from_config(config)
#     raise ValueError('codec not available: %r' % codec_id)


# Path: numcodecs/tests/common.py
import array
import json as _json
import os
import numpy as np
import pytest
from glob import glob
from numpy.testing import assert_array_almost_equal, assert_array_equal
from numcodecs.compat import ensure_bytes, ensure_ndarray
from numcodecs.registry import get_codec
from numcodecs import *  # noqa

    for arr_fn in glob(os.path.join(fixture_dir, 'array.*.npy')):

        # setup
        i = int(arr_fn.split('.')[-2])
        arr = np.load(arr_fn, allow_pickle=True)
        arr_bytes = arr.tobytes(order='A')
        if arr.flags.f_contiguous:
            order = 'F'
        else:
            order = 'C'

        for j, codec in enumerate(codecs):

            if codec is None:
                pytest.skip("codec has been removed")

            # setup a directory to hold encoded data
            codec_dir = os.path.join(fixture_dir, 'codec.{:02d}'.format(j))
            if not os.path.exists(codec_dir):  # pragma: no cover
                os.makedirs(codec_dir)

            # file with codec configuration information
            codec_fn = os.path.join(codec_dir, 'config.json')
            # one time save config
            if not os.path.exists(codec_fn):  # pragma: no cover
                with open(codec_fn, mode='w') as cf:
                    _json.dump(codec.get_config(), cf, sort_keys=True, indent=4)
            # load config and compare with expectation
            with open(codec_fn, mode='r') as cf:
                config = _json.load(cf)

                assert codec == get_codec(config)