Encoding and decoding Python data structrues using portable JData-annotated formats
Project description
JData for Python - lightweight and serializable data annotations for Python
- Copyright: (C) Qianqian Fang (2019-2022) <q.fang at neu.edu>
- License: Apache License, Version 2.0
- Version: 0.4.3
- URL: https://github.com/NeuroJSON/pyjdata
The JData Specification defines a lightweight
language-independent data annotation interface targetted at
storing and sharing complex data structures across different programming
languages such as MATLAB, JavaScript, Python etc. Using JData formats, a
complex Python data structure can be encoded as a dict
object that is easily
serialized as a JSON/binary JSON file and share such data between
programs of different languages.
How to install
- Github: download from https://github.com/NeuroJSON/pyjdata
- PIP: run
pip install jdata
see https://pypi.org/project/jdata/
This package can also be installed on Ubuntu 21.04 or Debian Bullseye via
sudo apt-get install python3-jdata
On older Ubuntu or Debian releases, you may install jdata via the below PPA:
sudo add-apt-repository ppa:fangq/ppa
sudo apt-get update
sudo apt-get install python3-jdata
Dependencies:
- numpy: PIP: run
pip install numpy
orsudo apt-get install python3-numpy
- (optional) bjdata: PIP: run
pip install bjdata
orsudo apt-get install python3-bjdata
, see https://pypi.org/project/bjdata/, only needed to read/write BJData/UBJSON files - (optional) lz4: PIP: run
pip install lz4
, only needed when encoding/decoding lz4-compressed data - (optional) backports.lzma: PIP: run
sudo apt-get install liblzma-dev
andpip install backports.lzma
(needed for Python 2.7), only needed when encoding/decoding lzma-compressed data
Replacing pip
by pip3
if you are using Python 3.x. If either pip
or pip3
does not exist on your system, please run
sudo apt-get install python3-pip
Please note that in some OS releases (such as Ubuntu 20.04), python2.x and python-pip are no longer supported.
One can also install this module from the source code. To do this, you first check out a copy of the latest code from Github by
git clone https://github.com/NeuroJSON/pyjdata.git
cd pyjdata
then install the module to your local user folder by
python3 setup.py install --user
or, if you prefer, install to the system folder for all users by
sudo python3 setup.py install
Please replace python
by python3
if you want to install it for Python 3.x instead of 2.x.
Instead of installing the module, you can also import the jdata module directly from your local copy by cd the root folder of the unzipped pyjdata package, and run
import jdata as jd
How to use
The PyJData module is easy to use. You can use the encode()/decode()
functions to
encode Python data into JData annotation format, or decode JData structures into
native Python data, for example
import jdata as jd
import numpy as np
a={'str':'test','num':1.2,'list':[1.1,[2.1]],'nan':float('nan'),'np':np.arange(1,5,dtype=np.uint8)}
jd.encode(a)
jd.decode(jd.encode(a))
d1=jd.encode(a,{'compression':'zlib','base64':1})
d1
jd.decode(d1,{'base64':1})
One can further save the JData annotated data into JSON or binary JSON (UBJSON) files using
the jdata.save
function, or loading JData-formatted data to Python using jdata.load
import jdata as jd
import numpy as np
a={'str':'test','num':1.2,'list':[1.1,[2.1]],'nan':float('nan'),'np':np.arange(1,5,dtype=np.uint8)}
jd.save(a,'test.json')
newdata=jd.load('test.json')
newdata
Utility
One can convert from JSON based data files (.json, .jdt, .jnii, .jmsh, .jnirs
) to binary-JData
based binary files (.bjd, .jdb, .bnii, .bmsh, .bnirs
) and vice versa using command
python3 -mjdata /path/to/text/json/file.json # convert to /path/to/text/json/file.jdb
python3 -mjdata /path/to/text/json/file.jdb # convert to /path/to/text/json/file.json
python3 -mjdata -h # show help info
Test
To see additional data type support, please run the built-in test using below command
python3 -m unittest discover -v test
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
Hashes for jdata-0.4.3-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cd3a4f817fef4e9bc16a5615959d3dc646fd4b43fb5999866477c36b419e893 |
|
MD5 | b6a3e5210b6c19a22b616913482104ca |
|
BLAKE2b-256 | 230adbdd8171858418c1cf16714c0b7c27ab21d0ca9f25df59f2f1238b9e3a51 |