[Zope] __bobo_traverse__ (Cont.)

Garito garito at sistes.net
Tue Aug 31 12:12:27 EDT 2004


Johan Carlsson wrote:

> Garito wrote:
>
>> Hi
>> In my last mail I ask you (thanks for your responses) about an error 
>> publishing the object in __bobo_traverse__
>> The solution passes by returning an object instead a string (I would 
>> like to return a string :( )
>> Then my __bobo_traverse__ is something like:
>>
>> def __bobo_traverse__(self, REQUEST, name):
>>    obj = getattr(self, name, None)
>>        if obj is not None:
>>            return obj
>>        else:
>>            return getattr(self, 'Result')
>>
>> But I need to process some information in bobo_traverse before 
>> returns Result
>> If you change the line return getattr(self, 'Result') for return 
>> getattr(self, 'Result')('someresults') doesn't work
>> For that I need to do something like:
>>
>> _Results = dict() # I put these declaration in my class
>>
>> def __bobo_traverse__(self, REQUEST, name):
>>    obj = getattr(self, name, None)
>>        if obj is not None:
>>            return obj
>>        else:
>>            self._Results[str(REQUEST.SESSION.id)] = self.MyResult()
>>            return getattr(self, 'Result')
>>
>> def MyResult(self):
>>    """MyResult"""
>>    return 'This is my test result'
>>
>> def GetResults(self, Session):
>>    """ GetResults"""
>>    return self._Results[str(Session)]
>>
>> Then I create a Python Script called Result like:
>> return context.GetResults(context.REQUEST.SESSION.id)
>> But all these code raise:
>>
>> Traceback (innermost last):
>>  Module ZPublisher.Publish, line 91, in publish
>>  Module ZPublisher.BaseRequest, line 302, in traverse
>>  Module Products.MyProducts.MyProduct, line 116, in __bobo_traverse__
>> TypeError: object does not support item assignment
>>
>> I can't undersand why can't I assign a dictionary. I can understand 
>> if I try to assign a number like dictionary key (but I convert the 
>> session id to a string)
>
>
> Exactly what is self._Results when you're trying to assign it.
> It sounds to me that it's not what you think it is.
> Remember Python is a Dynamically Typed language :-)
> I'd check that anyway.
>
> Second, from your example "return getattr(self, 'Result')" doesn't 
> make sense because Result is never set (as far as the exemple code goes).
>
> All this seems very complicated though, I think there can be a cleaner
> solution (for instance returning a object that does the calculation:
>
>
> def __bobo_traverse__(self, REQUEST, name):
>     obj = getattr(self, name, None)
>     if obj is not None:
>         return obj
>     else:
>     brain=CalculatingBrain(REQUEST.SESSION.id, or what ever
>             data is needed )
>         return brain
>
> with:
>
> class CalculatingBrain(Implicit):
>
>     security = ClassSecurityInfo()
>     security.declareObjectProtected(View) #Or what ever.
>     def __init__(self, session_id, or what ever):
>     pass or do stuff
>     
>     def MyProduct(self, REQUEST=None):
>     """You could even make this traversable if you'd like"""
>     return 'Result'
>
>
>     
>
Thanks again Johan for your response
getattr(self, 'Result') is a python script inside my product (my product
is a container)
I can't undersant the complexity in these case
Perhaps could be interesant to explain what I try to do before continue
these conversation
I try to process TraversalRequestNameStack (traverse_subpath) as user query
Then I would like to generate the response and serve it to the user
For example:
user tye some thing like:
http://myserver:myport/MyProduct/Value1
User wants Value1 in MyProduct
Value1 is a processed value that returns: 'This is the result of
precessing Value1'
How could I do it? (Perhaps with these point of view we can understand
ourselves better)

My thoughts:
Override __bobo_traverse__ to process TraversalRequestNameStack because
traverse_subpath is not available in products
Then return the results to be printed in the screen

I assume these would be truly in fact easier :'(

These explanation illuminate you? (I hope so!!!)

THANKS AGAIN

PD: Sorry for my english!



More information about the Zope mailing list