Skip to content

Instantly share code, notes, and snippets.

@blaflamme
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save blaflamme/0b25c8fed8db0b87b9b6 to your computer and use it in GitHub Desktop.

Select an option

Save blaflamme/0b25c8fed8db0b87b9b6 to your computer and use it in GitHub Desktop.
pyramid_sqla.py
# -*- coding: utf-8 -*-
from sqlalchemy import engine_from_config
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from zope.sqlalchemy import register
Base = declarative_base()
def get_session_maker(settings, prefix='sqlalchemy.'):
engine = engine_from_config(settings, prefix)
maker = sessionmaker()
maker.configure(bind=engine)
return maker
def get_session(request, maker):
session = maker()
register(session, transaction_manager=request.tm)
return session
def includeme(config):
config.include('pyramid_tm')
settings = config.get_settings()
maker = get_session_maker(settings)
config.add_request_method('db', lambda: maker(), reify=True)
@pauleveritt

Copy link
Copy Markdown

Can't the lambda be replaced with a class that has the get_session_maker work in its init and a call used by add_request_method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment