RE: [Zope] Use of lambda expression in DTML - Result!
Thanks for helping me solve this one. In the end I did the following: 1) hacked DocumentTemplate/DT_Util.py to include "filter" in the __builtins__ 2) Installed PythonMethods and called pruneTree with parameter 'self' and content return _.filter( lambda x: x.getProperty('pruneFromTree',0)==0 , self.objectValues(['Folder']) ) 3) The new tag reads dtml-tree name="Outline" leaves=dtcTemplate branches="pruneTree" Chris McDonough wrote: |If it's something you want badly (and if you haven't already) add it as |a feature request to the Collector on Zope.org. Once other folks get |past the knee-jerk reaction, maybe it'll sink in as a good thing. In |the meantime, you can add it yourself if you like. Good idea - I'll do that. |Hopefully, you'll be so kind as to forgive me. That's easy - as long as you keep writing HowTos of the same quality as "Zope installation and upgrade" and "Zope enlightenment by understanding object orientation"... :-) |After seeing people post |DTML code to the list that they need debugged that computes pay rates, |has the shape of an arrowhead and scrolls past the maximum character |length of my email client horizontally, I'm a little leery of adding |anything whatsoever to DTML, as I think the rest of the folks at DC |are. Reducing the complexity of DTML is a good thing, however |it can be accomplished. Agreed. You can do some very powerful things with the lamda expressions, but they very quickly become unwieldy if used inappropriately, so I think people are right to be wary. Having said that, any sort of block structured language has the same pitfalls - e.g. Smalltalk, Self, even Java's anonymous classes could be used to detrimental effect. I prefer to leave this stuff to style and convention ("good practice") than prescribe them entirely. Thanks again, Nick. ___ Zope maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] Use of lambda expression in DTML
- Original Message - From: "Lalo Martins" [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Monday, May 29, 2000 11:40 PM Subject: Re: [Zope] Use of lambda expression in DTML On Mon, May 29, 2000 at 10:58:54PM -0400, Chris McDonough wrote: dtml-in "_.map (lambda item: Catalog.getobject (item.data_record_id_), Catalog (REQUEST)" How could someone understand this when you're finished with it? Perfectly clearly for anyone used to functional programming; I'd even say, clear on first sight. A lot easier to understand and maintain than a separate method. Assuming I'm reading this correctly, I think I'd probably express this as: dtml-in "Catalog(REQUEST)" dtml-with "Catalog.getobject(data_record_id_)" ...foo... /dtml-with /dtml-in BTW, I agree that exprs should be a part of DTML. You don't want to put significant program logic in DTML, but there are many places in which exprs are useful: Total: dtml-var "subtotal + shipping" I'll grant that you can set up all of the variables you need via a PythonMethod beforehand, but things like this can be nice and convenient... Any power that can be given to PythonMethods securely is a boon. Kevin ___ Zope maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] Use of lambda expression in DTML
In brief: I get a NameError when invoking "filter(...)" from DTML. I thought this was a built-in python method, so I'm a bit puzzled. Python's ideas of 'built-in' is simply any method in the module stored in the global variable called __builtins__. Any code executed with a non-standard global variable space may or may not have the same set of builtins as normal Python code. Unfortunately Zope does not expose all of the 'builtin' python methods to DMTL expressions. Some obviously need to be cut out (such as 'open') for security reasons, but the reasons why others were dropped are less obvious. If you really need any of the missing builtins back in you have a couple of choices. Both require you have full access to your Zope system. If you want the function available throughout your Zope system simply edit DocumentTemplate\DT_Util.py. Look for the list beginning 'None', 'abs', 'chr' and simply add 'filter' into the list. Alternatively if you dont want to patch Zope directly, create an external python method that exposes filter. 5) In the dtml-tree tag, I change it to: dtml-tree Technical leaves=dtcTemplate branches_expr="filter( lambda f: not f.hasProperty('blockTreeBranching', false), objectValues(['Folder']) )" which, all things equal, should DoTheRightThang. Even if you make the filter function visible as described above, this still won't quite work. For example the method 'objectValues' is not in scope within the function. The way around this is to pass in an extra parameter _vars. Try (untested): branches_expr="filter( lambda f, _vars=_vars: not f.hasProperty('blockTreeBranching', 0), objectValues(['Folder']) )" and it might work. Alternatively use something like a PythonMethod, or even a DTML method and simply write the filter loop out in full using 'for' or 'dtml- in'. -- Duncan Booth [EMAIL PROTECTED] int month(char *p){return(124864/((p[0]+p[1]-p[2]0x1f)+1)%12)["\5\x8\3" "\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure? http://dales.rmplc.co.uk/Duncan ___ Zope maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] Use of lambda expression in DTML
Chris McDonough wrote Nope. Lamba, filter, reduce, and map should IMHO not be part of DTML. Actually, exprs probably shouldn't be in there in the first place. People are trying to use DTML as a way to process non-UI elements. This is a slippery slope, and leads to something like PHP or ASP. My opinion is this: Generate and format your HTML with DTML, and do everything else in Python. This gives you the added benefit that you can change the return values of functions by changing the Python code without having to muck with DTML, which is painful any way you look at it. Until PythonMethods are back in the distribution, you can't stop people from using DTML for this stuff - the only alternative is to give everyone who ever needs to do something more than simple HTML access to the file system. I can't see that happening on zope.org, or many other sites. I agree whole-heartedly that DTML's expr syntax is nightmarish and needs to be deprecated - but right now there's nothing to replace it with. As far as the issue at hand - whether lambda/map/reduce/filter should be allowed - the simplest way to enable them for PythonMethods also enables them for dtml-expr. Anthony -- Anthony Baxter [EMAIL PROTECTED] It's never too late to have a happy childhood. ___ Zope maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] Use of lambda expression in DTML
On Tue, May 30, 2000 at 10:28:42PM +0200, you wrote: Minor note: "lambda" is not a builtin but part of the syntax (a keyword). I am sure, they will work in expressions. Uh. Of course that is correct. []s, |alo + -- Hack and Roll ( http://www.hackandroll.org ) News for, uh, whatever it is that we are. http://zope.gf.com.br/lalo mailto:[EMAIL PROTECTED] pgp key: http://zope.gf.com.br/lalo/pessoal/pgp Brazil of Darkness (RPG)--- http://zope.gf.com.br/BroDar ___ Zope maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] Use of lambda expression in DTML
Nick Drew wrote: |I can see that argument... it depends on the reader, I suppose. I |wouldn't complain much actually if the Python code had functional stuff |in it. It's having it in DTML that bugs me, for reasons that |have to do |with separating HTML-like stuff from the stuff that actually runs the |site under the hood. I take your point, but it's making an assumption that map, filter, and lambda are going to be used for business logic. In my case (see original topic) would you argue that what I'm trying to do is business logic or presentation? Many apologies, I caught the thread at the tail and didn't look back to see the origin of the topic. This is pretty stupid on my part. I would say that you were indeed trying to do presentation logic. And furthermore, I would say that my arguments were offbase and pretty much knee-jerk reactions to the possibility of what might happen if functional kw's and builtins were added to DTML. After considering it further, it doesn't seem to be such a bad idea if it didn't impact security in any way. It might even reduce the complexity of DTML looping, which would be a good thing. It would most certainly be welcome in Python methods, I'd definitely miss map a lot in Python methods if it wasnt there. If it's something you want badly (and if you haven't already) add it as a feature request to the Collector on Zope.org. Once other folks get past the knee-jerk reaction, maybe it'll sink in as a good thing. In the meantime, you can add it yourself if you like. Hopefully, you'll be so kind as to forgive me. After seeing people post DTML code to the list that they need debugged that computes pay rates, has the shape of an arrowhead and scrolls past the maximum character length of my email client horizontally, I'm a little leery of adding anything whatsoever to DTML, as I think the rest of the folks at DC are. Reducing the complexity of DTML is a good thing, however it can be accomplished. As for HTML-like stuff vs. business stuff wrt to functional expressions: Do you object to CSS, XSLT, and XPath? I can't object to XSLT and XPath, I don't even know enough about them to spell them right. :-) CSS is all about presentation, but AFAICT doesn't have an expression syntax, nor even a way to spell a loop. I think I can see why the ZoPerl discussion is so vociferous... ;¬] I'm not sure I see the connection... ___ Zope maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] Use of lambda expression in DTML
On Fri, May 26, 2000 at 03:05:14PM -0700, Jonothan Farr wrote: In brief: I get a NameError when invoking "filter(...)" from DTML. I thought this was a built-in python method, so I'm a bit puzzled. Certain built-ins are not available in dtml for security and other reasons, filter and lambda are among them. As far as I know, you'll need to use an external method to do this or think of another way to do it in dtml. One big question is: what's wrong with filter, map and lambda? They can make a lot of code a lot simpler, specially for people who have a background in functional programming. IIRC there is nothing wrong with them - only that all builtins were disabled and some specific few were re-enabled, and then those three were overlooked. Count one vote for getting them back (_.lambda, _.filter, _.map is fine enough, of course). []s, |alo + -- Hack and Roll ( http://www.hackandroll.org ) News for, uh, whatever it is that we are. http://zope.gf.com.br/lalo mailto:[EMAIL PROTECTED] pgp key: http://zope.gf.com.br/lalo/pessoal/pgp Brazil of Darkness (RPG)--- http://zope.gf.com.br/BroDar ___ Zope maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] Use of lambda expression in DTML
On Mon, May 29, 2000 at 09:40:59PM +0200, Martijn Pieters wrote: I can't comment with authority on why these methods are not accessible, but I imagine that they are banned because they'll probably let you lock up Zope in one way or another, creating a convenient denial-of-service attack. If someone can edit arbitrary DTML, there are already easier ways to cause infinite loops. And these loops don't lock up Zope, they only eventually raise an exception (granted, consuming lots of resources in the proccess). What I would like to say is that if your application needs lambda, filter or map, your code is getting bejond report or presentation generation (for which DTML is intended) and in the realm of data manipulation and business rules. In this case your code would be much better placed in some form of Method object, be that an External, Python, or when ready, Perl Method, or even as a disk based Product. 1: This is simply not true, and a very poor excuse for handicapping the language. A common example is: dtml-in "_.map (lambda item: Catalog.getobject (item.data_record_id_), Catalog (REQUEST)" I know fetching the actual records from a ZCatalog introduces a performance penalty, but sometimes it's necessary; there are times you need to be absolutely sure everything is pushed on the namespace, including user-defined properties and sub-objects. This is the case in HackRoll, and I have to use a very ugly Python Method there (and as PythonMethods don't have map either, I have to build a list from scratch using for, which introduces additional penalties as I'm basically bypassing Python's optimizations). And _.filter can be a mini-catalog: dtml-in "_.filter (lambda item: AUTHENTICATED_USER.has_permission ('some_permission', item), objectValues (['Folder'])" 2: If I should be using a Python Method, then Python Methods should have these forms, and they don't. 3: In short, these excuses are just the fallback (or should I say Acquired?) excuses used mostly by people who don't know how to use these very cool features of Python. []s, |alo + -- Hack and Roll ( http://www.hackandroll.org ) News for, uh, whatever it is that we are. http://zope.gf.com.br/lalo mailto:[EMAIL PROTECTED] pgp key: http://zope.gf.com.br/lalo/pessoal/pgp Brazil of Darkness (RPG)--- http://zope.gf.com.br/BroDar ___ Zope maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] Use of lambda expression in DTML
What I would like to say is that if your application needs lambda, filter or map, your code is getting bejond report or presentation generation (for which DTML is intended) and in the realm of data manipulation and business rules. In this case your code would be much better placed in some form of Method object, be that an External, Python, or when ready, Perl Method, or even as a disk based Product. 1: This is simply not true, and a very poor excuse for handicapping the language. Nope. Lamba, filter, reduce, and map should IMHO not be part of DTML. Actually, exprs probably shouldn't be in there in the first place. People are trying to use DTML as a way to process non-UI elements. This is a slippery slope, and leads to something like PHP or ASP. My opinion is this: Generate and format your HTML with DTML, and do everything else in Python. This gives you the added benefit that you can change the return values of functions by changing the Python code without having to muck with DTML, which is painful any way you look at it. People are right when they say DTML is beginning to get worse than Perl, and IMHO, adding these functions could make it even worse than it is now. This sort of processing was not meant to happen in an expr under Zope. Python lets you do it, but it's sort of hard for somebody else to understand after you've completed it, even if they know Python well. Functional constructs are handy, but not the most clear thing on the planet. A common example is: dtml-in "_.map (lambda item: Catalog.getobject (item.data_record_id_), Catalog (REQUEST)" How could someone understand this when you're finished with it? I looked at it three times before figuring out what you were trying to do. What if you didn't write it and you needed to answer somebody asking a question about it on this mailing list? Isn't this much more understandable as an external method explicitly? def return_cataloged_objects(self): obs = [] catalog = self.Catalog request = self.REQUEST for r in catalog(request): d_rid = r.data_record_id_ ob = catalog.getobject(d_rid) obs.append(ob) return obs then: dtml-in return_cataloged_objects ... /dtml-in It *might* be slighly slower, but someone maintaining your code will eventually thank you for this. Eventually it will cost you less money somewhere that you might be able to buy a faster server with. I know fetching the actual records from a ZCatalog introduces a performance penalty, but sometimes it's necessary; there are times you need to be absolutely sure everything is pushed on the namespace, including user-defined properties and sub-objects. This is the case in HackRoll, and I have to use a very ugly Python Method there (and as PythonMethods don't have map either, I have to build a list from scratch using for, which introduces additional penalties as I'm basically bypassing Python's optimizations). Sometimes. Sometimes not. Read http://www.python.org/doc/essays/list2str.html . As I understand it, by using expr syntax to begin with, you're defeating some of Zope's caching mechanisms, actually slowing things down in reality. 2: If I should be using a Python Method, then Python Methods should have these forms, and they don't. Maybe they should... it would be nice. I'm not sure why they're not in Python Methods, other than Evan tried to make the security inherent in Python methods as close to DTML as he could. I use map a lot. I don't use lambda unless I really, really have to. Reduce and filter.. eh. I dunno. They're sort of on the periphery of usefulness. 3: In short, these excuses are just the fallback (or should I say Acquired?) excuses used mostly by people who don't know how to use these very cool features of Python. I would hardly say Martijn is one of those. :-) ___ Zope maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Re: [Zope] Use of lambda expression in DTML
Nick Drew writes: I'm a newbie zopista, and python for that matter... In brief: I get a NameError when invoking "filter(...)" from DTML. I thought this was a built-in python method, so I'm a bit puzzled. For security reasons, Zope are removed all builtin-functions from DTML expressions. You can access some of the builtin objects as attributes/methods the special variable "_". Look in the DTML reference (e.g. at URL:http://zdp.zope.org) for a list of available builtins in "_". Dieter ___ Zope maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )