view webcalc/formatters.py @ 1:12ac99c240ca default tip

* add documentation * add 1..10 autostep feature * bump version
author Jeff Hammel <k0scist@gmail.com>
date Wed, 10 Mar 2010 17:48:11 -0500
parents 1eea6356d2e5
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})