Numpy Specifics¶
If you end up with numpy arrays in your tree, you are no longer JSON
compatible. You can remediate this by using the np.ndarray.tolist()
method,
but this can lead to a very long JSON file. To help you with this, Nob offers
the np_serialize
method, which efficiently rewrites all numpy arrays as
binary strings using the internal np.save
function. You can even compress
these using the standard zip algorithm by passing the compress=True
argument. The result can be written directly to disc as a JSON or YAML file:
n.np_serialize()
# OR
n.np_serialize(compress=True)
with open('file.json', 'w') as fh:
json.dump(n[:], fh)
# OR
with open('file.yml', 'w') as fh:
yaml.dump(n[:], fh)
To read it back, use the opposite function np_deserialize
:
with open('file.json') as fh:
n = Nob(json.load(fh))
# OR
with open('file.yml') as fh:
n = Nob(yaml.load(fh))
n.np_deserialize()
And that’s it, your original Nob has been recreated.