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
Dictionaries are represented in AXON similar to JSON's objects. There are only 2 small differencies:
,) between values, but only spaces;") 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)
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)
Lists are ordered collections of values.
Lists are represented in AXON similar to JSON's arrays. There is only a small difference:
,) between values, but only spaces.
In [11]:
val = [1, 'qwerty', 3.141528, True, datetime.now()]
text = dumps([val])
print(text)
This is compact form of representation. There is formatted form:
In [12]:
text = dumps([val], pretty=1)
print(text)
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)
In [16]:
text = dumps([val], pretty=1)
print(text)
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)
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)
In [ ]: