On Thursday 06 March 2003 16:23, you wrote:
> Ok, how about this (though, I'm not sure if setting attributes in
> __init__ is safe or not... does it mean that an instance of the servlet
> will be kept around for each session until it expires?):

Uhh, I'm not sure I can follow your code :-)
Anyway, as far as I know, setting attributes in __init__ is safe, as long as 
these attributes have nothing to do with sessions. In prinziple, a session 
could live longer than a servlet as they could survive even a restart.

I liked your previous version much better.

Stephan
>
> # ---|  begin Test.py
>
> from WebKit.Page import Page
>
> class Test(Page):
>
>       def __init__ (self):
>               Page.__init__(self)
>               self.session = SessionProxy(self.session)
>
>       def _incrementX(self):
>               if not self.session.hasValue('x'):
>                       self.session.x = 0
>               self.session.x += 1
>
>       def writeContent (self):
>               self._incrementX()
>               self.write(str(self.session.x))
>
> class SessionProxy(object):
>
>       def __init__ (self, factory):
>               object.__setattr__(self, 'factory', factory)
>
>       def __getattr__ (self, attr):
>               try:
>                       return object.__getattribute__(self, attr)
>               except AttributeError:
>                       session = object.__getattribute__(self, 'factory')()
>                       if attr in dir(session):
>                               return getattr(session, attr)
>                       else:
>                               return session.value(attr)
>
>       def __setattr__ (self, attr, value):
>               session = object.__getattribute__(self, 'factory')()
>               session.setValue(attr, value)
>
> --T
>
> On Thursday, March 6, 2003, at 02:43 AM, Stephan Diehl wrote:
> > On Wednesday 05 March 2003 20:40, you wrote:
> >> I agree with the issue about confusing the beginner with a redirection
> >> like that.  Though, if you really want to, you could use a proxy and
> >> not muck with changing the getattr and setattr methods of the servlet,
> >> or the session:
> >
> > Are you sure that a servlet variable is used for one and only one
> > instance
> > between "awake" and "sleep"? Even if it is the behaviour now, won't it
> > change
> > in the future?
> > If, on the other hand, it can be assured that a servlet stays "clean"
> > between
> > awake and sleep, your solution is very elegant indeed and could be
> > easily
> > extended to be used for requests as well :-)
> >
> > Stephan
> >
> >> #------ begin Test.py
> >>
> >> from WebKit.Page import Page
> >>
> >> class Test(Page):
> >>
> >>    def awake (self, transaction):
> >>            Page.awake(self, transaction)
> >>            self.session = SessionProxy(self.session())
> >>            self.session.x = range(20)
> >>
> >>    def writeContent (self):
> >>            self.write(str(self.session.x))
> >>
> >> class SessionProxy:
> >>    def __init__ (self, session):
> >>            self.__dict__['session'] = session
> >>    def __getattr__ (self, attr):
> >>            return self.__dict__['session'].value(attr)
> >>    def __setattr__ (self, attr, value):
> >>            self.__dict__['session'].setValue(attr, value)
> >>
> >> #--- end Test.py


-------------------------------------------------------
This SF.net email is sponsored by: Etnus, makers of TotalView, The debugger 
for complex code. Debugging C/C++ programs can leave you feeling lost and 
disoriented. TotalView can help you find your way. Available on major UNIX 
and Linux platforms. Try it free. www.etnus.com
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to