> my current proposal regarding the association of objects to sessions, > as well as the optional association of objects and classes to > "session contexts", is as follows: (...)
That's very nice! I'm looking forward to refactor our application to use this interface. > # returns the session assocaited with the current "session context". > # the "session context" may be specific to a particular class or > object > def current_session(instance=None, klass=None): > # objects/classes may implement the __sessioncontext__ > method which returns a > # sessioncontext callable > if instance is not None and hasattr(instance, > '__sessioncontext__'): > context = instance.__sessioncontext__() > return context() > elif klass is not None and hasattr(klass, > '__sessioncontext__'): > context = klass.__sessioncontext__() > return context() I suggest a change here. The __sessioncontext__ method for a class becomes the __sessioncontext__ method for the instance once it's created. I suggest turning it into something like: def current_session(object=None): if object: context = getattr(object, "__instancecontext__", None) if (context and getattr(context, "im_self", True) is not None): session = context() if session: return session context = getattr(object, "__classcontext__", None) if context: session = context() if session: return session context = default_session_context(object) if context: session = context() if session: return None return None This implementation provides the following features: - Allows separate logic for instances and classes. - Allows a given type to implement only the class context, and have it working for instances as well (classmethods). - Allows a given type to implement *both* of them, and call each one when appropriate. - Allows a given type to implement both of them, but decide to optionally fallback to the more general context at runtime (returning None). > # allows a default "session context" function to be plugged in > # at the module level. the threadlocal mod plugs in one particular > implementation > # of this function. > def _default_session_context(instance=None, klass=None): > return None It'd be nice to implement a supported way to overload both of these functions. Doing thirdparty.some_func = my_func always seems like an unsupported hack that will break in the next upgrade. > def object_session(instance): > hashkey = getattr(instance, '_sa_session_id', None) > if hashkey is not None: > return _sessions.get(hashkey) > else: > return None That could potentially be merged at the top of the method above. It also sounds like the method names are reversed, but that's minor. -- Gustavo Niemeyer http://niemeyer.net ------------------------------------------------------- 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