Mercurial > genshi > genshi-test
annotate examples/basic/run.py @ 804:cce33406c1cf stable-0.5.x
Ported [1005] to 0.5.x branch.
author | cmlenz |
---|---|
date | Thu, 05 Mar 2009 10:06:45 +0000 |
parents | 24757b771651 |
children |
rev | line source |
---|---|
3 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 import os | |
5 import sys | |
73 | 6 import time |
3 | 7 |
230 | 8 from genshi.template import TemplateLoader |
3 | 9 |
10 def test(): | |
11 base_path = os.path.dirname(os.path.abspath(__file__)) | |
21
eca77129518a
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
17
diff
changeset
|
12 loader = TemplateLoader([base_path], auto_reload=True) |
3 | 13 |
73 | 14 start = time.clock() |
3 | 15 tmpl = loader.load('test.html') |
73 | 16 print ' --> parse stage: %.4f ms' % ((time.clock() - start) * 1000) |
3 | 17 |
17
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
3
diff
changeset
|
18 data = dict(hello='<world>', skin='default', hey='ZYX', bozz=None, |
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
3
diff
changeset
|
19 items=['Number %d' % num for num in range(1, 15)], |
ad63ad459524
Refactoring to address #6: all match templates are now processed by a single filter, which means that match templates added by included templates are properly applied. A side effect of this refactoring is that `Context` objects may not be reused across multiple template processing runs.
cmlenz
parents:
3
diff
changeset
|
20 prefix='#') |
3 | 21 |
149
7306bf730ff3
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
73
diff
changeset
|
22 print tmpl.generate(**data).render(method='html') |
3 | 23 |
24 times = [] | |
73 | 25 for i in range(1000): |
26 start = time.clock() | |
149
7306bf730ff3
`Template.generate()` now accepts the context data as keyword arguments, so that you don't have to import the `Context` class every time you want to pass data into a template.
cmlenz
parents:
73
diff
changeset
|
27 list(tmpl.generate(**data)) |
73 | 28 times.append(time.clock() - start) |
3 | 29 sys.stdout.write('.') |
30 sys.stdout.flush() | |
31 print | |
32 | |
73 | 33 print ' --> render stage: %s ms (average)' % ( |
34 (sum(times) / len(times) * 1000)) | |
3 | 35 |
36 if __name__ == '__main__': | |
37 if '-p' in sys.argv: | |
38 import hotshot, hotshot.stats | |
39 prof = hotshot.Profile("template.prof") | |
40 benchtime = prof.runcall(test) | |
41 stats = hotshot.stats.load("template.prof") | |
42 stats.strip_dirs() | |
43 stats.sort_stats('time', 'calls') | |
44 stats.print_stats() | |
45 else: | |
46 test() |