[Zope] Search a Catalog in Python

Max M maxm at mxm.dk
Tue Mar 16 12:50:25 EST 2004


Dragos Chirila wrote:
> Hi
> 
> I am doing something similarly.
> 
>     def __searchCatalog(self, p_criteria):
>         """Search catalog"""
>         return self.__catalog(p_criteria)
> 
> This returns a list of mybrains objects.
> 
>     def __getFoundObjects(self, p_list):
>         """gets objects from catalog results"""
>         return map(self.__catalog.getobject, map(getattr, p_list,
> ('data_record_id_',)*len(p_list)))
> 
> It gets a list of mybrains objects and returns a list of zope objects.

Hmm! I think that is a questionable approach.

If an object is in the catalog, but has been deleted from the ZODB, you 
will get an error.

I have had this happen a few times, and it is irritating.

I don't know how it can happen. Perhaps poorly written products can do 
it. I dont remember.


This approach should be failsafe

result = []
for brain in brains:
     try:
         obj = brain.getObject()
         result.append(obj)
     except: # Will probably raise AttributeError on None object
         pass


-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Zope mailing list