I have a SQL query that I need to wrap in a Python script. Fine and dandy. I'd like the results of the python call to be accessable with dtml-in but with custom field/attribute names. I other words, I want to be able to do this: --- dtml --- <dtml-in MyPythonScript> <dtml-var favorite_food> <dtml-var favorite_color> </dtml-in> And this: --- MyPythonScript --- rawresults = context.query_favorites() results = [] for rawitem in rawresults: food, color = results[0].bigfield.split(',') results.append(???) return results ---- What would I place in the ??? field? I've tried: A dictionary ("{'favorite_food':food, 'favorite_color':color}"). An object (placing a small class in the script. Fails to create the object.) I've also tried adding my attributes to the existing result objects but that also fails. Suggestions? This seems like it should be pretty easy.
Charlie, What you want is a Pluggable Brain. Have a look in the Zope Book chapter on relational database integration: there are some examples. Create a file in your Extensions folder: --- file "somefile.py" --- import string class MyBrain: def fav_food(self): return string.split(self.bigfield, ',')[0] def fav_color(self): return string.split(self.bigfield, ',')[1] Then in the Advanced tab of your SQL query, enter the filename and the name of the class in the box where it asks what class to bind to the results. Now in the DTML you can write: <dtml-in query_favourites> <dtml-var fav_food> <dtml-var fav_color> <dtml-in> and it should work. To be honest my DTML is rusty - I use ZPT more often these days - but I think this should work, modulo typos/brainos (I haven't tested any of this code.) David
Charlie, You want to use a dictionary. Should work just fine without having to resort to external file system code. Let me know if you need an example of this. Kevin -----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Charlie Reiman Sent: Friday, July 05, 2002 6:02 PM To: zope@zope.org Subject: [Zope] dtml-in over python script I have a SQL query that I need to wrap in a Python script. Fine and dandy. I'd like the results of the python call to be accessable with dtml-in but with custom field/attribute names. I other words, I want to be able to do this: --- dtml --- <dtml-in MyPythonScript> <dtml-var favorite_food> <dtml-var favorite_color> </dtml-in> And this: --- MyPythonScript --- rawresults = context.query_favorites() results = [] for rawitem in rawresults: food, color = results[0].bigfield.split(',') results.append(???) return results ---- What would I place in the ??? field? I've tried: A dictionary ("{'favorite_food':food, 'favorite_color':color}"). An object (placing a small class in the script. Fails to create the object.) I've also tried adding my attributes to the existing result objects but that also fails. Suggestions? This seems like it should be pretty easy. _______________________________________________ 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 )
Dictionaries don't work, as I mentioned. That is, I can return dictionaries but then I have to access them in DTML will cruft like: <dtml-var expr="_['sequence-item']['favorite_food']"> which strikes me as ridiculous. It's just personal opinion though. Did you have some other way to use dictionaries? Someone already suggested I use a brain, which does what I want but in what I consider a rather awkward way since I now need to provide external code.
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Kevin Carlson Sent: Sunday, July 07, 2002 1:01 PM To: Charlie Reiman; zope@zope.org Subject: RE: [Zope] dtml-in over python script
Charlie,
You want to use a dictionary. Should work just fine without having to resort to external file system code. Let me know if you need an example of this.
Kevin
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Charlie Reiman Sent: Friday, July 05, 2002 6:02 PM To: zope@zope.org Subject: [Zope] dtml-in over python script
I have a SQL query that I need to wrap in a Python script. Fine and dandy. I'd like the results of the python call to be accessable with dtml-in but with custom field/attribute names.
I other words, I want to be able to do this:
--- dtml --- <dtml-in MyPythonScript> <dtml-var favorite_food> <dtml-var favorite_color> </dtml-in>
And this:
--- MyPythonScript --- rawresults = context.query_favorites() results = [] for rawitem in rawresults: food, color = results[0].bigfield.split(',') results.append(???)
return results
----
What would I place in the ??? field? I've tried:
A dictionary ("{'favorite_food':food, 'favorite_color':color}").
An object (placing a small class in the script. Fails to create the object.)
I've also tried adding my attributes to the existing result objects but that also fails.
Suggestions? This seems like it should be pretty easy.
_______________________________________________ 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 )
_______________________________________________ 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 )
What you need is the mapping keyword: <dtml-in MyPythonScript mapping> <dtml-var favorite_food> </dtml-in> Kevin -----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Charlie Reiman Sent: Monday, July 08, 2002 12:51 PM To: Kevin Carlson; zope@zope.org Subject: RE: [Zope] dtml-in over python script Dictionaries don't work, as I mentioned. That is, I can return dictionaries but then I have to access them in DTML will cruft like: <dtml-var expr="_['sequence-item']['favorite_food']"> which strikes me as ridiculous. It's just personal opinion though. Did you have some other way to use dictionaries? Someone already suggested I use a brain, which does what I want but in what I consider a rather awkward way since I now need to provide external code.
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Kevin Carlson Sent: Sunday, July 07, 2002 1:01 PM To: Charlie Reiman; zope@zope.org Subject: RE: [Zope] dtml-in over python script
Charlie,
You want to use a dictionary. Should work just fine without having to resort to external file system code. Let me know if you need an example of this.
Kevin
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Charlie Reiman Sent: Friday, July 05, 2002 6:02 PM To: zope@zope.org Subject: [Zope] dtml-in over python script
I have a SQL query that I need to wrap in a Python script. Fine and dandy. I'd like the results of the python call to be accessable with dtml-in but with custom field/attribute names.
I other words, I want to be able to do this:
--- dtml --- <dtml-in MyPythonScript> <dtml-var favorite_food> <dtml-var favorite_color> </dtml-in>
And this:
--- MyPythonScript --- rawresults = context.query_favorites() results = [] for rawitem in rawresults: food, color = results[0].bigfield.split(',') results.append(???)
return results
----
What would I place in the ??? field? I've tried:
A dictionary ("{'favorite_food':food, 'favorite_color':color}").
An object (placing a small class in the script. Fails to create the object.)
I've also tried adding my attributes to the existing result objects but that also fails.
Suggestions? This seems like it should be pretty easy.
_______________________________________________ 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 )
_______________________________________________ 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 )
_______________________________________________ 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 )
Charlie Reiman wrote:
Dictionaries don't work, as I mentioned. That is, I can return dictionaries but then I have to access them in DTML will cruft like:
<dtml-var expr="_['sequence-item']['favorite_food']">
which strikes me as ridiculous. It's just personal opinion though.
so don't use DTML ;-) in ZPT, it would be something like: <tal:x repeat="item here/yourPythonScript"> <tal:x replace="item/favourite_food"> </tal:x> cheers, Chris
Charlie Reiman writes:
I have a SQL query that I need to wrap in a Python script. Fine and dandy. I'd like the results of the python call to be accessable with dtml-in but with custom field/attribute names.
I other words, I want to be able to do this:
--- dtml --- <dtml-in MyPythonScript> <dtml-var favorite_food> <dtml-var favorite_color> </dtml-in>
And this:
--- MyPythonScript --- rawresults = context.query_favorites() results = [] for rawitem in rawresults: food, color = results[0].bigfield.split(',') results.append(???) You may use a dictionary: " {'food':food, 'color':color}".
You can access the result with <dtml-in ... mapping> Dieter
participants (5)
-
Charlie Reiman -
Chris Withers -
David Loeffler -
Dieter Maurer -
Kevin Carlson