Quick Start


In [1]:
from __future__ import unicode_literals, print_function
from axon import loads, dumps
from axon import mapping, element, instance, sequence
from datetime import date, time, datetime
try:
    from cdecimal import Decimal
except:
    from decimal import Decimal

Dicts

Dictionaries are represented in AXON similar to JSON's objects. There are only 2 small differencies:

  • there are no commas (,) between values, but only spaces;
  • if the key is an identifier then double quotes (") around the key may be omitted.

In [7]:
val = {'a': 1, 'b': 'qwerty', 'c': 3.141528, 'd': True, 'e': datetime.now()}
text = dumps([val])
print(text)


{a:1 b:"qwerty" c:3.141528 d:true e:2013-12-26T11:17:43.755896}

This is compact form of representation. It's suitable for data exchange in a stream. But there also exists formatted form suitable for humans:


In [8]:
text = dumps([val], pretty=1)
print(text)


{ a: 1
  b: "qwerty"
  c: 3.141528
  d: true
  e: 2013-12-26T11:17:43.755896}

Lists

Lists are ordered collections of values.

Lists are represented in AXON similar to JSON's arrays. There is only a small difference:

  • there are no commas (,) between values, but only spaces.

In [11]:
val = [1, 'qwerty', 3.141528, True, datetime.now()]
text = dumps([val])
print(text)


[1 "qwerty" 3.141528 true 2013-12-26T11:18:25.587103]

This is compact form of representation. There is formatted form:


In [12]:
text = dumps([val], pretty=1)
print(text)


[ 1
  "qwerty"
  3.141528
  true
  2013-12-26T11:18:25.587103]

Tuples

Tuples are only for fixed collections of values. They are represented as lists, but enclose in opened bracket '(' and closed bracket ')'.

For example:


In [13]:
val = [('r', 10), ('g', 20), ('b', 30)]
text = dumps([val])
print(text)


[("r" 10) ("g" 20) ("b" 30)]

In [16]:
text = dumps([val], pretty=1)
print(text)


[
  ( "r"
    10)
  ( "g"
    20)
  ( "b"
    30)]

This formatted representation is "too vertical". In order to make it "more horizontal" there is parameter hsize. It define number of simple values that can be put before breaking the line:


In [17]:
text = dumps([val], pretty=1, hsize=2)
print(text)


[
  ( "r" 10)
  ( "g" 20)
  ( "b" 30)]

Compositions of dictionaries, lists and tuples

Any JSON-like data, i.e. data, which are compositions of dictionaries, lists and tuples, can be easily represented in AXON. For exampe:


In [23]:
val = {
    'node': [1,2,3,4,5,6,7],
    'edge': [(1,2), (2,3), (3,7), (5,6)]
}
print('Compact form:')
text = dumps([val])
print(text)
print('Formatted form:')
text = dumps([val], pretty=1)
print(text)
print('More compact formatted form:')
text = dumps([val], pretty=1, hsize=8)
print(text)


Compact form:
{edge:[(1 2) (2 3) (3 7) (5 6)] node:[1 2 3 4 5 6 7]}
Formatted form:
{
  edge: [
    ( 1
      2)
    ( 2
      3)
    ( 3
      7)
    ( 5
      6)]
  node: [
    1
    2
    3
    4
    5
    6
    7]}
More compact formatted form:
{
  edge: [
    ( 1 2)
    ( 2 3)
    ( 3 7)
    ( 5 6)]
  node: [
    1 2 3 4 5 6 7]}

In [ ]: