annotate examples/basic/run.py @ 21:b4d17897d053 trunk

* Include paths are now interpreted relative to the path of the including template. Closes #3. * The filename is now included as first item in the `pos` tuple of stream events. * Simplified the "basic" example so that it actually ''is'' basic. * Added a more complex example using nested relative includes in [source:/trunk/examples/includes/ examples/includes].
author cmlenz
date Tue, 20 Jun 2006 13:05:37 +0000
parents 74cc70129d04
children 1da51d718391
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 from datetime import datetime, timedelta
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
5 import os
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
6 import sys
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
7
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
8 from markup.template import Context, TemplateLoader
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
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
14 start = datetime.now()
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
15 tmpl = loader.load('test.html')
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
16 print ' --> parse stage: ', datetime.now() - start
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
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
22 print tmpl.generate(Context(**data)).render(method='html')
3
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
23
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
24 times = []
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
25 for i in range(100):
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
26 start = datetime.now()
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
27 list(tmpl.generate(Context(**data)))
3
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
28 sys.stdout.write('.')
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
29 sys.stdout.flush()
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
30 times.append(datetime.now() - start)
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
31 print
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
32
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
33 total_ms = sum([t.seconds * 1000 + t.microseconds for t in times])
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
34 print ' --> render stage: %s (avg), %s (min), %s (max)' % (
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
35 timedelta(microseconds=total_ms / len(times)),
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
36 timedelta(microseconds=min([t.seconds * 1000 + t.microseconds for t in times])),
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
37 timedelta(microseconds=max([t.seconds * 1000 + t.microseconds for t in times])))
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
38
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
39 if __name__ == '__main__':
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
40 if '-p' in sys.argv:
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
41 import hotshot, hotshot.stats
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
42 prof = hotshot.Profile("template.prof")
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
43 benchtime = prof.runcall(test)
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
44 stats = hotshot.stats.load("template.prof")
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
45 stats.strip_dirs()
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
46 stats.sort_stats('time', 'calls')
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
47 stats.print_stats()
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
48 else:
518a8520a6e1 Added basic example.
cmlenz
parents:
diff changeset
49 test()
Copyright (C) 2012-2017 Edgewall Software