Mercurial > hg > webcalc
diff webcalc/formatters.py @ 0:1eea6356d2e5
initial import of webcalc
author | k0s <k0scist@gmail.com> |
---|---|
date | Mon, 07 Sep 2009 15:09:03 -0400 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webcalc/formatters.py Mon Sep 07 15:09:03 2009 -0400 @@ -0,0 +1,20 @@ +from StringIO import StringIO + +def CSVformat(values): + if not values: + return '' + keys = sorted([key for key in values[0].keys() + if key != 'result']) + keys.append('result') + + buffer = StringIO() + if len(keys) > 1: + print >> buffer, ','.join(keys) + for value in values: + print >> buffer, ','.join([str(value[key]) + for key in keys]) + return buffer.getvalue() + +def JSONformat(values): + import simplejson + return simplejson.dumps({'values': values})