comparison examples/basic/run.py @ 3:518a8520a6e1 trunk

Added basic example.
author cmlenz
date Sat, 03 Jun 2006 12:28:53 +0000
parents
children 74cc70129d04
comparison
equal deleted inserted replaced
2:edbbe45da6e2 3:518a8520a6e1
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 from datetime import datetime, timedelta
5 import os
6 import sys
7
8 from markup.template import Context, TemplateLoader
9
10 def test():
11 base_path = os.path.dirname(os.path.abspath(__file__))
12 loader = TemplateLoader([os.path.join(base_path, 'common'),
13 os.path.join(base_path, 'module')],
14 auto_reload=True)
15
16 start = datetime.now()
17 tmpl = loader.load('test.html')
18 print ' --> parse stage: ', datetime.now() - start
19
20 ctxt = Context(hello='<world>', skin='default', hey='ZYX', bozz=None,
21 items=['Number %d' % num for num in range(1, 15)],
22 prefix='#')
23
24 print tmpl.generate(ctxt).render(method='html')
25
26 times = []
27 for i in range(100):
28 start = datetime.now()
29 list(tmpl.generate(ctxt))
30 sys.stdout.write('.')
31 sys.stdout.flush()
32 times.append(datetime.now() - start)
33 print
34
35 total_ms = sum([t.seconds * 1000 + t.microseconds for t in times])
36 print ' --> render stage: %s (avg), %s (min), %s (max)' % (
37 timedelta(microseconds=total_ms / len(times)),
38 timedelta(microseconds=min([t.seconds * 1000 + t.microseconds for t in times])),
39 timedelta(microseconds=max([t.seconds * 1000 + t.microseconds for t in times])))
40
41 if __name__ == '__main__':
42 if '-p' in sys.argv:
43 import hotshot, hotshot.stats
44 prof = hotshot.Profile("template.prof")
45 benchtime = prof.runcall(test)
46 stats = hotshot.stats.load("template.prof")
47 stats.strip_dirs()
48 stats.sort_stats('time', 'calls')
49 stats.print_stats()
50 else:
51 test()
Copyright (C) 2012-2017 Edgewall Software