Provides a unified syntax for reading and writing data to files
Project description
iotools
iotools provides a unified syntax for reading and writing data to files.
Example:
text = load_txt("filename.txt")
data = load_json("filename.json")
table = load_csv("filename.csv")
image = load_jpg("filename.jpg")
config = load_yaml("filename.yaml")
Functions
Reading
data = load_*(filename): filename is a path to an existing filedata = read_*(openfile): openfile is a file-like objectdata = decode_*(rawbytes): rawbytes is the file's bytes
Writing
save_*(filename, data): filename is a path to an existing filewrite_*(openfile, data): openfile is a file-like objectrawbytes = encode_*(data): returns bytes
Helping
help_*(): prints simple examples and simple implementations on how to use this file format without iotools functions
Installation
pip install iotools-wrappers
Supported formats
avibytes: not really a format, but use this to load raw bytescsvgifgz: gzipjpgjsonmp4npy: numpypicklepngtartxt: text filesxmlyamlzipzst: zstandard
How it works
iotools is nothing but wrappers on other python librairies, for example load_json will call json.load internally.
The backend used is specified in each function documentation.
iotools goal is to provide a memorable io syntax, on as many file formats as possible.
For more format-specific functions, or more complex io features, please use the library specific to that file format (help_*() may provide a good starting point)
Examples
Text
from iotools import load_txt, read_txt
# Load text in one line:
text = load_txt("filename.txt")
# Or if you prefer the 'with open()' syntax:
with open("filename.txt", "r") as f:
text = read_txt(f) # same as f.read()
Images
# Example where image colors are inversed
from iotools import load_image, save_image
image = load_image("input.jpg")
save_image("output.jpg", image[:, :, ::-1])
Archives
# Load a tar.gz archive
from iotools import load_gz, decode_tar
data = decode_tar(load_gz("archive.tar.gz"))
# If your archive is made of images, you can even do:
from iotools import load_gz, decode_tar, decode_image
data = {filename: decode_image(img) for filename, img in decode_tar(load_gz("archive.tar.gz")).items()}
GIFs
import numpy as np
from iotools import save_gif
gif = [np.ones((180, 320), dtype=np.uint8) * i * 10 for i in range(26)]
save_gif("filename.gif", gif, fps=6)
Additionnal features
File systems
iotools is compatible with fsspec: load_* and save_* functions have an fs kwarg in order to use different file systems.
Example:
# Load a tar.gz archive containing images
from iotools import load_image
from fsspec.implementations.tar import TarFileSystem
tarfs = TarFileSystem("archive.tar.gz")
data = {filename: load_image(filename, fs=tarfs) for filename in tarfs.glob("*")}
Makedirs
save_*(filename, data, fs=None, makedirs=False) have a kwarg makedirs that allows to automatically create filename's parent directory if it does not exist.
Format specific options
Since iotools functions are mostly wrappers of other python libraires, most of formats specific options can be used through iotools functions.
For example you can do save_json("filename.json", data, indent=4).
Use your prefered library
Sometimes several python librairies can be used to load/save data to a format. To let you use your preferred library, iotools functions are declined in several forms with the keyword using. Here is an example with jpg images:
from iotools.jpgio import load_jpg_using_imageio, load_jpg_using_pil, load_jpg_using_cv2
image = load_jpg_using_imageio("filename.jpg") # uses imageio library
image = load_jpg_using_pil("filename.jpg") # uses PIL (Pillow) library
image = load_jpg_using_cv2("filename.jpg") # uses cv2 (opencv-python) library
Note that the output image can be different based on which library is used internally
Generic formats
iotools supports 2 generic formats: images and videos. Thus there are functions like load_image or load_video to load any image or video format (formats not enumerated above were not tested).
save_image or save_video also exists, and a kwarg format is there for you to choose which image/video format to save in, otherwise it will be guessed from the filename.
Other notes
Import syntax
Several import syntaxs can be used:
from iotools import load_csv
from iotools import csvio # --> csvio.load_csv
from iotools.csvio import load_csv, load_csv_using_pandas
OS Support
iotools was only tested under Linux. Under Windows, it seems that there are still issues with csvs, images and videos, but the other formats should be fine.
Dependencies
Installing iotools does not install any format-specific backends, thus you may not be able to use a file format if the backend is not already installed. Please refer to pyproject.toml for backends supported versions.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file iotools_wrappers-0.1.1.tar.gz.
File metadata
- Download URL: iotools_wrappers-0.1.1.tar.gz
- Upload date:
- Size: 36.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-39-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a230b396bb4f10f20d579ff7225b70991b99611666a9bb004e28915ee9e26b0
|
|
| MD5 |
b3bfbd476fed44976145bd42a68df7af
|
|
| BLAKE2b-256 |
dc661d1c16c8783ab056b0af23a26026c544662055c62206aa2a867a94abd645
|
File details
Details for the file iotools_wrappers-0.1.1-py3-none-any.whl.
File metadata
- Download URL: iotools_wrappers-0.1.1-py3-none-any.whl
- Upload date:
- Size: 70.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.2.0-39-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b35b3d5e6af550db8e5ebd2f53d471b80c13f95ea0992113d5a1ec20d47a93f
|
|
| MD5 |
fe247324d2bb823629f3b66e97ed817c
|
|
| BLAKE2b-256 |
f26bfff43e77fdcf5b5ebf977765f371334cbee33201b4092ac9dadf9f414736
|