Re: [Zope] Testing multiple values for existance - still
On 24.09.2001 21:26:28 +0200 Dieter Maurer <dieter@handshake.de> wrote:
Apparently, you ignored my reply.....
<dtml-if "(_.has_key('foo') and foo) or (_.has_key('bar') and bar)">
Sorry, it seems I did. And that's sad, because it's the only one that really works out of the box. One question, though: After fiddling around a bit with that line (still trying to understand things better :-), it seems that <dtml-if "(_.has_key('foo')) or (_.has_key('bar'))"> works the same. In what situation would there be a key in the global namespace (that's the _, isn't it?), but the name is still not defined? The whole thing still has one great question for me: - How exactly is the expression "foo" defined? I mean, in C, the expression "foo" is defined to be the same as "foo != 0", and that's true in all circumstances. I can use "foo && bar" as a substitute for "(foo != 0) && (bar != 0)". I still fail to see the consistency in the Python expressions, because <dtml-if "foo and bar"> does not do the same thing as <dtml-if foo><dtml-if bar>, at least not if foo and bar are not defined. Thanks for your help! Oliver -- Oliver Sturm / <sturm@oliver-sturm.de>
Oliver, You are missing the distinction between: <dtml-if foo> and <dtml-if "foo and bar"> You're also missing some of the maddening Zope 'Magic'. The first statement tests first for existence, and only then for value. The second assumes existence and only tests for value. In your case I think you were testing for the esitence of a method, not of a variable, am I right? If foo doesn't exist then the first test returns false because foo is not in the currrent namespace, the second though drops into Python rules and has none of the Zope 'magic' applied. I hope this goes someway to explain what you asked, of course if you'd said explicitly that you were testing for the existence of methods in the current folder, then this could be made a bit simpler. hth Phil phil.harris@zope.co.uk On 25 Sep 2001 10:10:28 +0200, Oliver Sturm wrote:
On 24.09.2001 21:26:28 +0200 Dieter Maurer <dieter@handshake.de> wrote:
Apparently, you ignored my reply.....
<dtml-if "(_.has_key('foo') and foo) or (_.has_key('bar') and bar)">
Sorry, it seems I did. And that's sad, because it's the only one that really works out of the box. One question, though: After fiddling around a bit with that line (still trying to understand things better :-), it seems that
<dtml-if "(_.has_key('foo')) or (_.has_key('bar'))">
works the same. In what situation would there be a key in the global namespace (that's the _, isn't it?), but the name is still not defined?
The whole thing still has one great question for me:
- How exactly is the expression "foo" defined? I mean, in C, the expression "foo" is defined to be the same as "foo != 0", and that's true in all circumstances. I can use "foo && bar" as a substitute for "(foo != 0) && (bar != 0)". I still fail to see the consistency in the Python expressions, because <dtml-if "foo and bar"> does not do the same thing as <dtml-if foo><dtml-if bar>, at least not if foo and bar are not defined.
Thanks for your help!
Oliver
-- Oliver Sturm / <sturm@oliver-sturm.de>
_______________________________________________ Zope maillist - Zope@zope.org 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 )
On 25.09.2001 10:24:48 -0400 Phil Harris <phil.harris@zope.co.uk> wrote:
You are missing the distinction between:
<dtml-if foo>
and
<dtml-if "foo and bar">
You're also missing some of the maddening Zope 'Magic'.
The first statement tests first for existence, and only then for value. The second assumes existence and only tests for value.
I got that at last :-). But WHY? I reckon it may be as simple as that the "'s render the whole thing a Python expression instead of something that can be handled directly by Zope. Is that so? If yes, I'd consider that a large drawback and inconsistency... and does Python syntax for the test have to be so complicated?
In your case I think you were testing for the esitence of a method, not of a variable, am I right? If foo doesn't exist then the first test returns false because foo is not in the currrent namespace, the second though drops into Python rules and has none of the Zope 'magic' applied.
That's right (I think). Although I have never actually found many differences between methods and variables when it comes to their use in expressions (up to now, that is). I was considering it one of the strengths of Zope to work with objects of different types without apparent distinctions.
I hope this goes someway to explain what you asked, of course if you'd said explicitly that you were testing for the existence of methods in the current folder, then this could be made a bit simpler.
I didn't assume people would think I was doing so very complicated things. I usually structure Zope sites in a way that allows me to work with objects in the current context (folder?) 99% of the time, praise acquisition. Now, are there even simpler ways to test for alternate existence of more than one method in the current folder? Thanks for your help! Oliver -- Oliver Sturm / <sturm@oliver-sturm.de>
On Tue, Sep 25, 2001 at 11:54:33AM +0200, Oliver Sturm wrote:
On 25.09.2001 10:24:48 -0400 Phil Harris <phil.harris@zope.co.uk> wrote:
The first statement tests first for existence, and only then for value. The second assumes existence and only tests for value.
I got that at last :-). But WHY? I reckon it may be as simple as that the "'s render the whole thing a Python expression instead of something that can be handled directly by Zope. Is that so?
Exactly. Inside a dtml tag, anything in quotes is treated as python code.
If yes, I'd consider that a large drawback and inconsistency... and does Python syntax for the test have to be so complicated?
It's not "complicated" in normal python programming, which is usually easy, flexible, and powerful. But when you're limited to what you can fit between quotes in a dtml tag, you can't really take advantage of python effectively. It was never meant to be a language for one-liners. But of course, we have python scripts and external methods, which are much better for jobs like this. If all you want is to know if any of a bunch of zope objects exist in this folder, or if any of some properties has a true value, it's ugly and hard to read if you do it in the dtml-if tag: <dtml-if "(_.hasattr('foo') and foo) or (_.hasattr('bar') and bar)"> ... and it just gets worse if you want to test for more names! So I'd make a python script like this: ## Script (Python) "check_for_any" ##bind container=container ##bind context=context ##bind script=script ##bind subpath=traverse_subpath ##parameters=*args ##title= ## found = None for a in args: try: x=getattr(context, a) if x: found = 1 break # Jump out of the loop, no more tests needed except AttributeError: continue return found The key thing to notice is that the one argument is *args. In python, that means that this function takes a variable number of arguments, and they will be stored in a list of the given name (in this case, args). Now you can do this in your dtml: <p>Checking for one thing... <dtml-if "check_for_any('foo')"> ... </dtml-if> <p>Checking for more things... <dtml-if "check_for_any('foo', 'bar', 'baz', 'bat', 'baf')"> ... </dtml-if> Much nicer, eh? -- ................ paul winkler ................ custom calendars: http://www.calendargalaxy.com A member of ARMS: http://www.reacharms.com home page: http://www.slinkp.com
On 25.09.2001 13:42:49 -0400 Paul Winkler <slinkp23@yahoo.com> wrote:
I got that at last :-). But WHY? I reckon it may be as simple as that the "'s render the whole thing a Python expression instead of something that can be handled directly by Zope. Is that so?
Exactly. Inside a dtml tag, anything in quotes is treated as python code.
If yes, I'd consider that a large drawback and inconsistency... and does Python syntax for the test have to be so complicated?
It's not "complicated" in normal python programming, which is usually easy, flexible, and powerful. But when you're limited to what you can fit between quotes in a dtml tag, you can't really take advantage of python effectively. It was never meant to be a language for one-liners.
Would be nice if there was a built-in function that does the very same thing that Zope does implicitly in <dtml-if foo>, only explicitly in a python expression. So I could go <dtml-if "checkforexistenceandvalue ('foo') and checkforexistenceandvalue ('bar')">. The way it is, that's hidden magic to me. I hate it if I get the feeling that the system uses functionality that's not available for me to use, too. Kind of like programming VBA :-)
But of course, we have python scripts and external methods, which are much better for jobs like this.
[ cut ]
Much nicer, eh?
Yes, thanks for that! Seems like I'll have to use those scripts more... (I've even been writing some external scripts in Python, only I was a little reluctant to use them all over the place for things as simple as some existence checking :-) Oliver -- Oliver Sturm / <sturm@oliver-sturm.de>
participants (3)
-
Oliver Sturm -
Paul Winkler -
Phil Harris