def test_splitext2(convert_path): filepath0 = '/foo/bar/baz.txt' expected = ('/foo/bar', 'baz', '.txt') filepath = convert_path(filepath0) actual = splitext2(filepath) assert actual == expected filepath0 = 'baz.txt' expected = ('', 'baz', '.txt') filepath = convert_path(filepath0) actual = splitext2(filepath) assert actual == expected
def _read_tabular_h5(filepath): _, fn, ext = splitext2(filepath) _check_ext(ext, '.h5') with h5py.File(filepath, 'r') as hf: dataset = hf[fn] data = dataset[:] return data
def _write_tabular_h5(obj, filepath): _, fn, ext = splitext2(filepath) _check_ext(ext, '.h5') if isinstance(obj, np.ndarray): with h5py.File(filepath, 'w') as hf: hf.create_dataset(fn, data=obj) elif isinstance(obj, pd.core.frame.NDFrame): obj.to_hdf(filepath, key=fn) else: raise NotImplementedError
def _write_tabular_pickle(obj, filepath): _, fn, ext = splitext2(filepath) _check_ext(ext, '.pkl') if isinstance(obj, np.ndarray): with open(filepath, 'wb') as f: pickle.dump(obj, f) elif isinstance(obj, pd.core.frame.NDFrame): obj.to_pickle(filepath) else: raise NotImplementedError
def read_tabular(filepath: Pathy): """Read tabular object in HDF5 or pickle format Args: filepath: path to read to; must end in '.h5' or '.pkl' """ _, fn, ext = splitext2(filepath) if ext == '.h5': return _read_tabular_h5(filepath) elif ext == '.pkl': return _read_tabular_pickle(filepath) else: raise NotImplementedError
def write_tabular(obj: Union[np.ndarray, pd.DataFrame], filepath: Pathy): """Write tabular object in HDF5 or pickle format Args: obj: tabular object to write filepath: path to write to; must end in '.h5' or '.pkl' """ _, fn, ext = splitext2(filepath) if ext == '.h5': _write_tabular_h5(obj, filepath) elif ext == '.pkl': _write_tabular_pickle(obj, filepath) else: raise NotImplementedError
def write_tabular(obj, filepath): """Write tabular object in HDF5 or pickle format Args: obj (array or DataFrame): tabular object to write filepath (path-like): path to write to; must end in '.h5' or '.pkl' """ _, fn, ext = splitext2(filepath) if ext == '.h5': _write_tabular_h5(obj, filepath) elif ext == '.pkl': _write_tabular_pickle(obj, filepath) else: raise NotImplementedError
def _read_tabular_pickle(filepath): _, fn, ext = splitext2(filepath) _check_ext(ext, '.pkl') with open(filepath, 'rb') as f: return pickle.load(f)