Skip to content
Open
56 changes: 26 additions & 30 deletions json2html/jsonconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,22 @@ def convert_list(self, list_input):
if self.clubbing:
column_headers = self.column_headers_from_list_of_dicts(list_input)
if column_headers is not None:
converted_output += self.table_init_markup
converted_output += '<thead>'
converted_output += '<tr><th>' + '</th><th>'.join(column_headers) + '</th></tr>'
converted_output += '</thead>'
converted_output += '<tbody>'
for list_entry in list_input:
converted_output += '<tr><td>'
converted_output += '</td><td>'.join([self.convert_json_node(list_entry[column_header]) for column_header in
column_headers])
converted_output += '</td></tr>'
converted_output += '</tbody>'
converted_output += '</table>'
return converted_output

#so you don't want or need clubbing eh? This makes @muellermichel very sad... ;(
#alright, let's fall back to a basic list here...
converted_output = '<ul><li>'
converted_output += '</li><li>'.join([self.convert_json_node(child) for child in list_input])
converted_output += '</li></ul>'
return converted_output
header = '<thead><tr><th>%s</th></tr></thead>' %('</th><th>'.join(column_headers))
body = '<tbody>%s</tbody>' %(
'<tr>%s</tr>' %('</tr><tr>'.join([
'<td>%s</td>' %('</td><td>'.join([
self.convert_json_node(list_entry[column_header])
for column_header in column_headers
]))
for list_entry in list_input
]))
)
return '%s%s%s</table>' %(self.table_init_markup, header, body)

#no clubbing required here - column headers are not consistent across list
return '<ul><li>%s</li></ul>' %(
'</li><li>'.join([self.convert_json_node(child) for child in list_input])
)

def convert_object(self, json_input):
"""
Expand All @@ -163,15 +159,15 @@ def convert_object(self, json_input):
"""
if not json_input:
return "" #avoid empty tables
converted_output = self.table_init_markup + "<tr>"
converted_output += "</tr><tr>".join([
"<th>%s</th><td>%s</td>" %(
self.convert_json_node(k),
self.convert_json_node(v)
)
for k, v in json_input.items()
])
converted_output += '</tr></table>'
return converted_output
return "%s<tr>%s</tr></table>" %(
self.table_init_markup,
"</tr><tr>".join([
"<th>%s</th><td>%s</td>" %(
self.convert_json_node(k),
self.convert_json_node(v)
)
for k, v in json_input.items()
])
)

json2html = Json2Html()
33 changes: 33 additions & 0 deletions test/benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
import os, sys

lib_path = os.path.abspath(os.path.join('..'))
sys.path.append(lib_path)

from functools import wraps
from time import time
from json2html import *

def timing(f):
@wraps(f)
def wrap(*args, **kw):
ts = time()
result = f(*args, **kw)
te = time()
print 'func:%r args:[%r, %r] took: %2.4f sec' % \
(f.__name__, args, kw, te-ts)
return result
return wrap

@timing
def run(nesting=1000):
benchdata = {}
current_head = benchdata
for i in xrange(nesting):
current_head["test"] = {}
current_head = current_head["test"]
current_head["finally"] = "glob"
json2html.convert(benchdata)

sys.setrecursionlimit(100000)
run(int(sys.argv[1]) if len(sys.argv) > 1 else 1000)
12 changes: 7 additions & 5 deletions test/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,14 @@ def items(self):
u'<table border="1"><thead><tr><th>one</th><th>two</th></tr></thead><tbody><tr><td><ul><li>1</li><li>2</li></ul></td><td>blübi</td></tr></tbody></table>'
)
#clubbed with two elements
converted = json2html.convert([
binary_dict([1, 2], u"blübi"),
binary_dict("foo", "bar")
])
self.assertEqual(
json2html.convert([
binary_dict([1, 2], u"blübi"),
binary_dict("foo", "bar")
]),
u'<table border="1"><thead><tr><th>one</th><th>two</th></tr></thead><tbody><tr><td><ul><li>1</li><li>2</li></ul></td><td>blübi</td></tr><tr><td>foo</td><td>bar</td></tr></tbody></table>'
converted,
u'<table border="1"><thead><tr><th>one</th><th>two</th></tr></thead><tbody><tr><td><ul><li>1</li><li>2</li></ul></td><td>blübi</td></tr><tr><td>foo</td><td>bar</td></tr></tbody></table>',
converted
)
#not clubbed, second element has different keys
self.assertEqual(
Expand Down