[Zope] Deleting several items from BTrees

Oliver Bleutgen myzope@gmx.net
Mon, 07 Apr 2003 18:28:30 +0200


Thomas Guettler wrote:
> Hi!
> 
> I had problems to delete all entries from a BTree.
> The working code is now:
> 
> (self.objects is a BTreeFolder2)
> 
>     def deleteAllObjects(self):
>         "docstring"
>         keys=[]
>         for k in self.objects.keys():
>             keys.append(k)
>         len_keys=len(keys)
>         i=0
>         for k in keys:
>             self.objects[k].deleteObject()
>             i+=1
>         ret=[]
>         ret.append(self.standard_html_header())
>         ret.append("All objects where deleted %s %s" % (len_keys, i))
>         ret.append(self.standard_html_footer())
>         return ''.join(ret)
>  
> This does not work:
>  for k in self.objects.values():
>      k.deleteObject() # k gets deleted from self.objects
> since deleting while iterating does not work.
> 
> This code does not look very nice. Is there a better way? It would be
> nice to use just on loop.

Would you also go without any explicit loops? ;)

self.manage_delObjects(self.objectIds())

HTH,
oliver