AXON by examples


In [1]:
import axon

line = '\n'+32*'-'

Sequence of key:value pairs


In [25]:
vals = axon.loads("""
RU: "Moscow"
US: "Washington"
GB: "London"
""")
print(vals, line)
print(axon.dumps(vals))


OrderedDict([('RU', 'Moscow'), ('US', 'Washington'), ('GB', 'London')]) 
--------------------------------
RU:"Moscow"
US:"Washington"
GB:"London"

Sequence of ordered dicts


In [26]:
vals = axon.loads("""
[RU:"Moscow" FR:"Paris"]
[CN:"Beijin" JP:"Tokio"]
[US:"Washington" CA:"Ottava"]
""")
print(vals, line)
print(axon.dumps(vals), line)
print(axon.dumps(vals, pretty=1))


[OrderedDict([('RU', 'Moscow'), ('FR', 'Paris')]), OrderedDict([('CN', 'Beijin'), ('JP', 'Tokio')]), OrderedDict([('US', 'Washington'), ('CA', 'Ottava')])] 
--------------------------------
[RU:"Moscow" FR:"Paris"]
[CN:"Beijin" JP:"Tokio"]
[US:"Washington" CA:"Ottava"] 
--------------------------------
[ FR: "Paris"
  RU: "Moscow"]
[ CN: "Beijin"
  JP: "Tokio"]
[ CA: "Ottava"
  US: "Washington"]

Sequence of dicts


In [27]:
vals = axon.loads("""
{RU:"Moscow" FR:"Paris"}
{CN:"Beigin" JP:"Tokio"}
{US:"Washington" CA:"Ottava"}
""")
print(vals, line)
print(axon.dumps(vals, sorted=1), line)
print(axon.dumps(vals, sorted=1, pretty=1))


[{'RU': 'Moscow', 'FR': 'Paris'}, {'JP': 'Tokio', 'CN': 'Beigin'}, {'US': 'Washington', 'CA': 'Ottava'}] 
--------------------------------
{FR:"Paris" RU:"Moscow"}
{CN:"Beigin" JP:"Tokio"}
{CA:"Ottava" US:"Washington"} 
--------------------------------
{ FR: "Paris"
  RU: "Moscow"}
{ CN: "Beigin"
  JP: "Tokio"}
{ CA: "Ottava"
  US: "Washington"}

Sequence of tuples


In [28]:
vals = axon.loads("""
("RU" "Moscow")
("US" "Washington")
("GB" "London")
""")
print(vals, line)
print(axon.dumps(vals))


[('RU', 'Moscow'), ('US', 'Washington'), ('GB', 'London')] 
--------------------------------
("RU" "Moscow")
("US" "Washington")
("GB" "London")

Sequence of lists


In [29]:
vals = axon.loads("""
["RU" "Moscow"]
["US" "Washington"]
["GB" "London"]
""")
print(vals, line)
print(axon.dumps(vals))


[['RU', 'Moscow'], ['US', 'Washington'], ['GB', 'London']] 
--------------------------------
["RU" "Moscow"]
["US" "Washington"]
["GB" "London"]

List of atomic values


In [30]:
vals = axon.loads("""
[1 3.14 1000D "abc абв" 2015-12-01 12:00-03:00 2015-12-01T12:00+03:00 ? ∞]
""")
print(vals, line)
print(axon.dumps(vals))


[[1, 3.14, Decimal('1000'), 'abc абв', datetime.date(2015, 12, 1), datetime.time(12, 0, tzinfo=datetime.timezone(datetime.timedelta(-1, 75600))), datetime.datetime(2015, 12, 1, 12, 0, tzinfo=datetime.timezone(datetime.timedelta(0, 10800))), nan, inf]] 
--------------------------------
[1 3.14 1000D "abc абв" 2015-12-01 12:00-03 2015-12-01T12:00+03 ? ∞]

List of nodes


In [31]:
vals = axon.loads("""
country{id:"RU" capital:"Moscow"}
country{id:"USA" capital:"Washington"}
country{id:"GB" capital:"London"}
""")
print(vals, line)
print(axon.dumps(vals), line)
print(axon.dumps(vals, pretty=1), line)
print(axon.dumps(vals, pretty=1, braces=1))


[country{id: 'RU', capital: 'Moscow'}, country{id: 'USA', capital: 'Washington'}, country{id: 'GB', capital: 'London'}] 
--------------------------------
country{id:"RU" capital:"Moscow"}
country{id:"USA" capital:"Washington"}
country{id:"GB" capital:"London"} 
--------------------------------
country
  id: "RU"
  capital: "Moscow"
country
  id: "USA"
  capital: "Washington"
country
  id: "GB"
  capital: "London" 
--------------------------------
country {
  id: "RU"
  capital: "Moscow"}
country {
  id: "USA"
  capital: "Washington"}
country {
  id: "GB"
  capital: "London"}

Sequence of objects


In [35]:
class Country:
    def __init__(self, id, capital):
        self.id = id
        self.capital = capital
    def __repr__(self):
        return "Country(id='{}', capital='{}')".format(self.id, self.capital)

@axon.reduce(Country)
def reduce_country(ob):
    return axon.node("country", axon.odict([("id",ob.id), ("capital",ob.capital)]))

@axon.factory("country")
def factory_country(map, seq):
    return Country(map["id"], map["capital"])

vals = axon.loads("""
country
  id:"RU"
  capital:"Moscow"
country
  id:"USA"
  capital:"Washington"
country
  id:"GB"
  capital:"London"
""", mode='strict')
print(vals, line)
print(axon.dumps(vals), line)
print(axon.dumps(vals, pretty=1, braces=1))


[Country(id='RU', capital='Moscow'), Country(id='USA', capital='Washington'), Country(id='GB', capital='London')] 
--------------------------------
country{id:"RU" capital:"Moscow"}
country{id:"USA" capital:"Washington"}
country{id:"GB" capital:"London"} 
--------------------------------
country {
  id: "RU"
  capital: "Moscow"}
country {
  id: "USA"
  capital: "Washington"}
country {
  id: "GB"
  capital: "London"}

In [ ]: