1 # vim: set encoding=utf-8 et sw=4 sts=4 :
4 from pymin import seqtools
6 r"UTF-8 encoded CSV serializer."
8 def serialize(obj, output=None):
9 r"""serialize(obj[, output]) -> None/unicode string
11 Serialize the object obj to a UTF-8 encoded CSV string. If output
12 is not None, it's used as a file object to store the string. If it's
13 None, the string is returned.
15 obj is expected to be a sequence of sequences, i.e. a list of rows.
21 from cStringIO import StringIO
23 from StringIO import StringIO
25 ucsv.writer(output).writerows(seqtools.as_table(obj))
27 return output.getvalue()
30 if __name__ == '__main__':
32 from seqtools import Sequence
35 r"""Host(name, ip, mac) -> Host instance :: Class representing a host.
37 name - Host name, should be a fully qualified name, but no checks are done.
38 ip - IP assigned to the hostname.
39 mac - MAC address to associate to the hostname.
42 def __init__(self, name, ip, mac):
43 r"Initialize Host object, see class documentation for details."
49 return (self.name, self.ip, self.mac)
51 def __unicode__(self):
56 print serialize("lala")
58 print serialize(u"lala")
60 print serialize([1, 2])
62 print serialize(["lala", "lala"])
64 print serialize([u"lala", u"lala"])
66 h = Host('name', 'ip', 'mac')
69 print serialize(dict(a=1, b=2))
71 print serialize([[1, 2, 3], [7, 4, 2]])
73 print serialize([["adfj", "jdfhk"], ["alskdjal", "1uas"]])
75 print serialize([[u"adfj", u"jdfhk"], [u"alskdjal", u"1uas"]])
77 print serialize([h, h])
81 serialize([h, h], sys.stdout)