def parse(template, context, _locals, timed, cache, repeat): # use of extensions to inject _locals _sandbox.extensions = _locals try: if timed and cache: t = timeit.Timer('tpl.render()', 'from __main__ import Template\ntpl = Template("{0}")\ncontext={1}'.format(template, str(context))) print '%.2f ms %s' % (1000 * t.timeit(100)/100, template) elif timed: t = timeit.Timer('Template("{0}").render()'.format(template), 'from __main__ import Template') print '%.2f ms %s' % (1000 * t.timeit(repeat)/repeat, template) else: t = Template(template) print '<!DOCTYPE html>\n'+etree.tostring(etree.fromstring(t.render(**context)), pretty_print=True) except Exception as e: print 'Exception rendering ', template print e
def parse(template, context, _locals, timed, cache, repeat): # use of extensions to inject _locals _sandbox.extensions = _locals try: if timed and cache: t = timeit.Timer( 'tpl.render()', 'from __main__ import Template\ntpl = Template("{0}")\ncontext={1}' .format(template, str(context))) print '%.2f ms %s' % (1000 * t.timeit(100) / 100, template) elif timed: t = timeit.Timer('Template("{0}").render()'.format(template), 'from __main__ import Template') print '%.2f ms %s' % (1000 * t.timeit(repeat) / repeat, template) else: t = Template(template) print(t.render(**context)) #print '<!DOCTYPE html>\n'+etree.tostring(etree.fromstring(t.render(**context)), pretty_print=True) except Exception as e: print 'Exception rendering ', template print e
def test_py_formatter(self): parsed, expected = self.t['py_formatter'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
def test_basic_multilinetext(self): parsed, expected = self.t['basic_multilinetext'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
def test_py_mixed_content(self): parsed, expected = self.t['py_mixed_content'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
def test_py_include(self): parsed, expected = self.t['py_include'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
def test_py_block_default(self): parsed, expected = self.t['py_block_default'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
def test_basic_tag_hashes(self): parsed, expected = self.t['basic_tag_hashes'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
def test_basic_ending_colon(self): parsed, expected = self.t['basic_ending_colon'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
def test_py_looping(self): parsed, expected = self.t['py_looping'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
if r is None: return '' py_id = id(self.py_q) py_parse = py_locals['__py_parse__'] doc_py(r, py_id, py_parse) return r.tostring() if __name__ == '__main__': import sys import codecs from time import time _f = sys.argv[1] #print parse(_f) t = Template(_f) if '-p' in sys.argv: def run(): for x in range(2000): t.render() import cProfile, pstats prof = cProfile.Profile() prof.run('run()') stats = pstats.Stats(prof) stats.strip_dirs() stats.sort_stats('cumulative') stats.print_stats(25) else: print t.render()
def test_basic_variable_indent(self): parsed, expected = self.t['basic_variable_indent'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
def parse(t, **kwargs): return Template(t).render(**kwargs)
def test_py_newline_var(self): parsed, expected = self.t['py_newline_var'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
def test_py_raise(self): parsed, expected = self.t['py_raise'] #TODO fix this test for 2.6 self.assertRaises(Exception, Template(parsed).render)
def test_py_complex3(self): parsed, expected = self.t['py_complex3'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
def test_py_ending_colon(self): parsed, expected = self.t['py_ending_colon'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())
def test_py_nested_for(self): parsed, expected = self.t['py_nested_for'] parsed = Template(parsed).render() self.assertEqual(parsed.strip(), expected.strip())