diff --git a/json2html/jsonconv.py b/json2html/jsonconv.py index b724aec..c4053ae 100644 --- a/json2html/jsonconv.py +++ b/json2html/jsonconv.py @@ -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 += '' - converted_output += '' + ''.join(column_headers) + '' - converted_output += '' - converted_output += '' - for list_entry in list_input: - converted_output += '' - converted_output += ''.join([self.convert_json_node(list_entry[column_header]) for column_header in - column_headers]) - converted_output += '' - converted_output += '' - converted_output += '' - 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 = '' - return converted_output + header = '%s' %(''.join(column_headers)) + body = '%s' %( + '%s' %(''.join([ + '%s' %(''.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' %(self.table_init_markup, header, body) + + #no clubbing required here - column headers are not consistent across list + return '' %( + '
  • '.join([self.convert_json_node(child) for child in list_input]) + ) def convert_object(self, json_input): """ @@ -163,15 +159,15 @@ def convert_object(self, json_input): """ if not json_input: return "" #avoid empty tables - converted_output = self.table_init_markup + "" - converted_output += "".join([ - "%s%s" %( - self.convert_json_node(k), - self.convert_json_node(v) - ) - for k, v in json_input.items() - ]) - converted_output += '' - return converted_output + return "%s%s" %( + self.table_init_markup, + "".join([ + "%s%s" %( + self.convert_json_node(k), + self.convert_json_node(v) + ) + for k, v in json_input.items() + ]) + ) json2html = Json2Html() diff --git a/test/benchmark.py b/test/benchmark.py new file mode 100644 index 0000000..1e78700 --- /dev/null +++ b/test/benchmark.py @@ -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) \ No newline at end of file diff --git a/test/run_tests.py b/test/run_tests.py index 9ba80a7..82cbb0d 100644 --- a/test/run_tests.py +++ b/test/run_tests.py @@ -136,12 +136,14 @@ def items(self): u'
    onetwo
    • 1
    • 2
    blübi
    ' ) #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'
    onetwo
    • 1
    • 2
    blübi
    foobar
    ' + converted, + u'
    onetwo
    • 1
    • 2
    blübi
    foobar
    ', + converted ) #not clubbed, second element has different keys self.assertEqual(