Re: New session module

"Conan C. Albrecht" <conan-RXmsyT/Szrj2fBVCVOL8/[email protected]>
Newsgroups gmane.comp.python.spyce.general
Message-ID <[email protected]>
Answering your questions:

> Hi!
>
> No time to try this unfortunately but I definately agree with all your
> points.  I remember trying to use sessions for the first time and I
> almost gave up before finding session.auto.  Even with that, I do not
> like the setup required.  So, if your module does do what you say,
> then I'd vote for it replacing the current session module.  Then
> again, maybe I am missing something others can chime in on?

It does do what I said, but I'm also open to suggestions if people  
don't like my implementation.  I tested it considerably, but I've  
been known to throw a bug in my code once or twice in my life.  It  
may have a lurking bug somewhere. :)

>
> Also... concerning my last post, will your session module deal with
> non pickle able objects or at least throw an error?

The in-memory method (web server installation only) can store  
anything, pickable or not.  But the default method, dbm, can only  
take picklable objects.  There really isn't a way around this.  It's  
a fact that occurs when you try to save something to disk from any  
language, not just python.

However, I could throw a nice error explaining the situation to an  
unknowing user.  I'll do this.

>
> Cheers.
> Jon.
>
> On 12/28/05, Conan C. Albrecht <conan-RXmsyT/Szrj2fBVCVOL8/[email protected]> wrote:
>> Spyce developers (Rimon & Jonathan in particular):
>>
>> Attached to this email is a new session module, called session2.  I'd
>> like feedback on it, and if it passes muster after people comment and
>> I modify it appropriately, I'd like to either replace the existing
>> session module with it or add it as a second alternative in the main
>> source tree.
>>
>> I've never been happy with the existing session module.  I think it's
>> a weakness of Spyce right now.   I hope I'm not the only user who
>> feels this way.  (If I am, I'll go back to my corner :)  The
>> session.auto is not obvious, and there are too many options.  Most
>> users just want to save data for a given user in a cache and get it
>> on subsequent requests.  The existing module supports this, but it
>> requires too much setup and doesn't have the "batteries included"
>> feel.  I want a session module that is seamless and automatic,
>> similar to sessions in competing platforms.
>>
>> This new session2 module is easy and automatic.  In short, it acts
>> like a dictionary.  It can store arbitrary (hashable) items just like
>> a regular dictionary can. The contents of the "dictionary" change
>> automatically depending upon the user (browser cookie) is viewing the
>> page.  The programmer doesn't have to worry about cookies, disk
>> files, or anything else.  He/she just puts stuff in the dictionary
>> and gets it on subsequent calls.
>>
>> Following is example code using the new module:
>>
>> [[.import name=session2]]
>> [[\
>>    session2['username'] = 'Bart Simpson'
>>    print session2.get('username')        # prints Bart Simpson
>>    print session2['username']            # prints Bart Simpson
>>
>>    del session2['username']              # deletes the key/value from
>> the session
>>    #print session2['username']           # throws a KeyError (so
>> commented out)
>>    print session2.get('username')        # prints None
>>    print session2.get('username', 'Me')  # prints Me since it was the
>> default
>>
>>    session2['visited'] = session2.get('visited', 0) + 1
>>    print 'You visited us %s times.' % session2['visited']
>> ]]
>>
>> I've tried to program it as efficiently as possible.  Sessions get
>> created just-in-time, so if you never put anything in a person's
>> session (meaning you just retrieve from it), you get the default
>> values without any memory or disk access happening.  It spreads
>> sessions across disk files in a clever way, providing efficient
>> access.   It is also thread-safe.
>>
>> It defaults to saving session data in dbm-files since I estimate most
>> installations of spyce are mod_python, cgi, or default fastcgi (i.e.
>> multiple instances of spyce).  A memory-cache is also available for
>> those using web-server installations.
>>
>> It only supports cookies right now.  I haven't supported passing the
>> sessionid via the request because it requires URL rewriting for every
>> URL on the site (which is a lot of work).  I believe cookies are
>> widely accepted on the net now -- you can't access many sites with
>> cookies disabled anymore.  The point of this module is to be seamless
>> and automatic, and URL rewriting is not seamless or automatic. :)  If
>> people want this, I can add it, but I think it kind of defeats the
>> point of the module.
>>
>> The only real problem is it is not process-safe.  If two instances of
>> spyce hit the same dbm file on disk at the same time, they might
>> blast the same file at the same time.  The existing session module
>> doesn't prevent this either.  I'm not sure it it is possible to
>> prevent this in an efficient way.
>>
>> So... I present this module for your review.  Drop it in your modules
>> directory and try it.  Comments are welcome.
>>
>> Conan
>> ____________________________________
>> Conan C. Albrecht, Ph.D.
>> Information Systems Department
>> Brigham Young University
>>
>> Web: http://warp.byu.edu/
>> Email: conan-RXmsyT/Szrj2fBVCVOL8/[email protected]
>> Skype: doconix
>> (please IM in active working groups; send email otherwise)
>>
>>
>>
>>
>>
>>
>>
>>
smime.p7s (application/pkcs7-signature, 2.3 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.