I am using a decorator that handles all this dirty work:

@session_manager('get_session')
def do_session_stuff(self):
    pass

Gunnlaugur Briem wrote:
> Hi,
>
> trying to be careful to close all sessions when I'm done with them, I
> find myself doing this all the time:
>
> session = Session()
> try:
>     do_stuff_with(session)
> finally:
>     session.close()
>
> This would be neater:
>
> with Session() as session:
>     do_stuff_with(session)
>
> but the sessionmaker-produced class does not implement the context
> manager protocol (the __enter__ and __exit__ methods) used by the with
> statement. Now, I can add on the context manager protocol using
> contextlib:
>
> from contextlib import closing
> with closing(Session()) as session:
>     do_stuff_with(session)
>
> but is there any reason for the session itself not to support the
> context manager protocol and save me the extra closing()?
>
> Regards,
>
>     - Gulli
>
> >
>
> ____________________________________________________________________________________
> Earn more money.  Click here to be trained in human resources and launch your 
> career.
> http://ads.lavabit.com/fc/BLSrjwrsFB3b2AgV4nQOOBOi9zEHy45QcpNGLOxaH5Pv5FyYj3gBD3XRd2w/
> ____________________________________________________________________________________
>   


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to