[Zope] calling dtmlMethod from python
Chris Kratz
chris.kratz@vistashare.com
Tue, 30 Oct 2001 12:19:52 -0500
This is a multi-part message in MIME format.
------=_NextPart_000_008C_01C1613D.2D5180E0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Go to your python script and click on the bindings tab.
The fourth item has a heading of Namespace. Put the recommended value of =
_ into the field and save changes.
What this does is it binds the namespace that is available to =
dtml-methods normally to the name _. The problem you were running into =
is that when you enter a python script, normally this namespace *IS NOT* =
passed into the script. But, once you bind it to the _ name, you can =
access it by doing things like _.getitem('') or _.has_key('') etc. The =
last piece of the puzzle is to pass that namespace into your dtml method =
so it can access it by passing it as the second parameter.
Notice that you can set it to whatever name you want. I think there are =
historical reasons why it has generally been _. If you do set it to =
something else, then you need to change _ to your new name in your code.
This was one of the biggest frustrations I had and still sometimes have =
with moving to more python scripted code. Writing python scripts is a =
lot easier for complex logic. But the namespace stuff seems =
unnecessarily complex and frustrating sometimes.
There is probably an easier way to do this sort of thing and perhaps =
someone with more experience would like to comment and clarify, but this =
is how I was able to get my code working.
Good Luck,
-Chris
------------------------------
Chris Kratz
chris.kratz@vistashare.com
----- Original Message -----=20
From: Shane O'Sullivan=20
To: Chris Kratz ; zope@zope.org=20
Sent: Tuesday, October 30, 2001 12:04 PM
Subject: Re: [Zope] calling dtmlMethod from python
Chris,
thanks for your help...I tried your suggestion but it has now =
returned the error
Error Type: NameError
Error Value: global name '_' is not defined
If you have time, would you mind explaining what "_" represents? Is =
this defined in a library that we mightn't have included?
Thanks again,
Shaneo
----- Original Message -----=20
From: Chris Kratz=20
To: Shane O'Sullivan ; zope@zope.org=20
Sent: Tuesday, October 30, 2001 4:26 PM
Subject: Re: [Zope] calling dtmlMethod from python
I ran into this awhile back. Try this:
context.scReport(None, _)
Interestingly enough, I also learned that you can pass things into a =
dtml-method and they become available to use internally. For example =
with the following call:
context.scReport(None, _, MsgToUser=3D'Hello')
Inside your dtml-method you could do a=20
<dtml-var MsgToUser>
This is very usefull to passing stuff around without cluttering up =
your REQUEST namespace.
Another little trick I learned. Want to find an object via =
acquisition and then call it? I found that the following worked in a =
python script:
methodToCall =3D _.getitem('methodName') # Find object via =
acquisition and get handle on it.
print methodToCall(None, _) # Call the method and print the =
results.
This is really usefull if you generate the method name as I do =
depending on the request. ie methodToCall =3D _.getitem(someStrVar + =
'form') will concatenate the two strings and then look for that object =
in the namespace. If you do this kind of lookups, you should probably =
also check for existence before calling it, ie _.has_key(someStrVar + =
'form').
Hope that helps.
-Chris
------------------------------
Chris Kratz
chris.kratz@vistashare.com
----- Original Message -----=20
From: Shane O'Sullivan=20
To: zope@zope.org=20
Sent: Tuesday, October 30, 2001 11:13 AM
Subject: [Zope] calling dtmlMethod from python
Can anyone please help with this, I'm new to zope and trying to =
write a simple python script to handle some logic which will then call a =
dtml method. I'm calling the method as such: context.scReport()
The error I'm getting back is Error Type: KeyError
Error Value: =
standard_html_header
standard_html_header is being called from the scReport method =
which suggests the scReport method is being invoked but why are we =
getting an error?? Any suggestions, TIA
Shaneo
------=_NextPart_000_008C_01C1613D.2D5180E0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3D"Courier New" size=3D2>Go to your python script and =
click on the=20
bindings tab.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>The fourth item has a heading =
of Namespace.=20
Put the recommended value of _ into the field and save =
changes.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>What this does is it binds the =
namespace=20
that is available to dtml-methods normally to the name _. The =
problem you=20
were running into is that when you enter a python script, normally =
this=20
namespace *IS NOT* passed into the script. But, once you bind it =
to the _=20
name, you can access it by doing things like _.getitem('') or =
_.has_key('')=20
etc. The last piece of the puzzle is to pass that namespace into =
your dtml=20
method so it can access it by passing it as the second =
parameter.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Notice that you can set it to =
whatever name=20
you want. I think there are historical reasons why it has =
generally been=20
_. If you do set it to something else, then you need to change _ =
to your=20
new name in your code.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>This was one of the biggest =
frustrations I=20
had and still sometimes have with moving to more python scripted =
code. =20
Writing python scripts is a lot easier for complex logic. But the=20
namespace stuff seems unnecessarily complex and frustrating=20
sometimes.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>There is probably an easier way =
to do this=20
sort of thing and perhaps someone with more experience would like to =
comment and=20
clarify, but this is how I was able to get my code working.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Good Luck,</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>-Chris</FONT></DIV>
<DIV>------------------------------<BR>Chris Kratz<BR><A=20
href=3D"mailto:chris.kratz@vistashare.com">chris.kratz@vistashare.com</A>=
<BR></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV=20
style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
<A title=3Dshane.osullivan@engitech.ie=20
href=3D"mailto:shane.osullivan@engitech.ie">Shane O'Sullivan</A> =
</DIV>
<DIV style=3D"FONT: 10pt arial"><B>To:</B> <A =
title=3Dchris.kratz@vistashare.com=20
href=3D"mailto:chris.kratz@vistashare.com">Chris Kratz</A> ; <A=20
title=3Dzope@zope.org href=3D"mailto:zope@zope.org">zope@zope.org</A> =
</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Tuesday, October 30, 2001 =
12:04=20
PM</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Subject:</B> Re: [Zope] calling =
dtmlMethod=20
from python</DIV>
<DIV><BR></DIV>
<DIV><FONT face=3DArial size=3D2>Chris,</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2> thanks=20
for your help...I tried your suggestion but it has now returned the=20
error</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>
<P><STRONG>Error Type: NameError</STRONG><BR><STRONG>Error Value: =
global name=20
'_' is not defined</STRONG><BR></P>
<P>If you have time, would you mind explaining what "_" represents? Is =
this=20
defined in a library that we mightn't have included?</P>
<P>Thanks again,</P>
<P>Shaneo</P>
<P> </P>
<P> </P></FONT></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV=20
style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
<A title=3Dchris.kratz@vistashare.com=20
href=3D"mailto:chris.kratz@vistashare.com">Chris Kratz</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>To:</B> <A=20
title=3Dshane.osullivan@engitech.ie=20
href=3D"mailto:shane.osullivan@engitech.ie">Shane O'Sullivan</A> ; =
<A=20
title=3Dzope@zope.org =
href=3D"mailto:zope@zope.org">zope@zope.org</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Tuesday, October 30, =
2001 4:26=20
PM</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Subject:</B> Re: [Zope] calling =
dtmlMethod=20
from python</DIV>
<DIV><BR></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>I ran into this awhile =
back. Try=20
this:</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>context.scReport(None, =
_)</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Interestingly enough, I =
also learned=20
that you can pass things into a dtml-method and they become =
available to use=20
internally. For example with the following call:</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>context.scReport(None, _,=20
MsgToUser=3D'Hello')</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Inside your dtml-method you =
could do a=20
</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2><dtml-var =
MsgToUser></FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>This is very usefull to =
passing stuff=20
around without cluttering up your REQUEST namespace.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Another little trick I =
learned. =20
Want to find an object via acquisition and then call it? I =
found that=20
the following worked in a python script:</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>methodToCall =3D =
_.getitem('methodName')=20
# Find object via acquisition and get handle on it.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>print methodToCall(None, =
_) #=20
Call the method and print the results.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>This is really usefull if =
you generate=20
the method name as I do depending on the request. ie =
methodToCall =3D=20
_.getitem(someStrVar + 'form') will concatenate the two strings and =
then=20
look for that object in the namespace. If you do this kind of =
lookups,=20
you should probably also check for existence before calling it, ie=20
_.has_key(someStrVar + 'form').</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Hope that =
helps.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2>-Chris</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT> </DIV>
<DIV>------------------------------<BR>Chris Kratz<BR><A=20
=
href=3D"mailto:chris.kratz@vistashare.com">chris.kratz@vistashare.com</A>=
<BR></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style=3D"FONT: 10pt arial">----- Original Message ----- =
</DIV>
<DIV=20
style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
<A title=3Dshane.osullivan@engitech.ie=20
href=3D"mailto:shane.osullivan@engitech.ie">Shane O'Sullivan</A> =
</DIV>
<DIV style=3D"FONT: 10pt arial"><B>To:</B> <A =
title=3Dzope@zope.org=20
href=3D"mailto:zope@zope.org">zope@zope.org</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Tuesday, October 30, =
2001 11:13=20
AM</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Subject:</B> [Zope] calling =
dtmlMethod=20
from python</DIV>
<DIV><BR></DIV>
<DIV><FONT face=3DArial size=3D2>Can anyone please help with this, =
I'm new to=20
zope and trying to write a simple python script to handle some=20
logic which will then call a dtml method. I'm calling the =
method as=20
such: context.scReport()</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>The error I'm getting back is =
<STRONG>Error=20
Type:=20
=
KeyError</STRONG><BR><STRONG> &n=
bsp; &nb=
sp; &nbs=
p; Error=20
Value: standard_html_header</STRONG></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>standard_html_header is being =
called from the=20
scReport method which suggests the scReport method is being =
invoked but=20
why are we getting an error?? Any suggestions, TIA</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2>Shaneo</FONT></DIV></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE></BODY=
></HTML>
------=_NextPart_000_008C_01C1613D.2D5180E0--