Hello, I am looping through a list of dictionary objects using <dtml-in... and within the dtml-in body I have: <dtml-var "_.getitem('sequence-item')['titled']" missing="none"> Some of the dictionaries in the list don't have 'titled' key so it should replace it with the word "none", but it gives me an error Error Type: KeyError Error Value: 'titled' I don't understand. Another option is that I can use <dtml-if "_.getitem('sequence-item').has_key('titled')"> but it doesn't work, says has_key is an unknown attribute. This is the stupidest thing ever! it's a dictionary object! I try to typecast it using the dict(_.getitem('sequence-item')).has_key('titled') it gives me an error saying it can't typecast, but I am 100% sure it's a dictionary object because I can access it easily _.getitem('sequence-item')['titled'] for those that have that key. Any idea how I can check if the titled key is in the dictionary for the current list item? Thanks in advance guys! --------------------------------- Yahoo! Messenger with Voice. PC-to-Phone calls for ridiculously low rates.
It might make your life easier if you used a dtml-let (WARNING: untested): <dtml-in dictList="ScriptThatReturnsAListofDicts()"> <dtml-let aDict="_['sequence-item']"> <dtml-if "aDict.has_key('titled')"> do something here But this is still pretty ugly. Another approach would be to clean up the data in your list/dicts within the python script, before you get to the dtml method. Jonathan ----- Original Message ----- From: Alric Aneron To: zope@zope.org Sent: Monday, June 19, 2006 2:46 PM Subject: [Zope] dictionary from sequence-item Hello, I am looping through a list of dictionary objects using <dtml-in... and within the dtml-in body I have: <dtml-var "_.getitem('sequence-item')['titled']" missing="none"> Some of the dictionaries in the list don't have 'titled' key so it should replace it with the word "none", but it gives me an error Error Type: KeyError Error Value: 'titled' I don't understand. Another option is that I can use <dtml-if "_.getitem('sequence-item').has_key('titled')"> but it doesn't work, says has_key is an unknown attribute. This is the stupidest thing ever! it's a dictionary object! I try to typecast it using the dict(_.getitem('sequence-item')).has_key('titled') it gives me an error saying it can't typecast, but I am 100% sure it's a dictionary object because I can access it easily _.getitem('sequence-item')['titled'] for those that have that key. Any idea how I can check if the titled key is in the dictionary for the current list item? Thanks in advance guys! ------------------------------------------------------------------------------ Yahoo! Messenger with Voice. PC-to-Phone calls for ridiculously low rates. ------------------------------------------------------------------------------ _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Thanks, I tried your suggestion Jonathan and I get this: Error Type: AttributeError Error Value: has_key Thanks anyway. I never used ZPT, I'm not sure how to approach it from that angle... would it be easy to do? (You mean tal and metal right?) Anyone have any other suggestions? Thanks Jonathan <dev101@magma.ca> wrote: It might make your life easier if you used a dtml-let (WARNING: untested): <dtml-in dictList="ScriptThatReturnsAListofDicts()"> <dtml-let aDict="_['sequence-item']"> <dtml-if "aDict.has_key('titled')"> do something here But this is still pretty ugly. Another approach would be to clean up the data in your list/dicts within the python script, before you get to the dtml method. Jonathan ----- Original Message ----- From: Alric Aneron To: zope@zope.org Sent: Monday, June 19, 2006 2:46 PM Subject: [Zope] dictionary from sequence-item Hello, I am looping through a list of dictionary objects using <dtml-in... and within the dtml-in body I have: <dtml-var "_.getitem('sequence-item')['titled']" missing="none"> Some of the dictionaries in the list don't have 'titled' key so it should replace it with the word "none", but it gives me an error Error Type: KeyError Error Value: 'titled' I don't understand. Another option is that I can use <dtml-if "_.getitem('sequence-item').has_key('titled')"> but it doesn't work, says has_key is an unknown attribute. This is the stupidest thing ever! it's a dictionary object! I try to typecast it using the dict(_.getitem('sequence-item')).has_key('titled') it gives me an error saying it can't typecast, but I am 100% sure it's a dictionary object because I can access it easily _.getitem('sequence-item')['titled'] for those that have that key. Any idea how I can check if the titled key is in the dictionary for the current list item? Thanks in advance guys! --------------------------------- Yahoo! Messenger with Voice. PC-to-Phone calls for ridiculously low rates. --------------------------------- _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev ) --------------------------------- Love cheap thrills? Enjoy PC-to-Phone calls to 30+ countries for just 2¢/min with Yahoo! Messenger with Voice.
Alric Aneron wrote:
Hello, I am looping through a list of dictionary objects using <dtml-in... and within the dtml-in body I have: <dtml-var "_.getitem('sequence-item')['titled']" missing="none"> Some of the dictionaries in the list don't have 'titled' key so it should replace it with the word "none", but it gives me an error *Error Type: KeyError* *Error Value: 'titled' * I don't understand. Another option is that I can use <dtml-if "_.getitem('sequence-item').has_key('titled')"> but it doesn't work, says has_key is an unknown attribute. This is the stupidest thing ever! it's a dictionary object! I try to typecast it using the dict(_.getitem('sequence-item')).has_key('titled') it gives me an error saying it can't typecast, but I am 100% sure it's a dictionary object because I can access it easily _.getitem('sequence-item')['titled'] for those that have that key.
Any idea how I can check if the titled key is in the dictionary for the current list item?
Thanks in advance guys!
------------------------------------------------------------------------
Alric, Your earlier post indicated that your DTML obtains the sequence of dictionaries from a python script - and *then* tries to filter it and process it. Why not do your filtering, e.g. appending only thoses values that have a key == 'key1' in your python script first? Thats why "god" invented python scripts :-) . And as Jonathan indicated, you should also simplify the list returned. I would just return a list of filtered values, e.g. # ----------------------------------------------- # python script (based on your earlier message) # ----------------------------------------------- # DTML call to this script can pass these as parameters or obtain them from the request ... mydict = {'key1': 'value1', 'key2': 'value2'} mydict2 = {'key1': 'value3', 'key2': 'value4'} desired_key = 'key1' #results bucket finalList = [] for dict in (mydict,mydict2): # we avoid returning list of dictionaries which can be awkward in DTML for k in dict.keys(): if k == desired_key: # <------- filter it here so DTML doesn't have to finalList.append( dict[ k ] ) return finalList Now your DTML is simple. David
participants (3)
-
Alric Aneron -
David H -
Jonathan