Help with Zope Python result object please!
hello, I have a python code that recieves two result objects from two different ZSQL methods. I need to join them (sort of like UNION). I can't do result1+result2, otherwise it raises an exception that the + operator isn't supported. When I try to do this (read below, I explained what i've done if you are lazy to read the code) result1 = context.zsqlmethod1() result2 = context.zsqlmethod2() if (len(result1) == 0): return result2 row1 = 0 row2 = len(result1) + 1 col = 0 for r in result2: col = 0 if (context.duplicateExists(result1, result2[row1][0]) == -1): result1[row2][col] = result2[row][col] col = col + 1 row1 = row1 + 1 row2 = row2 + 1 return result1 Basically, it loops through the rows and columns of result2 and adds them to the end of the result 1. However it for some reason raises "index out of range" and says that assigning result1[0][0]=result2[0][0] (if there is an element in both of them) is illegal because: Error Value: object does not support item or slice assignment I realize that in most programming languages you can't add them to result1 without resizing it first, but I can't find much documentaiton on member functions of the result object, so I can't resize it. I am completely stuck, I've tried everything, spent so much time on it, can anyone please help save my sanity :P Thanks in advance guys! I really appreciate it. --------------------------------- Yahoo! DSL Something to write home about. Just $16.99/mo. or less
Alric Aneron wrote:
hello, I have a python code that recieves two result objects from two different ZSQL methods. I need to join them (sort of like UNION). I can't do result1+result2, otherwise it raises an exception that the + operator isn't supported. When I try to do this (read below, I explained what i've done if you are lazy to read the code) result1 = context.zsqlmethod1() result2 = context.zsqlmethod2() if (len(result1) == 0): return result2 row1 = 0 row2 = len(result1) + 1 col = 0 for r in result2: col = 0 if (context.duplicateExists(result1, result2[row1][0]) == -1): result1[row2][col] = result2[row][col] col = col + 1 row1 = row1 + 1 row2 = row2 + 1
return result1
Basically, it loops through the rows and columns of result2 and adds them to the end of th e result 1. However it for some reason raises "index out of range" and says that assigning result1[0][0]=result2[0][0] (if there is an element in both of them) is illegal because: *Error Value: object does not support item or slice assignment *I realize that in most programming languages you can't add them to result1 without resizing it first, but I can't find much documentaiton on member functions of the result object, so I can't resize it. I am completely stuck, I've tried everything, spent so much time on it, can anyone please help save my sanity :P
Thanks in advance guys! I really appreciate it.
------------------------------------------------------------------------ Y <http://pa.yahoo.com/*http://us.rd.yahoo.com/evt=37474/*http://promo.yahoo.com/broadband/%20>
Alric, The merging of two zsql result sets is easy - here's one way: newResults = [] for r in resultset1: newResults.append( r ) for r in resultset2: newResults.append( r ) return newResults. David
Thank you sir! Sorry, I am very new to python. It's quite unlike other languages. Happy new year! Al David H <bluepaul@earthlink.net> wrote: Alric Aneron wrote: hello, I have a python code that recieves two result objects from two different ZSQL methods. I need to join them (sort of like UNION). I can't do result1+result2, otherwise it raises an exception that the + operator isn't supported. When I try to do this (read below, I explained what i've done if you are lazy to read the code) result1 = context.zsqlmethod1() result2 = context.zsqlmethod2() if (len(result1) == 0): return result2 row1 = 0 row2 = len(result1) + 1 col = 0 for r in result2: col = 0 if (context.duplicateExists(result1, result2[row1][0]) == -1): result1[row2][col] = result2[row][col] col = col + 1 row1 = row1 + 1 row2 = row2 + 1 return result1 Basically, it loops through the rows and columns of result2 and adds them to the end of th e result 1. However it for some reason raises "index out of range" and says that assigning result1[0][0]=result2[0][0] (if there is an element in both of them) is illegal because: Error Value: object does not support item or slice assignment I realize that in most programming languages you can't add them to result1 without resizing it first, but I can't find much documentaiton on member functions of the result object, so I can't resize it. I am completely stuck, I've tried everything, spent so much time on it, can anyone please help save my sanity :P Thanks in advance guys! I really appreciate it. --------------------------------- Y Alric, The merging of two zsql result sets is easy - here's one way: newResults = [] for r in resultset1: newResults.append( r ) for r in resultset2: newResults.append( r ) return newResults. David --------------------------------- Yahoo! Photos Ring in the New Year with Photo Calendars. Add photos, events, holidays, whatever.
At Friday 30/12/2005 14:32, Alric Aneron wrote:
I have a python code that recieves two result objects from two different ZSQL methods. I need to join them (sort of like UNION). I can't do result1+result2, otherwise it raises an exception that the + operator isn't supported.
This is *untested* but looking at the Results constructor, it does not use its data argument in any way (just stores it in the instance). If you are *sure* both results have the same columns in the same position and all are the same type and size and..., you could do: R1._data = R1._data + R2._data (in a product or external method) Or, create a new instance: Results((R1.__items__, R1._data + R2._data)) Gabriel Genellina Softlab SRL
participants (3)
-
Alric Aneron -
David H -
Gabriel Genellina