ansar-encode

The ansar-encode library provides for the convenient storage and recovery of application data using system files. Files are created using standard encodings - the default is JSON - and are human readable. Complex application data can be stored including containers, instances of classes and object graphs.

Installation

ansar-encode can be installed from PyPI using pip:

pip install ansar-encode

Features

  • Broad suite of primitive types, e.g. integers, floats, strings, times and enumerations.

  • Structured data, e.g. an 8-by-8 table of user-defined class instances.

  • Recovered data is fully-typed, e.g. reading a class User produces a User instance.

  • Graphs and graphs that include cycles, e.g. circular lists, syntax trees and state diagrams.

  • Polymorphism, e.g. read an object of unknown type.

  • Type-checking.

  • Plain text files.

  • Managed folders of files.

  • Object versioning.

Quick Taste

Storage and recovery operations are reduced to a few simple lines. Taking the simplest possible example of application data;

import ansar.encode as ar

f = ar.File('my-flag', bool)
b, _ = f.recover()
..

b = not b
f.store(b)

A boolean value is read from the file called my-flag.json and assigned to the variable b. At some later point b is modified and the new value is written back to the same file, overwriting its previous contents.

Note

The _ variable is ignored. It contains version information about the recovered application data, i.e. for a boolean value this is always None.

The library is equally comfortable with arbitrarily complex data. Storage of an 8-by-8 matrix of floating point follows the same coding pattern and is quite readable;

import ansar.encode as ar

f = ar.File('my-matrix', ar.ArrayOf(ar.ArrayOf(float,8),8))
m, _ = f.recover()
..

m[0][7] = 0.125
f.store(m)

Author

The ansar-encode library was developed by <scott.18.ansar@gmail.com>. The library has evolved under the influence of several large projects and more recently has moved from C++ to Python.

License

The ansar-encode library is released under the MIT License.