本文整理汇总了Python中sqlalchemy.orm.scoped_session函数的典型用法代码示例。如果您正苦于以下问题:Python scoped_session函数的具体用法?Python scoped_session怎么用?Python scoped_session使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了scoped_session函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_session
def get_session():
"""Creates a new database session for instant use"""
engine = create_engine(settings.DATASTORE_URI)
Session = sessionmaker(bind = engine)
scoped_session(Session)
return (Session(), engine)
示例2: autoupdate
def autoupdate():
"""
Autoupdate data layer about new models in engines.
"""
scoped_session(Backend.instance().get_sessionmaker)
Backend.instance().get_base().metadata.create_all(
Backend.instance().get_engine())
示例3: __init__
def __init__(self, uri=None, engine=None, **kwargs):
"""Create a new SqlAlchemyStore. When given a connection string (uri)
or SQLAlchemy Engine (engine), this Store will create it's own internal
SQLAlchemy Session to manage objects. If you do not provide a URI or
Engine, your mapped object metadata must be bound to their own engines.
"""
super(SqlAlchemyStore, self).__init__()
# get the session
if "session" in kwargs:
# we no longer allow initialization with a pre-existing session object, there
# are too many issues with this approach at the moment
raise DeprecationWarning("cannot instantiate a SqlAlchemyStore with a pre-existing session object")
else:
# no session, we have to make one
# first we need to get the engine
if uri and engine:
# can't give two ways to get an engine
raise ValueError("you can only provide either a connection string URI or an engine, not both")
elif uri:
# we have a uri to create an engine
engine = create_engine(uri)
if engine:
# we have an engine, we can create the bound session now
self.__session = scoped_session(sessionmaker(autoflush=True, bind=engine))
else:
# no engine or URI was specified, create an unbound session
# (mapped object metadata will need to be bound to an engine in this case)
self.__session = scoped_session(sessionmaker(autoflush=True))
示例4: create_security_group_rule_bulk_native
def create_security_group_rule_bulk_native(self, context,
security_group_rule):
r = security_group_rule['security_group_rules']
scoped_session(context.session)
security_group_id = self._validate_security_group_rules(
context, security_group_rule)
with context.session.begin(subtransactions=True):
if not self.get_security_group(context, security_group_id):
raise ext_sg.SecurityGroupNotFound(id=security_group_id)
self._check_for_duplicate_rules(context, r)
ret = []
for rule_dict in r:
rule = rule_dict['security_group_rule']
tenant_id = self._get_tenant_id_for_create(context, rule)
db = SecurityGroupRule(
id=uuidutils.generate_uuid(), tenant_id=tenant_id,
security_group_id=rule['security_group_id'],
direction=rule['direction'],
remote_group_id=rule.get('remote_group_id'),
ethertype=rule['ethertype'],
protocol=rule['protocol'],
port_range_min=rule['port_range_min'],
port_range_max=rule['port_range_max'],
remote_ip_prefix=rule.get('remote_ip_prefix'))
context.session.add(db)
ret.append(self._make_security_group_rule_dict(db))
return ret
示例5: __init__
def __init__(self, bus, connection_string):
"""
The plugin is registered to the CherryPy engine and therefore
is part of the bus (the engine *is* a bus) registery.
We use this plugin to create the SA engine. At the same time,
when the plugin starts we create the tables into the database
using the mapped class of the global metadata.
"""
plugins.SimplePlugin.__init__(self, bus)
# self.sa_engine = None
# self.connection_string = connection_string
# self.session = scoped_session(sessionmaker(autoflush=True, autocommit=False))
self.sa_engine_online = None
self.connection_string_online = connection_string.connectUrlonline
self.session_online = scoped_session(sessionmaker(autoflush=True,
autocommit=False))
self.sa_engine_offline = None
self.connection_string_offline = connection_string.connectUrloffline
self.session_offline = scoped_session(sessionmaker(autoflush=True,
autocommit=False))
self.sa_engine_cache = None
self.connection_string_cache = connection_string.connectUrlcache
self.session_cache = scoped_session(sessionmaker(autoflush=True,
autocommit=False))
示例6: test_default_constructor_state_not_shared
def test_default_constructor_state_not_shared(self):
scope = scoped_session(sa.orm.sessionmaker())
class A(object):
pass
class B(object):
def __init__(self):
pass
scope.mapper(A, table1)
scope.mapper(B, table2)
A(foo='bar')
assert_raises(TypeError, B, foo='bar')
scope = scoped_session(sa.orm.sessionmaker())
class C(object):
def __init__(self):
pass
class D(object):
pass
scope.mapper(C, table1)
scope.mapper(D, table2)
assert_raises(TypeError, C, foo='bar')
D(foo='bar')
示例7: test_all_htmlpopups
def test_all_htmlpopups(self):
from chsdi.models import models_from_name
from chsdi.models.bod import LayersConfig
from sqlalchemy import distinct
from sqlalchemy.orm import scoped_session, sessionmaker
val = True
DBSession = scoped_session(sessionmaker())
query = DBSession.query(distinct(LayersConfig.idBod)).filter(LayersConfig.queryable == val).filter(LayersConfig.staging == 'prod')
# Get a list of all the queryable layers
layers = [q[0] for q in query]
DBSession.close()
# Get a list of feature ids
features = []
for layer in layers:
try:
model = models_from_name(layer)[0]
DBSession = scoped_session(sessionmaker())
query = DBSession.query(model.primary_key_column()).limit(1)
ID = [q[0] for q in query]
# If tables are empty ID is an empty list
if ID:
features.append((layer, str(ID[0])))
DBSession.close()
except Exception as e:
print e
finally:
DBSession.close()
for f in features:
for lang in ('de', 'fr', 'it', 'rm', 'en'):
self.testapp.get('/rest/services/all/MapServer/%s/%s/htmlPopup' % (f[0], f[1]), params={'callback': 'cb', 'lang': '%s' % lang}, status=200)
示例8: __init__
def __init__(self, name=None):
if not name:
name = 'default'
data = {}
if hasattr(settings, 'DATABASES') and \
name in settings.DATABASES:
data = settings.DATABASES[name]
if data.get('ENGINE', '') == '':
raise ImproperlyConfigured('''\
The database ORM connection requires, at minimum, an engine type.''')
if '.' in data['ENGINE']:
data['ENGINE'] = data['ENGINE'].rsplit('.', 1)[-1]
# django needs sqlite3 but sqlalchemy references sqlite
if data['ENGINE'] == 'sqlite3':
data['ENGINE'] = 'sqlite'
self.engine = self._create_engine(data)
ro_values = dict([(k[9:], v) for k, v in data.iteritems()
if k.startswith('READONLY_')])
if len(ro_values):
ro_data = dict(data)
ro_data.update(ro_values)
self.readonly_engine = self._create_engine(ro_data)
self.metadata = MetaData(self.engine)
self.Base = declarative_base(metadata=self.metadata)
if hasattr(self, 'readonly_engine'):
self._readonly_sessionmaker = \
scoped_session(sessionmaker(bind=self.readonly_engine))
self._sessionmaker = scoped_session(sessionmaker(bind=self.engine))
示例9: init_sqlalchemy
def init_sqlalchemy(settings):
# master
master_url = settings['sqlalchemy.url']
connect_kwargs = settings.get('sqlalchemy.connect_kwargs')
kwargs = {}
if connect_kwargs is not None:
if isinstance(connect_kwargs, str):
connect_kwargs = json.loads(connect_kwargs)
for k, v in connect_kwargs.items():
kwargs[k] = v
engine = create_engine(master_url, **kwargs)
sm = orm.sessionmaker(bind=engine, extension=ZopeTransactionExtension())
meta.Session = orm.scoped_session(sm)
meta.metadata.bind = engine
# slaves
slaves_url = settings.get('sqlalchemy.slaves', [])
slaves = []
for url in slaves_url:
slave = create_engine(url, **kwargs)
sm = orm.sessionmaker(bind=slave, extension=ZopeTransactionExtension())
slaves.append(orm.scoped_session(sm))
if slaves:
slave = random.choice(slaves)
meta.BaseObject.query = slave.query_property(orm.Query)
else:
meta.BaseObject.query = meta.Session.query_property(orm.Query)
示例10: test_all_htmlpopups
def test_all_htmlpopups(self):
from chsdi.models import models_from_name
from chsdi.models.bod import LayersConfig
from sqlalchemy import distinct
from sqlalchemy.orm import scoped_session, sessionmaker
val = True
DBSession = scoped_session(sessionmaker())
valnone = None
query = DBSession.query(distinct(LayersConfig.layerBodId)).filter(LayersConfig.staging == 'prod').filter(LayersConfig.queryable == val).filter(LayersConfig.parentLayerId == valnone)
features = []
try:
for layer in getLayers(query):
try:
FeatDBSession = scoped_session(sessionmaker())
models = models_from_name(layer)
self.failUnless(models is not None and len(models) > 0, layer)
model = models[0]
query = FeatDBSession.query(model.primary_key_column()).limit(1)
ID = [q[0] for q in query]
if ID:
features.append((layer, str(ID[0])))
finally:
FeatDBSession.close()
finally:
DBSession.close()
for f in features:
for lang in ('de', 'fr', 'it', 'rm', 'en'):
link = '/rest/services/all/MapServer/' + f[0] + '/' + f[1] + '/htmlPopup?callback=cb&lang=' + lang
resp = self.testapp.get(link)
self.failUnless(resp.status_int == 200, link)
示例11: connect
def connect(dbstr):
engine = create_engine(dbstr, echo = False)
connection = engine.connect()
#handle case that the db was initialized before a version table existed yet
if engine.dialect.has_table(connection, "program"):
# if there are existing tables
if not engine.dialect.has_table(connection, "_meta"):
# if no version table, assume outdated db version and error
connection.close()
raise Exception("Your opentuner database is currently out of date. Save a back up and reinitialize")
# else if we have the table already, make sure version matches
if engine.dialect.has_table(connection, "_meta"):
Session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
version = _Meta.get_version(Session)
if not DB_VERSION == version:
raise Exception('Your opentuner database version {} is out of date with the current version {}'.format(version, DB_VERSION))
Base.metadata.create_all(engine)
Session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
# mark database with current version
_Meta.add_version(Session, DB_VERSION)
Session.commit()
return engine, Session
示例12: init_rdbms
def init_rdbms(engine):
"""Call me before using any RDBMSStore class in the model."""
sm = orm.sessionmaker(autocommit=True, bind=engine)
rdbms.sqlEngine = engine
rdbms.sqlSession = orm.scoped_session(sm)
sqlEngine = engine
sqlSession = orm.scoped_session(sm)
assert(sqlEngine)
assert(sqlSession)
示例13: create_group
def create_group():
'''
Endpoint that allows the user to generate a group. GET request generates
the form required to create the group. POST request makes the DB calls to
create the group.
'''
global USE_PROTOBUFF
if request.method == 'POST':
if USE_PROTOBUFF:
GrpCreateClient = message_pb2.UsrGrp()
result = base64.b64decode(request.form['protoString'])
GrpCreateClient.ParseFromString(result)
name = GrpCreateClient.name
user_ids = GrpCreateClient.user_ids
else:
name = request.form['name']
user_ids = request.form['user_ids']
#parse string of users into integer array
user_ids = user_ids.split(',')
user_ids = [int(user_id) for user_id in user_ids]
user_ids.append(int(current_user.id))
user_ids.sort()
#make sure that the group has more than 2 people
if len(user_ids) <= 2:
flash('Groups must be made with more than 2 people')
return json.dumps({}, 403, {'ContentType':'application/json'})
user_ids = str(user_ids)
#make sure the group has not been created yet
Session = scoped_session(sessionmaker(bind=engine))
s = Session()
result_proxy = s.execute('SELECT * FROM groups WHERE user_ids = "' + user_ids + '" LIMIT 1')
s.close()
result = result_proxy.fetchone()
if result is not None:
flash('That group has already been created')
return json.dumps({}, 403, {'ContentType':'application/json'})
#create group
try:
Session = scoped_session(sessionmaker(bind=engine))
s = Session()
s.execute('INSERT INTO groups(groupname, user_ids) VALUES ("' + name + '","' + user_ids + '")')
s.commit()
s.close()
return json.dumps({}, 200, {'ContentType':'application/json'})
except:
return json.dumps({}, 403, {'ContentType':'application/json'})
else:
return render_template(
'create_group.html'
)
示例14: _rasp_session
def _rasp_session(engine = None):
if engine is None:
return scoped_session(sessionmaker(autoflush=False,
autocommit=False))
else:
return scoped_session(sessionmaker(autoflush=False,
autocommit=False,
bind=engine))
示例15: __new__
def __new__(cls, *args, **kwarg):
if not cls._session:
engine = create_engine(settings.DATABASE_ENGINE, pool_size=settings.DATABASE_POOL_SIZE, pool_recycle=300, proxy=TimerProxy())
if hasattr(settings, 'CACHED_QUERY') and settings.CACHED_QUERY:
from torneira.cache import CachedQuery
cls._session = scoped_session(sessionmaker(autocommit=True, autoflush=False, expire_on_commit=False, query_cls=CachedQuery, bind=engine))
else:
cls._session = scoped_session(sessionmaker(autocommit=True, autoflush=False, expire_on_commit=False, bind=engine))
return cls._session