Debian Codegraph @

docs/dev_guide/design.rst

raw
1 Design Decisions and Tradeoffs
2 ==============================
3
4
5 Delimiters
6 ----------
7
8
9 One of the first decisions we encountered was which delimiter
10 syntax to use. We decided to follow Velocity's {$placeholder} and
11 {#directive} syntax because the former is widely used in other
12 languages for the same purpose, and the latter stands out in an
13 HTML or text document. We also implemented the
14 ``${longPlaceholder}`` syntax like the shells for cases where
15 Cheetah or you might be confused where a placeholder ends. Tavis
16 went ahead and made ``${longPlaceholder}`` and
17 ``$[longPlaceholder]`` interchangeable with it since it was trivial
18 to implement. Finally, the {#compiler} directive allows you to
19 change the delimiters if you don't like them or if they conflict
20 with the text in your document. (Obviously, if your document
21 contains a Perl program listing, you don't necessarily want to
22 backslash each and every {$} and {#}, do you?)
23
24 The choice of comment delimiters was more arbitrary. {##} and {#\*
25 ... \*#} doesn't match any language, but it's reminiscent of Python
26 and C while also being consistent with our "{#} is for directives"
27 convention.
28
29 We specifically chose { not} to use pseudo HTML tags for
30 placeholders and directives, as described more thoroughly in the
31 Cheetah Users' Guide introduction. Pseudo HTML tags may be easier
32 to see in a visual editor (supposedly), but in text editors they're
33 hard to distinguish from "real" HTML tags unless you look closely,
34 and they're many more keystrokes to type. Also, if you make a
35 mistake, the tag will show up as literal text in the rendered HTML
36 page where it will be easy to notice and eradicate, rather than
37 disappearing as bogus HTML tags do in browsers.
38
39 Late binding
40 ------------
41
42
43 One of Cheetah's unique features is the name mapper, which lets you
44 write {$a.b} without worrying much about the type of {a} or {b}.
45 Prior to version 0.9.7, Cheetah did the entire NameMapper lookup at
46 runtime. This provided maximum flexibility at the expense of speed.
47 Doing a NameMapper lookup is intrinsically more expensive than an
48 ordinary Python expression because Cheetah has to decide what type
49 of container {a} is, whether the the value is a function (autocall
50 it), issue the appropriate Python incantation to look up {b} in it,
51 autocall again if necessary, and then convert the result to a
52 string.
53
54 To maximize run-time (filling-time) performance, Cheetah 0.9.7
55 pushed much of this work back into the compiler. The compiler
56 looked up {a} in the searchList at compile time, noted its type,
57 and generated an eval'able Python expression based on that.
58
59 This approach had two significant drawbacks. What if {a} later
60 changes type before a template filling? Answer: unpredictable
61 exceptions occur. What if {a} does not exist in the searchList at
62 compile time? Answer: the template can't compile.
63
64 To prevent these catastrophes, users were required to prepopulate
65 the searchList before instantiating the template instance, and then
66 not to change {a}'s type. Static typing is repugnant in a dynamic
67 language like Python, and having to prepopulate the searchList made
68 certain usages impossible. For example, you couldn't instantiate
69 the template object without a searchList and then set {self}
70 attributes to specify the values.
71
72 After significant user complaints about the fragility of this
73 system, Tavis rewrote placeholder handling, and in version 0.9.8a3
74 (August 2001), Tavis moved the name mapper lookup back into
75 runtime. Performance wasn't crippled because he discovered that
76 writing a C version of the name mapper was easier than anticipated,
77 and the C version completed the lookup quickly. Now Cheetah had
78 "late binding", meaning the compiler does not look up {a} or care
79 whether it exists. This allows users to create {a} or change its
80 type anytime before a template filling.
81
82 The lesson we learned is that it's better to decide what you want
83 and then figure out how to do it, rather than assuming that certain
84 goals are unattainable due to performance considerations.
85
86 Caching framework
87 -----------------
88
89
90 Webware compatibility and the transaction framework
91 ---------------------------------------------------
92
93
94 Single inheritance
95 ------------------
96
97
98