Unexpected Behaviour iterating over catalog search...
Hi, I have something like this: for brain in Catalog(some_index=some_value): # delete the object, and then Catalog.uncatalog_object(brain.getPath()) ...which wasn't deleting all objects where some_index=some_value. On a hunch, I tried: for brain in tuple(Catalog(some_index=some_value)): # delete the object, and then Catalog.uncatalog_object(brain.getPath()) ...which magically worked. Surely the thing returned by a Catalog search should be immutable? cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
Chris Withers wrote:
I have something like this:
for brain in Catalog(some_index=some_value): # delete the object, and then Catalog.uncatalog_object(brain.getPath())
...which wasn't deleting all objects where some_index=some_value.
On a hunch, I tried:
for brain in tuple(Catalog(some_index=some_value)): # delete the object, and then Catalog.uncatalog_object(brain.getPath())
...which magically worked.
Surely the thing returned by a Catalog search should be immutable?
Nope, it is "lazy"; immutability would require "realizing" it first, which would be prohibitively expensive in many cases. Wrapping 'list()' around the result set would've worked, too. Tres. -- =============================================================== Tres Seaver tseaver@zope.com Zope Corporation "Zope Dealers" http://www.zope.com
Surely the thing returned by a Catalog search should be immutable?
Nope, it is "lazy"; immutability would require "realizing" it first, which would be prohibitively expensive in many cases.
Yes .. thing is, wrapping with list() or tuple() will therefore also be prohibitive in those cases, so can't be done routinely. In those cases, what would be better? Something like for brain in Catalog(some_index=some_value): # delete the object, and then brain_to_delete = Catalog(unique_index=brain.unique_prop) Catalog.uncatalog_object(brain_to_delete.getPath()) .. can't really think why that would work if Chris's original doesn't, though. -- Jean Jordaan http://www.upfrontsystems.co.za /courses.html <-- Zope/Plone training! /kursusse.html
Jean Jordaan wrote at 2004-3-8 16:33 +0200:
Surely the thing returned by a Catalog search should be immutable?
Nope, it is "lazy"; immutability would require "realizing" it first, which would be prohibitively expensive in many cases.
Yes .. thing is, wrapping with list() or tuple() will therefore also be prohibitive in those cases,
When you want to uncatalog everything, "tuple"ing the result should not be a problem. Otherwise, a standard approach is to remember the objects you want to delete in a standard list and iterate over this list in a separate loop (outside the first one). -- Dieter
Jean Jordaan wrote:
for brain in Catalog(some_index=some_value): # delete the object, and then brain_to_delete = Catalog(unique_index=brain.unique_prop) Catalog.uncatalog_object(brain_to_delete.getPath())
.. can't really think why that would work if Chris's original doesn't, though.
...it won't, this is analagous to the code I have that fails. cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
participants (5)
-
Chris Withers -
Chris Withers -
Dieter Maurer -
Jean Jordaan -
Tres Seaver