Re: Regarding autocalling (was Re: #def woes)
Tavis Rudd <[email protected]>
| Newsgroups | gmane.comp.python.cheetah |
|---|---|
| Message-ID | <[email protected]> |
I agree with you on this, but, as you said, it's too big of a change to rip it out. Btw, another way to turn it off, all the time rather than per template, is to define a subclass of Cheetah.Template that has a custom set of default compiler settings and then use that subclass instead of Cheetah.Template. On Thu, 21 May 2009, R. Tyler Ballance wrote: > On Thu, May 21, 2009 at 04:06:16PM -0700, [email protected] wrote: >> why do u not like autocalling? > > I suppose I should enumerate my objections to the use of autocalling and > why I harsh on it so much :) > > For what autocalling actually does, you can refer to this document: > http://www.cheetahtemplate.org/docs/users_guide_html_multipage/language.namemapper.autocalling.html > > My primary issue with autocalling is that I dislike the language being > "smart", whenever software tries to be "smart" it usually ends up > screwing things up. What makes programming languages important is that > they do not only what you tell them, but *only* what you tell them. > > Autocalling doesn't quite fit that mold, if you use the NameMapper to > execute this statement: > > $foo.bar > > Being a Python developer, I would expect this to me an attribute or > property access of "bar" from the object "foo". With autocalling > enabled, this isn't quite the case. > > Sure $foo.bar can equate to `foo.bar`, but it can also mean `foo['bar']` > or even `foo.bar()` > > > This can lead to subtle bugs (or lazy developers) if you're not aware > of this "smartness" on Cheetah's side of the fence. The "workarounds" > are to do any one of the following: > > * Compile your templates with --settings="useAutocalling=False" > * Use $getVar('foo.bar', autoCall=False) > * Add `#compiler-settings useAutocalling=False #end > compiler-settings` to your template > > That all said I'm not going to change the behavior because I'm aware of > how many Cheetah users depend on this functionality. > > > Cheers > >> On Thu, May 21, 2009 at 3:59 PM, R. Tyler Ballance <[email protected]> >> wrote: >> >> On Thu, May 21, 2009 at 03:52:08PM -0700, [email protected] >> wrote: >> > #def displayfloat($a) >> > $("%.2f"%$a)#slurp >> > #end def >> > #set showfloat = $getVar('displayfloat', autoCall=False) >> > >> > ## return a callable value for $name >> > #def callable($name) >> > #return $getVar($name, autoCall=False) >> > #end def >> > >> > #set showfloat =$callable('displayfloat') >> >> Dude you're stomping on builtins, you probably shouldn't do that: >> >> >>> callable >> <built-in function callable> >> >>> >> >> The first example you've got works fine: >> >> >>> from Cheetah.Template import Template >> >> t = Template('''#def displayfloat(a) >> ... $("%.2f"%$a)#slurp >> ... #end def >> ... #set showfloat = $getVar('displayfloat', >> autoCall=False) >> ... $showfloat(3.14) >> ... ''') >> >>> t.respond() >> u'3.14\n' >> >>> >> >> I'm not sure what you're doing, it could be due to overriding the >> builtin >> callable() function which NameMapper *does* use for lookups (stupid >> auto-calling), that's my only guess for why this is failing for you. >> > >> > both these options dont work >> > return str(compiled_tmpl) >> > File "/usr/lib/python2.5/site-packages/Cheetah/Template.py", line >> 997, >> > in __str__ >> > return getattr(self, mainMethName)() >> > File "<string>", line 3514, in respond >> > NotFound: cannot find 'showfloat' while searching for 'showfloat' >> > >> > On Thu, May 21, 2009 at 2:57 PM, R. Tyler Ballance >> <[email protected]> >> > wrote: >> > >> > On Thu, May 21, 2009 at 02:55:50PM -0700, >> [email protected] >> > wrote: >> > > #set $faa = self.foo >> > > >> > > This is cool calling with self. doesnt autocall >> > >> > It doesn't call because you're in the right hand operand (where >> you need >> > not use Cheetah syntax), and you're not using the '$'. When you >> use the >> > $, >> > that invokes the NameMapper which is responsible for autocalling. >> > >> > I personally prefer this option to $getVar() which feels >> distinctly >> > non-Pythonic ;) >> > >> > > On 5/20/09, Michael Higgins <[email protected]> wrote: >> > > >> > > On May 20, 2009, at 4:18 PM, R. Tyler Ballance wrote: >> > > >> > > I /think/ this should work: >> > > #set $faa = foo >> > > >> > > I'm not running the latest and greatest, but that doesn't >> work >> > for me. >> > > What *does* work for me is saying >> > > >> > > #set $faa = self.foo >> > > >> > > Exploiting self in such an unvarnished way seems like it >> might be >> > too >> > > close to relying on an implementation detail, but maybe >> it's >> > okay. I >> > > guess Cheetah already has to guarantee not to do >> name-mangling or >> > > something so that Python code can call Cheetah methods >> > properly... >> > > >> > > Alternatively, you can use: >> > > #set $faa = $foo >> > > >> > > If you compile the templates with autocalling disabled >> > > >> > > Personally, I'm not a huge fan of tweaking the compile >> settings a >> > lot >> > > for different templates. It's just one more thing that >> can >> > mysteriously >> > > break and take a long time to figure out. >> > > >> > > To answer mobiledreamer's (later) question about what a >> wrapper >> > function >> > > would look like: >> > > >> > > ## return a callable value for $name >> > > #def F($name) >> > > #return $getVar($name, autoCall=False) >> > > #end def >> > > >> > > #set $faa = $F('foo') >> > > >> > > ... assuming the self trick isn't the preferable style. >> (It's >> > certainly >> > > shorter!) >> > > >> > > Mike >> > > >> > > -- >> > > Bidegg worlds best auction site >> > > http://bidegg.com >> > >> > -- >> > -R. Tyler Ballance >> > Slide, Inc. >> > >> > -- >> > Bidegg worlds best auction site >> > http://bidegg.com >> >> -- >> -R. Tyler Ballance >> Slide, Inc. >> >> -- >> Bidegg worlds best auction site >> http://bidegg.com > > -- > -R. Tyler Ballance > Slide, Inc. > ------------------------------------------------------------------------------ Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT is a gathering of tech-side developers & brand creativity professionals. Meet the minds behind Google Creative Lab, Visual Complexity, Processing, & iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian Group, R/GA, & Big Spaceship. http://www.creativitycat.com