Ejemplo n.º 1
0
 def test_bad_file_mode(self):
     with open(os.devnull) as f:
         with self.assertRaises(OSError):
             _kastore.dump({}, f)
     with open(os.devnull, "w") as f:
         with self.assertRaises(OSError):
             _kastore.load(f)
Ejemplo n.º 2
0
 def test_bad_fd(self):
     bad_fd = 10000
     with self.assertRaises(OSError):
         _kastore.dump({}, bad_fd)
     with self.assertRaises(OSError):
         _kastore.load(bad_fd)
     bad_fd = -1
     with self.assertRaises(ValueError):
         _kastore.dump({}, bad_fd)
     with self.assertRaises(ValueError):
         _kastore.load(bad_fd)
Ejemplo n.º 3
0
def load(file,
         read_all=False,
         key_encoding=DEFAULT_KEY_ENCODING,
         engine=PY_ENGINE):
    """
    Loads a store from the specified file.

    :param str file: The path of the file to load, or a file-like object
        with a ``read()`` method.
    :param bool read_all: If True, read the entire file into memory. This
        optimisation is useful when all the data will be needed, saving some
        malloc and fread overhead.
    :param str key_encoding: The encoding to use when converting the keys from
        raw bytes.
    :param str engine: The underlying implementation to use.
    :return: A dict-like object mapping the key-array pairs.
    """
    if engine == PY_ENGINE:
        # The Python engine returns an object which needs to keep its own
        # copy of the file object, so must handle the semantics of opening
        # itself.
        ret = store.load(file, key_encoding=key_encoding, read_all=read_all)

    elif engine == C_ENGINE:
        _check_low_level_module()
        with _open_file(file, "rb") as f:
            ret = _kastore.load(f, read_all=read_all)
    else:
        _raise_unknown_engine()
    return ret
Ejemplo n.º 4
0
def load(filename, read_all=False, key_encoding="utf-8", engine=PY_ENGINE):
    """
    Loads a store from the specified file.

    :param bool read_all: If True, read the entire file into memory. This
        optimisation is useful when all the data will be needed, saving some
        malloc and fread overhead.
    :param str filename: The path of the file to load.
    :param str key_encoding: The encoding to use when converting the keys from
        raw bytes.
    :param str engine: The underlying implementation to use.
    :return: A dict-like object mapping the key-array pairs.
    """
    if engine == PY_ENGINE:
        return store.load(filename,
                          read_all=read_all,
                          key_encoding=key_encoding)
    elif engine == C_ENGINE:
        _check_low_level_module()
        try:
            return _kastore.load(filename, read_all=read_all)
        except _kastore.FileFormatError as e:
            # TODO implement this using exception chaining, "raise X from e"
            # https://github.com/tskit-dev/kastore/issues/81
            raise FileFormatError(str(e))
        except _kastore.VersionTooOldError:
            raise VersionTooOldError()
        except _kastore.VersionTooNewError:
            raise VersionTooNewError()

    else:
        _raise_unknown_engine()
Ejemplo n.º 5
0
 def test_fileno_round_trip(self):
     with tempfile.TemporaryFile() as f:
         data = {"a": np.arange(10)}
         _kastore.dump(data, f.fileno())
         f.seek(0)
         x = _kastore.load(f.fileno())
         self.assertDataEqual(x, data)
Ejemplo n.º 6
0
 def test_same_file_round_trip(self):
     with tempfile.TemporaryFile() as f:
         for j in range(10):
             start = f.tell()
             data = {"a": np.arange(j * 100)}
             _kastore.dump(data, f)
             f.seek(start)
             x = _kastore.load(f)
             self.assertDataEqual(x, data)
Ejemplo n.º 7
0
 def test_bad_load_args(self):
     with self.assertRaises(TypeError):
         _kastore.load(0, read_all="sdf")
Ejemplo n.º 8
0
 def test_bad_numeric_fd(self):
     for fd in ["1", 1.0, 2.0]:
         with self.assertRaises(TypeError):
             _kastore.dump({}, fd)
         with self.assertRaises(TypeError):
             _kastore.load(fd)