annotate examples/basic/run.py @ 912:c74a141a48ff experimental-py3k

py3k branch: add 2to3 build infrastructure to setup.py (this pulls the tests into the source distribution so that tests can be run after building with 2to3)
author hodgestar
date Sun, 24 Oct 2010 21:09:36 +0000
parents 84168828b074
children
rev   line source
3
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
1 #!/usr/bin/python
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
2 # -*- coding: utf-8 -*-
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
3
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
4 import os
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
5 import sys
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 21
diff changeset
6 import time
3
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
7
230
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 149
diff changeset
8 from genshi.template import TemplateLoader
3
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
9
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
10 def test():
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
11 base_path = os.path.dirname(os.path.abspath(__file__))
21
b4d17897d053 * 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
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
13
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 21
diff changeset
14 start = time.clock()
3
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
15 tmpl = loader.load('test.html')
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 21
diff changeset
16 print ' --> parse stage: %.4f ms' % ((time.clock() - start) * 1000)
3
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
17
17
74cc70129d04 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,
74cc70129d04 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)],
74cc70129d04 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
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
21
149
537f819c547b `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
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
23
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
24 times = []
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 21
diff changeset
25 for i in range(1000):
1da51d718391 Some more performance tweaks.
cmlenz
parents: 21
diff changeset
26 start = time.clock()
149
537f819c547b `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
1da51d718391 Some more performance tweaks.
cmlenz
parents: 21
diff changeset
28 times.append(time.clock() - start)
3
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
29 sys.stdout.write('.')
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
30 sys.stdout.flush()
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
31 print
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
32
73
1da51d718391 Some more performance tweaks.
cmlenz
parents: 21
diff changeset
33 print ' --> render stage: %s ms (average)' % (
1da51d718391 Some more performance tweaks.
cmlenz
parents: 21
diff changeset
34 (sum(times) / len(times) * 1000))
3
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
35
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
36 if __name__ == '__main__':
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
37 if '-p' in sys.argv:
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
38 import hotshot, hotshot.stats
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
39 prof = hotshot.Profile("template.prof")
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
40 benchtime = prof.runcall(test)
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
41 stats = hotshot.stats.load("template.prof")
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
42 stats.strip_dirs()
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
43 stats.sort_stats('time', 'calls')
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
44 stats.print_stats()
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
45 else:
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
46 test()
Copyright (C) 2012-2017 Edgewall Software