RE: Passing namespace to Python scrict was: how to call a n external module from a Python script?
From: Dieter Maurer *EXTERN* [mailto:dieter@handshake.de]
Reif Peter writes:
... My script:
... ##bind namespace=namespace ...
zone = namespace.zone return zone
It is called from a DTML-method where the tag <dtml-var zone> is rendered correctly:
... <dtml-var get_status_a> ...
Error:
Zope Error Zope has encountered an error while publishing this resource. Error Type: AttributeError Error Value: zone How is it called?
Implicitly (<dtml-var myPythonScript>) or explicitly?
In the second case, you must pass the namespace yourself! In the first case, check in the binding tab that the binding really took place.
PythonScripts are quite picky with the binding comments at their start. A minor typo and part of the comments are no longer recognized.
My variable is named "zone". The following Python-statement yields 1: return namespace.has_key("zone") The following calls yield an error (each one): return namespace.items() return namespace.keys() return getattr(namespace, "zone") I am using Zope-2.4.0 --- A second test with explicit calling: DTML method: <dtml-let xy="test"> <dtml-var expr="get_status_a(ns=_)"> </dtml-let> python script get_status_a: ## Script (Python) "get_status_a" ##bind container=container ##bind context=context ##bind namespace=namespace ##bind script=script ##bind subpath=traverse_subpath ##parameters=ns ##title= return ns.has_key("xy") # OK, yields 1 return ns.xy # ERROR: Error Type: AttributeError, Error Value: xy
Reif Peter writes:
.... My variable is named "zone". The following Python-statement yields 1:
return namespace.has_key("zone")
The following calls yield an error (each one):
return namespace.items() return namespace.keys() return getattr(namespace, "zone") This is all as it should be!
"namespace" was obviously passed correctly, as shown by the success of 'namespace.has_key("zone")'. "namespace.items" and "namespace.keys" fail because the DTML namespace implements the mapping API only partially: "has_key" and subscription "[...]". The bound variables are exposed via subscription and not as attributes. Therefore, 'namespace["zone"]' will work while "namespace.zone" and the equivalent 'getattr(namespace,"zone")' will fail. In some cases, you will want to use "namespace.getitem('zone')" instead of "namespace['zone']". Please read the "Name Lookup" and "DTML" sections of <http://www.dieter.handshake.de/pyprojects/zope/book/chap3.html> Most of your problems and questions are addressed there. Dieter
participants (2)
-
Dieter Maurer -
Reif Peter