本文整理匯總了Python中sqlalchemy.event.contains方法的典型用法代碼示例。如果您正苦於以下問題:Python event.contains方法的具體用法?Python event.contains怎麽用?Python event.contains使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sqlalchemy.event
的用法示例。
在下文中一共展示了event.contains方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __create_event
# 需要導入模塊: from sqlalchemy import event [as 別名]
# 或者: from sqlalchemy.event import contains [as 別名]
def __create_event(self):
""" Create an SQLAlchemy event listening the 'set' in a particular column.
:rtype : object
"""
if not event.contains(self.field, 'set', self.__validate):
event.listen(self.field, 'set', self.__validate, retval=True)
示例2: stop
# 需要導入模塊: from sqlalchemy import event [as 別名]
# 或者: from sqlalchemy.event import contains [as 別名]
def stop(self):
""" Remove the listener to stop the validation
"""
if event.contains(self.field, 'set', self.__validate):
event.remove(self.field, 'set', self.__validate)
示例3: start
# 需要導入模塊: from sqlalchemy import event [as 別名]
# 或者: from sqlalchemy.event import contains [as 別名]
def start(self):
""" Restart the listener
"""
if not event.contains(self.field, 'set', self.__validate):
self.__create_event()
示例4: register_events
# 需要導入模塊: from sqlalchemy import event [as 別名]
# 或者: from sqlalchemy.event import contains [as 別名]
def register_events(self, remove=False):
for e, h in (
('before_insert', self.before_insert),
('before_update', self.before_update),
('before_delete', self.before_delete),
):
is_event_exist = event.contains(self.base_class, e, h)
if remove and is_event_exist:
event.remove(self.base_class, e, h)
elif not is_event_exist:
event.listen(self.base_class, e, h, propagate=True)
return self
示例5: _setup_composite_listener
# 需要導入模塊: from sqlalchemy import event [as 別名]
# 或者: from sqlalchemy.event import contains [as 別名]
def _setup_composite_listener():
def _listen_for_type(mapper, class_):
for prop in mapper.iterate_properties:
if (hasattr(prop, 'composite_class') and
isinstance(prop.composite_class, type) and
issubclass(prop.composite_class, MutableComposite)):
prop.composite_class._listen_on_attribute(
getattr(class_, prop.key), False, class_)
if not event.contains(Mapper, "mapper_configured", _listen_for_type):
event.listen(Mapper, 'mapper_configured', _listen_for_type)
示例6: _setup_composite_listener
# 需要導入模塊: from sqlalchemy import event [as 別名]
# 或者: from sqlalchemy.event import contains [as 別名]
def _setup_composite_listener():
def _listen_for_type(mapper, class_):
for prop in mapper.iterate_properties:
if (
hasattr(prop, "composite_class")
and isinstance(prop.composite_class, type)
and issubclass(prop.composite_class, MutableComposite)
):
prop.composite_class._listen_on_attribute(
getattr(class_, prop.key), False, class_
)
if not event.contains(Mapper, "mapper_configured", _listen_for_type):
event.listen(Mapper, "mapper_configured", _listen_for_type)
示例7: create_engine_from_conf
# 需要導入模塊: from sqlalchemy import event [as 別名]
# 或者: from sqlalchemy.event import contains [as 別名]
def create_engine_from_conf(config):
need_connection_pool_fix = True
if not config.has_section('database'):
seafile_data_dir = os.environ['SEAFILE_CONF_DIR']
if seafile_data_dir:
path = os.path.join(seafile_data_dir, 'seafile.db')
else:
logging.warning('SEAFILE_CONF_DIR not set, can not load sqlite database.')
return None
db_url = "sqlite:///%s" % path
need_connection_pool_fix = False
else:
backend = config.get('database', 'type')
if backend == 'mysql':
if config.has_option('database', 'host'):
host = config.get('database', 'host').lower()
else:
host = 'localhost'
if config.has_option('database', 'port'):
port = config.getint('database', 'port')
else:
port = 3306
username = config.get('database', 'user')
passwd = config.get('database', 'password')
dbname = config.get('database', 'db_name')
db_url = "mysql+pymysql://%s:%s@%s:%s/%s?charset=utf8" % (username, quote_plus(passwd), host, port, dbname)
elif backend == 'oracle':
if config.has_option('database', 'host'):
host = config.get('database', 'host').lower()
else:
host = 'localhost'
if config.has_option('database', 'port'):
port = config.getint('database', 'port')
else:
port = 1521
username = config.get('database', 'username')
passwd = config.get('database', 'password')
service_name = config.get('database', 'service_name')
db_url = "oracle://%s:%s@%s:%s/%s" % (username, quote_plus(passwd),
host, port, service_name)
else:
raise RuntimeError("Unknown database backend: %s" % backend)
# Add pool recycle, or mysql connection will be closed by mysqld if idle
# for too long.
kwargs = dict(pool_recycle=300, echo=False, echo_pool=False)
engine = create_engine(db_url, **kwargs)
if need_connection_pool_fix and not has_event_listener(Pool, 'checkout', ping_connection):
# We use has_event_listener to double check in case we call create_engine
# multipe times in the same process.
add_event_listener(Pool, 'checkout', ping_connection)
return engine