Michael Bayer wrote:
ive liked __session__ all along. Dan's concern is that people will think __session__ returns the session which the instance is attached to, not just the "contextual" session which may be different.

Yeah, I'm not thrilled about __session__ for that exact reason. However, I 
agree that it is the nicest name. People are going to use dir(obj) and think 
they can get an object's session by calling obj.__session__(). It may even seem 
to work in most cases, but then there will be a time that it doesn't work and 
they'll be confused. How about __defaultsession__ or even __default_session__? 
That makes it look like it's related to 
sqlalchemy.orm.session._default_session, which it is. Then change 
current_session() to default_session() and the correlation will be complete 
(and intuitive). Note that changing them all to current_session would not be as 
intuitive because __current_session__ would not return an object's current 
session.

One thing to keep in mind is that people will not be calling this method very 
often if ever--it's really just a hook for advanced use cases.


In my original proposal, i was going to just have __session__ just return wahtever contextual session the developer wants, whether it be associated with a class, with an instance, whatever; I cannot predict what people might want to use it for. I guess your specific use case does require that different instances reference a different "contextual" session.

So what do you say Dan ?  can we just leave it open ended ?

To clarify, you're talking about doing this:


def current_session(obj=None):
   if hasattr(obj, '__sessioncontext__'):
       return obj.__sessioncontext__()
   else:
       return _default_session(obj=obj)


rather than this:


def current_session(obj=None):
   if hasattr(obj.__class__, '__sessioncontext__'):
       return obj.__class__.__sessioncontext__()
   else:
       return _default_session(obj=obj)


Right? Yes, that's fine. That (the first version) will work with either 
classmethod or instance method. I don't see much use in an instance method ATM, 
but you're right why not allow it? It will allow (tempt?) people to do things 
they shouldn't do, but hey this is Python and we're consenting adults...

It will be important to distinguish __session__ and _sa_session_id when this is 
documented (if it ever is documented).

Oh, and use a try/except block rather than hasattr. It's more pythonic... So 
with all those suggestions this is what we end up with:


def default_session(obj=None):
   try:
       return obj.__default_session__()
   except AttributeError:
       return _default_session(obj=obj)


~ Daniel


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to