AXON is a "better" version of YAML

  |   Source

This post continue the series about AXON and pyaxon.

YAML combines orthogonal features that are suitable both for markup and serialization. AXON supports a sort of YAML-like formatting without of braces only for named complex values (mappings, sequences, elements, instances). This kind of formatting makes easy reading and writing AXON text by humans.

Let's explain this by simple example.

In [1]:
from __future__ import print_function, unicode_literals
import axon
from pprint import pprint

Here is an example of AXON text.

In [2]:
axon_text1 = '''
person {
  name: "John Smith"
  age: 25
  address {
      { type: "home"
        street:"21 2nd Street" city:"New York" state:"NY"
      }
      { type:"current"
        street:"1410 NE Campus Parkway" city:"Seattle" state:"WA"
      }
  }
  phone {
    { type: home number: "212-555-1234" }
    { type: fax number: "646-555-4567" }
  }
}
'''
vals1 = axon.loads(axon_text1)
print(vals1)
[element(u'person', {u'age': 25, u'name': u'John Smith'}, [sequence(u'address', [{u'city': u'New York', u'state': u'NY', u'street': u'21 2nd Street', u'type': u'home'}, {u'city': u'Seattle', u'state': u'WA', u'street': u'1410 NE Campus Parkway', u'type': u'current'}]), sequence(u'phone', [{u'type': u'home', u'number': u'212-555-1234'}, {u'type': u'fax', u'number': u'646-555-4567'}])])]

Here is a compact form of the AXON text. It's useful for saving space.

In [3]:
print(axon.dumps(vals1))
person{age:25 name:"John Smith" address{{city:"New York" state:"NY" street:"21 2nd Street" type:"home"} {city:"Seattle" state:"WA" street:"1410 NE Campus Parkway" type:"current"}} phone{{number:"212-555-1234" type:"home"} {number:"646-555-4567" type:"fax"}}}

Here is an indented formatting of the AXON text without of braces. This form is easier to read by a human.

In [4]:
print(axon.dumps(vals1, pretty=1))
person:
  age: 25
  name: "John Smith"
  address:
    { city: "New York"
      state: "NY"
      street: "21 2nd Street"
      type: "home"}
    { city: "Seattle"
      state: "WA"
      street: "1410 NE Campus Parkway"
      type: "current"}
  phone:
    { number: "212-555-1234"
      type: "home"}
    { number: "646-555-4567"
      type: "fax"}

There is a simple idea behind this kind of formatting.

All values inside of the named complex value have the same indentation bigger than indentation of its name.

This rule of formatting is similar to syntactic rule for statements in python.

Here is also indented formatting with braces:

In [5]:
print(axon.dumps(vals1, pretty=1, braces=1))
person {
  age: 25
  name: "John Smith"
  address {
    { city: "New York"
      state: "NY"
      street: "21 2nd Street"
      type: "home"}
    { city: "Seattle"
      state: "WA"
      street: "1410 NE Campus Parkway"
      type: "current"}}
  phone {
    { number: "212-555-1234"
      type: "home"}
    { number: "646-555-4567"
      type: "fax"}}}

It's easy to see that indented formatting with of braces can be rewrited as indented formatting without braces:

  1. replace any header like "name {" with "name:";
  2. remove closing "}" from any named complex value.

AXON allows also to mix indented formatting without braces and formatting with braces:

In [6]:
axon_text2 = '''
person {
  age: 25
  name: "John Smith"
  address:
    { city: "New York"
      state: "NY"
      street: "21 2nd Street"
      type: "home"}
    { city: "Seattle"
      state: "WA"
      street: "1410 NE Campus Parkway"
      type: "current"}
  phone:
    { number: "212-555-1234"
      type: "home"}
    { number: "646-555-4567"
      type: "fax"}
}
'''
vals2 = axon.loads(axon_text2)
print(vals2)
[element(u'person', {u'age': 25, u'name': u'John Smith'}, [sequence(u'address', [{u'city': u'New York', u'state': u'NY', u'street': u'21 2nd Street', u'type': u'home'}, {u'city': u'Seattle', u'state': u'WA', u'street': u'1410 NE Campus Parkway', u'type': u'current'}]), sequence(u'phone', [{u'type': u'home', u'number': u'212-555-1234'}, {u'type': u'fax', u'number': u'646-555-4567'}])])]

In [7]:
print(axon.dumps(vals2, pretty=1))
person:
  age: 25
  name: "John Smith"
  address:
    { city: "New York"
      state: "NY"
      street: "21 2nd Street"
      type: "home"}
    { city: "Seattle"
      state: "WA"
      street: "1410 NE Campus Parkway"
      type: "current"}
  phone:
    { number: "212-555-1234"
      type: "home"}
    { number: "646-555-4567"
      type: "fax"}

In [7]:
 
Comments powered by Disqus
Share