[Grok-dev] Deleting Container items

Chris McDonough chrism at plope.com
Fri Mar 6 07:08:16 EST 2009


I assume grok.Container uses a BTree to hold its subobjects.  You shouldn't
mutate a BTree while you iterate over its keys and values.  So this might work
instead:

         atnames=list(self.context['ar'].keys())
         for x in atnames:
             del self.context['ar'][x]

Tim Cook wrote:
> Okay, what is wrong with this code?
> 
> I have a grok.Container named 'ar'
> 
> I want to be able to completely empty it at certain times.
> 
> When I execute the following code it deletes exactly half of the
> contents.  For example if I have 4 items it deletes 2.  If I execute it
> again it deletes 1.  Finally on the 3rd execution it deletes the last
> one.
> 
> 
> class Emptyar(grok.View):
>     grok.context(oship)
>     
>     def render(self):
>         atnames=self.context['ar'].keys()
> 
>         for x in atnames:
>             del self.context['ar'][x]
> 
> 
> I even tried self.context['ar'].__delattr__(x) and had exactly (not
> surprising I suppose) the same effect.
> 
> Thanks,
> Tim
> 
> 
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev



More information about the Grok-dev mailing list