view 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 source

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})