Hi all, I (i am a newbie) have written a simple python script that queries a MS-SQL database and captues the result set into a list. I have called this sccipt in a dtml document and want to iterate through the list elements. When i do this- <dtml-in expr="Python_script()"> <li> <dtml-var text> </li> </dtml-in> where, "text" is the list that is returned by the python scirpt. It says - Error Type: KeyError Error Value: text My question is, how do i iterate through the list and obtain the values from the list. Thanking you in advance Madhavi Bagepalli
<mbagepll@memphis.edu> writes:
<dtml-in expr="Python_script()"> <li> <dtml-var text> </li> </dtml-in>
with <dtml-var sequence-item> you get the elements of your list. If these elements have attributes that you want to access then DTML provides them directly with their name. Suppose you have list elements with the attributes name and price you would use: <dtml-in expr="Python_script()"> <dtml-if sequence-start> <ul> </dtml-if> <li><dtml-var name>: <dtml-var price></li> <dtml-if sequence-end> </ul> </dtml-if> <dtml-else> <p>Sorry, nothing found.</p> </dtml-in> Regards, Frank
[Frank Tegtmeyer]
<mbagepll@memphis.edu> writes:
<dtml-in expr="Python_script()"> <li> <dtml-var text> </li> </dtml-in>
with <dtml-var sequence-item> you get the elements of your list. If these elements have attributes that you want to access then DTML provides them directly with their name. Suppose you have list elements with the attributes name and price you would use:
<dtml-in expr="Python_script()"> <dtml-if sequence-start> <ul> </dtml-if> <li><dtml-var name>: <dtml-var price></li> <dtml-if sequence-end> </ul> </dtml-if> <dtml-else> <p>Sorry, nothing found.</p> </dtml-in>
Frank, I'm going take exception to your way of using "if" statements separately around start and end tags (I mean the <ul> tags). It's true that it works here, but it's better not to produce unbalanced parts in a markup element in separate and uncoordinated places. If you were to try to port this to something written in xml, where the tags have to nest properly, for example, it wouldn't work. I'm suggesting that it is a good idea to try produce things that have to be balanced in a balanced way, even though it may not be strictly necessary. It will be better in the long run, and less prone to error if something doesn't work as expected inside statement blocks. <ul> tags have to be balanced. In this case, it oculd be done like this: <ul> <dtml-in expr="Python_script()"> .... </dtml-in> </ul> If you don't want to have the possiblity of an empty <ul> element, you could write <dtml-let List=Python_script> <dtml-if List> <ul> <dtml-in List> ... </dtml-in> </ul> </dtml-if> </dtml-let> Cheers, Tom P
"Thomas B. Passin" <tpassin@mitretek.org> writes:
If you don't want to have the possiblity of an empty <ul> element, you could write
<dtml-let List=Python_script> <dtml-if List> <ul> <dtml-in List> ... </dtml-in> </ul> </dtml-if> </dtml-let>
Yes, that's definitely cleaner. Does it double the necessary storage for the list? I think dtml-in has to have the list in memory to provide it's attributes. If the list is huge this may be a problem, but in this case dtml-in is a problem by itself anyway. Regards, Frank
[Frank Tegtmeyer]
"Thomas B. Passin" <tpassin@mitretek.org> writes:
If you don't want to have the possiblity of an empty <ul> element, you could write
<dtml-let List=Python_script> <dtml-if List> <ul> <dtml-in List> ... </dtml-in> </ul> </dtml-if> </dtml-let>
Yes, that's definitely cleaner. Does it double the necessary storage for the list? I think dtml-in has to have the list in memory to provide it's attributes. If the list is huge this may be a problem, but in this case dtml-in is a problem by itself anyway.
I don't think that it doubles. In <dtml-in List>, List is only a reference to the variable List that was created in the <dtml-let>, it's not a copy (I'm "sure" but don't truly know how Zope implements dtml-in. I've always thought that it's based on Python's "for a in b" construction). That is, there is only one instance of List. And it's better to create it using dtml-let because then it will go away when the page is done, whereas using REQUEST.set() is said to lead to memory leaks on occasion (apparently some references to the REQUEST may remain somewhere else, preventing garbage collection). Now if you had used Python_script (instead of List) both in dtml-if and dtml-in, you __would__ have incurred both the memory and the time to create the object twice. That's why I created List, so it only has to be done once. Cheers, Tom P
[<mbagepll@memphis.edu>]
I (i am a newbie) have written a simple python script that queries a MS-SQL database and captues the result set into a list. I have called this sccipt in a dtml document and want to iterate through the list elements. When i do this- <dtml-in expr="Python_script()"> <li> <dtml-var text> </li> </dtml-in>
where, "text" is the list that is returned by the python scirpt. It says - Error Type: KeyError Error Value: text
My question is, how do i iterate through the list and obtain the values from the list.
You're going to have to supply some more information. Like, what is the query, what is the script (or at least the signicant parts of it), is there a column name in the data from the database called "text", what does the script return, and so on. This is probably a job done better and easier by a ZSQL method than by a python script, but you might still end up with the same problem, depending on the answers to the questions. Cheers, Tom P
participants (3)
-
Frank Tegtmeyer -
mbagepll@memphis.edu -
Thomas B. Passin