[Zope] external methods

Service Informatique CHSR chsrinfo@guetali.fr
Wed, 31 Mar 1999 10:36:51 +0400


Hello,


>Well, this is an interesting question. In my experience, seldom do people
>try to munge the DTML namespace from within an External Method. I guess it
>could be done (perhaps by passing the special underscore variable to an
>External Method), but I wonder if this is really the best way to solve your
>problem.

Thanks for your answer.
In fact, I want to write external methods to replace DTML methods which work
like a charm, but are not very readable (a lot of iterative stuff, given the
fact
that mySQL doesn't support sub-select) and pretty slow.
I have used python for several years right now, but DTML is new for me.

My users can search in our news database in several ways, that are
covered by different SQL queries. However, the output of all these queries
are the same, so I use a single DTML method to display the result.
So the name of the ZSQL Method is only known at runtime, and I use something
like:

<!--#in "_[query]"-->

to display the result. The arguments of the query are taken from the
namespace.
I can also write:

<!--#with "_.namespace(cooked_arg0=some_expr(arg0),
cooked_arg1=some_expr(arg1)"-->
<!--#in "_[query]"-->
<!-- ... -->
<!--#/in-->
<!--#/with-->

If I want to cook some args before feeding it to the query.
All these work from DTML.

Now, I want to write this in an external method.

def results(self):
    for record in getattr(self, self.REQUEST['query'])(self):
        pass # do watever

'query' has to be in the form REQUEST variables (that is not a problem), but
I cannot be sure that any arguments that the query need are all accessible
through
REQUEST.

>Well let's see what's going on here. You call my method, it acquires the
>REQUEST object and looks for a 'my_var', doesn't see it and returns
>'boooo'. so far so good. Now you push a namespace containing only 'my_var'
>on to the DTML namespace stack and call the method again. Not surprisingly
>the method does the same thing as before, it acquires the REQUEST and finds
>that it does not contain 'my_var' so it prints 'boooo'. Finally you call
>'my_var' which is found in the DTML namespace and 'my_value' is printed.
I wrote this to test wether an external method has access to the entire
namespace,
or only to REQUEST variables.

>So the lesson is: the DTML namespace is not the same thing as the REQUEST
>object.
So I come back with the question: is there any way to access the DTML
namespace
from an external method. Apparently there is not

>For more on how DTML namespace works see my advanced DTML how-to:
>
>http://www.zope.org/Documentation/HowTo/DTML
>
I have already read it.
By the way, let me ask you a question (again. I already asked that, but the
answer
was not very clear, and I finally gave up):

<!--#var my_var--> and <!--#var "_['my_var']"--> are the same.
So instead of writing:

<!--#if var0--><!--#if var1-->
<!-- ... -->
<!--#/if--><!--#/if-->

I usually write:

<!--#if "_['var0'] and _['var1']"-->
<!-- ... -->
<!--#/if--><!--#/if-->

But almost everyone told me that

<!--#if "var0 and var1"-->

also work. So I wonder: are <!--#var "var0"--> and <!--#var var0-->
the same??? The DTML User's guide say that in an expression,
variable are not called by default, but _['var0'] calls var0 if possible.


>My advice to you is to keep things simple. It's easy to confuse yourself
>with the complexities of DTML. In general, I would suggest that your
>External Methods take well defined arguments and return results, rather
>than modify the objects passed to them. Then allow the DTML to pass those
>arguments and munge its own namespace in response to the results of the
>External Method. Of course, this schema isn't always the right one. Finally
>I would suggest that you, don't munge the DTML namespace from within an
>External Method unless you have a good reason to, I think that this would
>make the DTML even harder to understand.
To keep things simple mean I have to write several times the same external
method
with different queries and differents arguments each time. this is not what
I want.
I can remember that in DTML I only need one DTML Method.
I would really like to replace some of my DTML Methods with external
methods.

That's enough for today. Let's take some rest :-)

Regards,
Jephte CLAIN, Service Informatique CHSR