In [1]:
from __future__ import print_function
from axon import loads, dumps
from pprint import pprint
There are two API functions loads/dumps for loading/dumping from/to unicode string.
By default loading and dumping are safe. By the word "safe" we mean that there is no user code is executed while loading and dumping. Unicode strings are converted only into python objects of given types. There is "unsafe" mode too. It allows to transform unicode string into user defined objects and to dump objects into unicode string under user control. But this is the topic of another post.
In [2]:
text = '''\
note {
from: "Pooh"
to: "Bee"
posted: 2006-08-15T17:30
heading: "Honey"
"Don't forget to get me honey!" }
'''
vals = loads(text)
Here vals is always list of objects.
In [3]:
ob = vals[0]
type(ob)
Out[3]:
We see that the message is converted to the instance of class Element. Attribute vals.mapping is dictionary containing objects's attributes:
In [5]:
print(type(ob.__attrs__))
print(ob.__attrs__)
Attributes of the object are accessable by methods get/set:
In [10]:
[(attr, getattr(ob, attr)) for attr in ob.__attrs__]
Out[10]:
Element objects has content - list of values. They are accessible by python's sequence protocol. In our case the first value is the message body of the note.
In [11]:
print(ob[0])
For dumping objects there are three modes. First mode is compact:
In [12]:
print(dumps([ob]))
Second mode is pretty dumping mode with indentations and without braces:
In [13]:
print(dumps([ob], pretty=1))
Third mode is pretty dumping mode with indentation and braces:
In [14]:
print(dumps([ob], pretty=1, braces=1))
At the end let's consider JSON-like representation too:
In [16]:
text = """\
{note: {
from: "Pooh"
to: "Bee"
posted: 2006-08-15T17:30
heading: "Honey"
body: "Don't forget to get me honey!"
}}
"""
vals = loads(text)
It has converted into python dicts:
In [17]:
pprint(vals)
Compact dump is pretty small in size.
In [18]:
print(dumps(vals))
Dumping in pretty mode is also pretty formatted.
In [19]:
print(dumps(vals, pretty=1))
JSON-like objects are pretty dumps only in indented form with braces.
Let's consider now JSON-like example with crossreferences and datetimes:
In [20]:
text = '''\
{
topic: [
&1 {python: "Python related"}
&2 {axon: "AXON related"}
&3 {json: "JSON related"}
]
posts: [
{ id: 1
topic: *1
date: 2012-01-02T12:15+03
body:"..." }
{ id: 2
topic: *2
date: 2012-01-12T09:25+03
body:"..." }
{ id: 3
topic: *3
date: 2012-02-08T10:35+03
body:"..." }
]
}
'''
vals = loads(text)
pprint(vals)
It's easy to see that crossreference links just works:
In [21]:
assert vals[0]['topic'][0] is vals[0]['posts'][0]['topic']
assert vals[0]['topic'][1] is vals[0]['posts'][1]['topic']
assert vals[0]['topic'][2] is vals[0]['posts'][2]['topic']
Pretty dump looks like this one:
In [22]:
print(dumps(vals, pretty=1, crossref=1, sorted=0))
Note that
sortedparameter defines whether to sort keys in dict.
In [23]:
text = '''\
html {
xmlns:"http://www.w3.org/1999/xhtml"
head {
title {"Form Example"}
link {
rel:"stylesheet"
href: "formstyle.css"
type: "text/css" }}
body {
h1 {"Form Example"}
form {
action: "sample.py"
div {
class: "formin"
"(a)"
input {type:"text" name:"text1" value:"A textbox"}}
div {
class: "formin"
"(b)"
input {type:"text" size:6 maxlength:10 name:"text2"}}
div {
class: "formb"
"(c)"
input {type:"submit" value:"Go!"}}
}
}
}
'''
vals = loads(text)
val = vals[0]
print(val)
Let's examine the value:
In [25]:
print(type(val))
print(val.__attrs__)
head, body = val[0], val[1]
In [26]:
print(type(head))
title, link = head
print(title)
print(link)
In [36]:
h1, form = body
print(h1)
In [37]:
print(form.__vals__)
div1, div2, div3 = form
In [38]:
print(div1.__attrs__)
label1, input1 = div1
print(label1)
print(input1)
In [40]:
print(div2.__attrs__)
label2, input2 = div2
print(label2)
print(input2)
In [41]:
print(type(div3))
print(div3.__attrs__)
label3, input3 = div3
print(label3)
print(input3)
In [42]:
print(dumps([val], pretty=1))
In [43]:
print(dumps([val], pretty=1, braces=1))
Let's consider simple tabular dataset:
In [44]:
text = '''\
dataset {
fields: ("id" "date" "time" "territory_id" "A" "B" "C")
(1 2012-01-10 12:35 17 3.14 22 33500)
(2 2012-01-11 13:05 27 1.25 32 11500)
(3 2012-01-12 10:45 -17 -2.26 -12 44700)
}
'''
ob = loads(text)[0]
print(ob.__tag__)
pprint(ob.__attrs__)
pprint(ob.__vals__, width=132)
print("\nPretty form of dataset:")
print(dumps([ob], pretty=1, hsize=10))
In [47]:
from collections import namedtuple
Datarow = namedtuple("Datarow", ob.fields)
rows = []
for line in ob:
print(type(line), line)
rows.append(Datarow(*line))
print("\n")
for row in rows:
print(type(row), row)
In [ ]: