This post continue series about AXON and pyaxon. Now we consider AXON datamodel and some realted aspects of object serialization.

AXON datamodel bases on composition of several data construction patterns. It reflects both JSON and XML datamodels.

AXON datamodel has two rules of composition that reflects JSON datamodel:

  • if values $D_1,\dots,D_n$ belong to AXON datamodel then the list $[D_1,\dots,D_n]$ belongs to AXON datamodel;
  • if values $D_1,\dots,D_n$ belong to AXON datamodel then for any sequence of string keys $k_1,\dots,k_n$ the dictionary $\{k_1\colon D_1,\dots,k_2\colon D_n\}$ belongs to AXON datamodel.

In addition AXON datamodel has a rule of composition for construction of tuples — fixed sequence of values:

  • if values $D_1,\dots,D_n$ belong to AXON datamodel then the tuple $(D_1,\dots,D_n)$ belongs to AXON datamodel.

AXON datamodel also has three rules of composition that reflects XML infoset datamodel:

  • if values $D_1,\dots,D_n$ belong to AXON datamodel then for any name $\text{N}$ the named sequence $\text{N}\{D_1,\dots,D_n\}$ belongs to AXON datamodel;
  • if values $D_1,\dots,D_n$ belong to AXON datamodel then for any sequence of attribute names $a_1,\dots,a_n$ and any name $\text{N}$ the named mapping $\text{N}\{a_1\colon D_1,\dots,a_n\colon D_n\}$ belongs to AXON datamodel;
  • values if $D_1,\dots,D_n$ and $E_1,\dots,E_m$ belong to AXON datamodel then for any name $\text{N}$ and sequence of attribute names $a_1,\dots,a_n$ the named element $\text{N}\{a_1\colon D_1,\dots,a_n\colon D_n,\ E_1,\dots,E_m \}$ belongs to AXON datamodel.

First rule allows to construct structures (named sequences) similar to XML element without attributes. Second rule allows to construct structures (named mappings) similar to XML element without of subelements. Third rule allows to construct structures (named elements) similar XML element both with attributes and subelements. Unlike XML, in AXON attribute's value can be an arbitrary value.

AXON datamodel has yet another rule of composition that reflects concept of named tuple, which contain fixed number of values and may contain also optional values specified by their attribute name: res, which are composed by several rules:

  • if values $D_1,\dots,D_n$ and $E_1,\dots,E_m$ belong to AXON datamodel then for any name $\text{N}$ and sequence of attribute names $a_1,\dots,a_n$ the named instance $\text{N}\{E_1,\dots,E_m,\ a_1\colon D_1,\dots,a_n\colon D_n \}$ belongs to AXON datamodel.

Last rule is for symmetry; it useful for representation of data rows with fixed fields and optional fields specified by their names.

AXON named complex values can be mapped to objects by their names. pyaxon module supports protocol for loading and dumping.

AXON lists, dicts and tuples are mapped to python's list, dicts and tuples.

Loading protocol

For named complex values one can define a class with base class GenericBuilder, which has four methods:

  • sequence(name, list);
  • mapping(name, dict);
  • element(name, dict, list);
  • instance(name, list, dict).

During loading these methods are called according to next rules that corresponds to last four above rules of composition:

  • for objects $D_1,\dots,D_n$ and name $\text{N}$ object is constructed by calling of the method sequence( $\text{N}$, $[D_1,\dots,D_n]$ );
  • for objects $D_1,\dots,D_n$, names $a_1,\dots,a_n$ and name $\text{N}$ object is constructed by calling of the method mapping( $\text{N}$, $\{a_1\colon D_1,\dots,a_n\colon D_n\}$ );
  • for objects $D_1,\dots,D_n$ and $E_1,\dots,E_m$, name $\text{N}$ and names $a_1,\dots,a_n$ object is constructed by calling of the method element( $\text{N}$, $\{a_1\colon D_1,\dots,a_n\colon D_n\}, [E_1,\dots,E_m]$ );
  • for objects $D_1,\dots,D_n$ and $E_1,\dots,E_m$, name $\text{N}$ and names $a_1,\dots,a_n$ object is constructed by calling of the method instance( $\text{N}$, $[E_1,\dots,E_m]$, $\{a_1\colon D_1,\dots,a_n\colon D_n\}$ ).

Dumping protocol

There is a protocol for dumping python objects. In order to dump the class C instances one have to do the following:

  • define function that takes instance o of the class C and returns instance of Empty, Sequence, Mapping, Element or Instance;
  • register them using builtin reduce function.

Empty


In [1]:
from __future__ import print_function, unicode_literals
from axon.api import loads, dumps

# pretty printing of python objects
from pprint import pprint

# ipython tools for pretty of images and html
from IPython.display import HTML, display