Hi; I'm new to Python and Zope, but I'm at a loss to understand WHY this doesn't work! I have this line in my dtml code: <span class="top"><img src="images/top" width=<dtml-var "sizeCalc(1008,size)"> height=<dtml-var "sizeCalc(82,size)">></span> *size* is a dtml method and is defined thus: 1 just the number one. sizeCalc(x,y) is an external method and is defined thus: import string def sizeCalc(x,y): x = string.atoi(x) y = string.atoi(y) y = (x*5) return (x*y) I tried the string conversions because the values are being interpreted as strings (already tested that). I get this error: Error Type: TypeError Error Value: int() can't convert non-string with explicit base and the following: Traceback (innermost last): File /apache/ZopeInstallation/Zope_software_home/lib/python/ZPublisher/Publish.py, line 223, in publish_module File /apache/ZopeInstallation/Zope_software_home/lib/python/ZPublisher/Publish.py, line 187, in publish File /apache/ZopeInstallation/Zope_software_home/lib/python/Zope/__init__.py, line 226, in zpublisher_exception_hook (Object: LockableItem) File /apache/ZopeInstallation/Zope_software_home/lib/python/ZPublisher/Publish.py, line 171, in publish File /apache/ZopeInstallation/Zope_software_home/lib/python/ZPublisher/mapply.py, line 160, in mapply (Object: template) File /apache/ZopeInstallation/Zope_software_home/lib/python/ZPublisher/Publish.py, line 112, in call_object (Object: template) File /apache/ZopeInstallation/Zope_software_home/lib/python/OFS/DTMLMethod.py, line 194, in __call__ (Object: template) File /apache/ZopeInstallation/Zope_software_home/lib/python/DocumentTemplate/DT_String.py, line 546, in __call__ (Object: template) File /apache/ZopeInstallation/Zope_software_home/lib/python/DocumentTemplate/DT_Util.py, line 231, in eval (Object: sizeCalc(1008,size)) (Info: sizeCalc) File <string>, line 2, in f File /apache/ZopeInstallation/Zope_software_home/lib/python/Products/ExternalMethod/ExternalMethod.py, line 274, in __call__ (Object: sizeCalc) (Info: ((1008, <DTMLMethod instance at 88917d0>), {}, None)) File /apache/ZopeInstallation/Zope_software_home/Extensions/sizeCalc.py, line 4, in sizeCalc File /apache/ZopeInstallation/Zope_software_home/lib/python2.1/string.py, line 214, in atoi TypeError: (see above) What am I doing wrong? Frustrated, BenO
On Tue, Oct 23, 2001 at 11:43:21AM -0700, Ben Ocean wrote:
Hi; I'm new to Python and Zope, but I'm at a loss to understand WHY this doesn't work! I have this line in my dtml code: <span class="top"><img src="images/top" width=<dtml-var "sizeCalc(1008,size)"> height=<dtml-var "sizeCalc(82,size)">></span> *size* is a dtml method and is defined thus: 1 just the number one. sizeCalc(x,y) is an external method and is defined thus:
import string
def sizeCalc(x,y): x = string.atoi(x) y = string.atoi(y)
try this: x = int(x) y = int(y)
I tried the string conversions because the values are being interpreted as strings (already tested that).
Good - you're looking in the right direction. DTML methods return strings.
I get this error:
Error Type: TypeError Error Value: int() can't convert non-string with explicit base
Right. In your dtml page, you called sizeCalc like so: <dtml-var "sizeCalc(82,size)"> The second argument comes from your DTML method, right? That's a string, and that's why you got type errors before. But that first argument is not a string - it's an integer. string.atoi() can only take string arguments. So you've just traded one TypeError for another. You could keep your external method as it is now and call it like this: <dtml-var "sizeCalc('82',size)"> But I suspect you'll be much happier if you make your external method able to deal with both integers and strings, so you don't have to remember this every time you call it. Use python's built-in int() function. (It can also take float arguments - it just chops off anything after the decimal.) -- paul winkler home: http://www.slinkp.com music: http://www.reacharms.com calendars: http://www.calendargalaxy.com
* Ben Ocean <zope@thewebsons.com> [011023 19:49]:
Hi; I'm new to Python and Zope, but I'm at a loss to understand WHY this doesn't work! I have this line in my dtml code: <span class="top"><img src="images/top" width=<dtml-var "sizeCalc(1008,size)"> height=<dtml-var "sizeCalc(82,size)">></span> *size* is a dtml method and is defined thus: 1 just the number one.
<snip>
Error Type: TypeError Error Value: int() can't convert non-string with explicit base
The 'size' argument in <dtml-var "sizeCalc(1008, size)"> is a string; the first argument is an int. Try string.atoi(1008) in your python interpreter and compare it with string.atoi('1008'). seb
At 09:08 PM 10/23/01 +0100, you wrote:
* Ben Ocean <zope@thewebsons.com> [011023 19:49]:
Hi; I'm new to Python and Zope, but I'm at a loss to understand WHY this doesn't work! I have this line in my dtml code: <span class="top"><img src="images/top" width=<dtml-var "sizeCalc(1008,size)"> height=<dtml-var "sizeCalc(82,size)">></span> *size* is a dtml method and is defined thus: 1 just the number one.
<snip>
Error Type: TypeError Error Value: int() can't convert non-string with explicit base
The 'size' argument in
<dtml-var "sizeCalc(1008, size)">
is a string; the first argument is an int.
Hmm. Tested *both* variables using type and *both* returned string. Paul Winkler writes:
try this: x = int(x) y = int(y)
I forgot to post that I *had* tried that. This is the error: Error Type: AttributeError Error Value: __int__ I'm even more confused than before and concerned that there might be something wrong with my installation at this point. I *do* have functioning sites in Zope, however. Here is the current code for sizeCalc() import string def sizeCalc(x,y): # x = string.atoi(x) # y = string.atoi(y) x = int(x) y = int(y) return (x*y) TIA, BenO
* Ben Ocean <zope@thewebsons.com> [011023 20:47]:
<snip>
Error Type: TypeError Error Value: int() can't convert non-string with explicit base
The 'size' argument in
<dtml-var "sizeCalc(1008, size)">
is a string; the first argument is an int.
Hmm. Tested *both* variables using type and *both* returned string.
<snip>
I forgot to post that I *had* tried that. This is the error: Error Type: AttributeError Error Value: __int__
I'm even more confused than before and concerned that there might be something wrong with my installation at this point.
Unlikely ;-) I just tried the code to make sure, and it works for me; the number being passed as the first argument *is* an int. The AttributeError suggests you're passing an object rather than a string or an int to the script. I feel sure you have somehow got your type checking wrong when you were debugging, since strings and ints would never give this AttributeError, and the first argument, 2008, is definitely an int. Perhaps you have a namespace clash with an object elsewhere in your acquisition path, also called 'size'? Try renaming the 'size' method. seb
On Tue, Oct 23, 2001 at 09:42:23PM +0100, seb bacon wrote:
Hmm. Tested *both* variables using type and *both* returned string.
AFAIK that can't be true with the examples you posted. Are you sure you didn't change the <dtml-var> stuff since you checked the types?
I forgot to post that I *had* tried that. This is the error: Error Type: AttributeError Error Value: __int__
I'm even more confused than before and concerned that there might be something wrong with my installation at this point.
Unlikely ;-)
I just tried the code to make sure, and it works for me; the number being passed as the first argument *is* an int.
The AttributeError suggests you're passing an object rather than a string or an int to the script.
Right. Try this at the python prompt (the BEST way to learn the zen of python!): $ python Python 1.5.2 (#1, Aug 25 2000, 09:33:37) [GCC 2.96 20000731 (experimental)] on linux-i386 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
class Foo: ... pass ... int(1) 1 int(1.0) 1 int("1") 1 int( Foo() ) Traceback (innermost last): File "<stdin>", line 1, in ? AttributeError: __int__
When int() gets an object that isn't an integer, a float, or a string, it looks to see if that object provides an __int__() method and calls that to convert it to an integer. If the object doesn't provide such a method, you get the AttributeError.
I feel sure you have somehow got your type checking wrong when you were debugging, since strings and ints would never give this AttributeError, and the first argument, 2008, is definitely an int.
Perhaps you have a namespace clash with an object elsewhere in your acquisition path, also called 'size'? Try renaming the 'size' method.
Wait a minute - if "size" is a method, you need to call it, not pass it! Python doesn't automatically call methods the way Zope does. It could be you're passing the method to your external method, instead of calling the size method and passing the RESULT to your external method. Without quotes it gets automagically called: <dtml-var size> With quotes, it's python and you need parentheses to call it: <dtml-var "size()"> -- paul winkler home: http://www.slinkp.com music: http://www.reacharms.com calendars: http://www.calendargalaxy.com
On Tue, 23 Oct 2001 11:43:21 -0700, Ben Ocean <zope@thewebsons.com> wrote:
Hi; I'm new to Python and Zope,
Welcome.
*size* is a dtml method and is defined thus: 1 just the number one.
ok
but I'm at a loss to understand WHY this doesn't work! I have this line in my dtml code: <span class="top"><img src="images/top" width=<dtml-var "sizeCalc(1008,size)"> height=<dtml-var "sizeCalc(82,size)">></span>
Here the second parameter to sizeCalc is the dtml method itself, which is never called. I guess you were expecting it to be the result of calling that dtml method. To explitly call a dtml method from python you need: <dtml-var "sizeCalc(82,size(_.None,_)"> Toby Dickenson tdickenson@geminidataloggers.com
* Toby Dickenson <tdickenson@devmail.geminidataloggers.co.uk> [011024 08:33]:
To explitly call a dtml method from python you need:
<dtml-var "sizeCalc(82,size(_.None,_)">
I just mailed this suggestion, too. However, now I'm slightly confused. It's what I was originally going to advise, and it's obviously the problem in this case, but then I tried it out, and found that <dtml-var "sizeCalc(82, size)"> actually *worked* for me. I wonder why? seb
participants (4)
-
Ben Ocean -
Paul Winkler -
seb bacon -
Toby Dickenson