return structured result from Python scripts for page templates
Hi, I would like to return a structured result (an "object") from a Python script to use it conveniently in a page template. E.g. I would like to have a get_person Python script which gives back a person with a name and an address. Then I could use a page template like: <span tal:define="person here/get_person"> Name: <em><span tal:replace="person/name">John</span></em>, lives at <span tal:replace="person/name">London</span> </span> What I tried, but did not work: 1. Create an empty object in the Python script, set some attribute, give back the result class Person: pass p = Person() p.name = "Mike" ... return p Result: Error Type: TypeError Error Value: attribute-less object (assign or del) 2. Create an object with functions class Person: def name(self): return "Mike" ... result: Resource not found Sorry, the requested resource does not exist. Check the URL and try again. Resource: ?.Person instance at 0157E384 3. Give back a dictionary return {'name':'Mike', 'address':'Paris'} the script now works, but I cannot access the values from the page template with the person/name notation. Any ideas how to achieve this functionality in ZOPE? I think it is so basic, that surely exists a way to do this. Gabor
Gabor Nagypal wrote:
I would like to return a structured result (an "object") from a Python script to use it conveniently in a page template. E.g. I would like to have a get_person Python script which gives back a person with a name and an address. Then I could use a page template like:
I don't think you can create objects in a Python script. You need to create an external script for that. Otherwise your code looks dandy. regards Max M -- "Sorry I would Really Like To Help More On This Project, But Am To Busy Doing Paid Work On A Tight Deadline" Max M
I tried to create external script, but this does not help. The result is practically the same. Anyway, I do not insist on creating objects. I just would like to have a way to return a structured result from a Python script, where I can access the named attributes easily. I have seen an example in a how-to, how to do it with dictionaries and DTML (specifying a "mapping" parameter for dtml-in), but it does not work for page templates (or at least the simple item/attribute notation does not work, and the mapping parameter does not exist in TALES path expressions). Gabor Max M wrote:
Gabor Nagypal wrote:
I would like to return a structured result (an "object") from a Python script to use it conveniently in a page template. E.g. I would like to have a get_person Python script which gives back a person with a name and an address. Then I could use a page template like:
I don't think you can create objects in a Python script. You need to create an external script for that.
Otherwise your code looks dandy.
regards Max M
Oops, it was my mistake. The dictionary solution works! I do not know, why has it failed for the first time. I think the stuff is interesting enough to explain short the solution here. So the solution is: Python script get_persons: result = [] result.append({'name':'Jack','title':'Mr.'}) result.append({'name':'Joe','title':'Mr.'}) result.append({'name':'Mary','title':'Ms.'}) return result A possible page template: <html> <head> <title tal:content="template/title">The title</title> </head> <body> <table> <tr tal:repeat="item container/get_persons"> <td tal:content="item/name">Kate</td> <td tal:content="item/title">Mrs.</td> </tr> </table> </body> </html> Nagypal Gabor wrote:
I tried to create external script, but this does not help. The result is practically the same. Anyway, I do not insist on creating objects. I just would like to have a way to return a structured result from a Python script, where I can access the named attributes easily. I have seen an example in a how-to, how to do it with dictionaries and DTML (specifying a "mapping" parameter for dtml-in), but it does not work for page templates (or at least the simple item/attribute notation does not work, and the mapping parameter does not exist in TALES path expressions).
Gabor
Max M wrote:
Gabor Nagypal wrote:
I would like to return a structured result (an "object") from a Python script to use it conveniently in a page template. E.g. I would like to have a get_person Python script which gives back a person with a name and an address. Then I could use a page template like:
I don't think you can create objects in a Python script. You need to create an external script for that.
Otherwise your code looks dandy.
regards Max M
_______________________________________________ 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 )
[Nagypal Gabor]
Anyway, I do not insist on creating objects. I just would like to have a way to return a structured result from a Python script, where I can access the named attributes easily.
Example 1: Script named "script_test": return [1,2] Typical usage in DTML document: <dtml-var "script_test()[1]"> ===================== Example 2: Script: return {"a":1,"b":2} Typical usage in DTML: <dtml-var "script_test()['a']"> ======================== Example 3; Script: p={"a":1,"b":2} return p Same usage as Example 2. Should work in ZPT too. Cheers, Tom P
Use an external method, and return instances of "security-aware" classes. See the section entitled "Class Security Assertions In Non-Product Code (External Methods/Python Scripts)" in the Zope Developer's Guide Security chapter (http://www.zope.org/Documentation/ZDG/Security.stx). I usually just return a dictionary for this purpose, though. That's really all an instance is anyway if you don't have any methods. ;-) - C Nagypal Gabor wrote:
I tried to create external script, but this does not help. The result is practically the same. Anyway, I do not insist on creating objects. I just would like to have a way to return a structured result from a Python script, where I can access the named attributes easily. I have seen an example in a how-to, how to do it with dictionaries and DTML (specifying a "mapping" parameter for dtml-in), but it does not work for page templates (or at least the simple item/attribute notation does not work, and the mapping parameter does not exist in TALES path expressions).
Gabor
Max M wrote:
Gabor Nagypal wrote:
I would like to return a structured result (an "object") from a Python script to use it conveniently in a page template. E.g. I would like to have a get_person Python script which gives back a person with a name and an address. Then I could use a page template like:
I don't think you can create objects in a Python script. You need to create an external script for that.
Otherwise your code looks dandy.
regards Max M
_______________________________________________ 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 )
-- Chris McDonough Zope Corporation http://www.zope.org http://www.zope.com "Killing hundreds of birds with thousands of stones"
Gabor Nagypal wrote:
I would like to return a structured result (an "object") from a Python script to use it conveniently in a page template.
As you've discovered, a dictionary will work. It's not hard to create what you originally wanted, though. Adding the following lines before the last line of Products/PythonScripts/standard.py will do it: from ZPublisher.HTTPRequest import record security.declarePublic('Object') class Object(record): _guarded_writes = 1 def __setitem__(self, key, value): self.__dict__[str(key)] = value This will allow you to write code like this: from Products.PythonScripts.standard import Object o = Object() o.x = 1 o['spam!'] = 'Make Money Fast' return o Cheers, Evan @ 4-am
Hi Evan, Evan Simpson writes:
As you've discovered, a dictionary will work. It's not hard to create what you originally wanted, though. Adding the following lines before the last line of Products/PythonScripts/standard.py will do it:
from ZPublisher.HTTPRequest import record
security.declarePublic('Object') class Object(record): _guarded_writes = 1
def __setitem__(self, key, value): self.__dict__[str(key)] = value
Thank you for the tip. It should go into Zope 2.6, if possible in any way. Dieter
participants (7)
-
Chris McDonough -
Dieter Maurer -
Evan Simpson -
Gabor Nagypal -
Max M -
Nagypal Gabor -
Thomas B. Passin