Package gchecky :: Module tools
[hide private]
[frames] | no frames]

Module tools

source code


The module provides two classes encoder and decoder that allow to
serialize and deserialize python-ish PODs into/from XML.
Note that data should be simple:
None, True, False, strings, lists, tupls, dicts
Anything other than this will trigger an error.

Also note that any circular references in the data will also trigger an error,
so please do not try to serialize something like:
>>> a = []
>>> a.append(a)
>>> a
[[...]]

Important notes:
- tuples are treated as lists and deserialized into lists.
- any empty list, tuple or dictionary is deserialized into None.

TODO: Expand the notes on how exactly the data values are serialized.

Some doctests:

>>> def test_enc_dec(data, return_res=False):
...   from xml.dom.minidom import parseString
...   doc = parseString('<?xml version="1.0"?><dummy/>')
...   encoder().serialize(data, doc.documentElement)
...   xml = doc.toprettyxml('  ')
...   data2 = decoder().deserialize(doc.documentElement)
...   if data2 != data:
...       msg = '''--- Expected: ---
...                 %s
...                 --- Got: ---
...                 %s
...                 === Xml: ===
...                 %s
...       ''' % (data, data2, xml)
...       if return_res:
...          return data2
...       print msg

>>> test_enc_dec(None)
>>> test_enc_dec(True)
>>> test_enc_dec(False)
>>> test_enc_dec('string')
>>> test_enc_dec(u'string')
>>> test_enc_dec({'a':'b'})
>>> test_enc_dec([1,2])
>>> test_enc_dec(['1',2])
>>> test_enc_dec([1])
>>> test_enc_dec({'':'aa'})
>>> test_enc_dec(['_'])
>>> test_enc_dec(['aa',['bb','cc'],[None], None, ['_']])
>>> test_enc_dec([[False]])
>>> test_enc_dec([[False], None])
>>> test_enc_dec([False, True, [False], [[True]], [None]])
>>> test_enc_dec({'vasya':['aa', 'bb']})
>>> test_enc_dec({'name':['Peter', 'Mary'], 'age':[11, 15]})

To fix:
>>> test_enc_dec([], return_res=True) != None
False
>>> test_enc_dec({}, return_res=True) != None
False



Classes [hide private]
  decoder
  encoder
Variables [hide private]
  TRUE_LABEL = u'True'
  FALSE_LABEL = u'False'