本文整理汇总了Python中sqlalchemy.orm.collections.collection.remover方法的典型用法代码示例。如果您正苦于以下问题:Python collection.remover方法的具体用法?Python collection.remover怎么用?Python collection.remover使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm.collections.collection
的用法示例。
在下文中一共展示了collection.remover方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: remover
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import remover [as 别名]
def remover(fn):
"""Tag the method as the collection remover.
The remover method is called with one positional argument: the value
to remove. The method will be automatically decorated with
:meth:`removes_return` if not already decorated::
@collection.remover
def zap(self, entity): ...
# or, equivalently
@collection.remover
@collection.removes_return()
def zap(self, ): ...
If the value to remove is not present in the collection, you may
raise an exception or return None to ignore the error.
If the remove method is internally instrumented, you must also
receive the keyword argument '_sa_initiator' and ensure its
promulgation to collection events.
"""
fn._sa_instrument_role = 'remover'
return fn
示例2: _assert_required_roles
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import remover [as 别名]
def _assert_required_roles(cls, roles, methods):
"""ensure all roles are present, and apply implicit instrumentation if
needed
"""
if 'appender' not in roles or not hasattr(cls, roles['appender']):
raise sa_exc.ArgumentError(
"Type %s must elect an appender method to be "
"a collection class" % cls.__name__)
elif (roles['appender'] not in methods and
not hasattr(getattr(cls, roles['appender']), '_sa_instrumented')):
methods[roles['appender']] = ('fire_append_event', 1, None)
if 'remover' not in roles or not hasattr(cls, roles['remover']):
raise sa_exc.ArgumentError(
"Type %s must elect a remover method to be "
"a collection class" % cls.__name__)
elif (roles['remover'] not in methods and
not hasattr(getattr(cls, roles['remover']), '_sa_instrumented')):
methods[roles['remover']] = ('fire_remove_event', 1, None)
if 'iterator' not in roles or not hasattr(cls, roles['iterator']):
raise sa_exc.ArgumentError(
"Type %s must elect an iterator method to be "
"a collection class" % cls.__name__)
示例3: remover
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import remover [as 别名]
def remover(fn):
"""Tag the method as the collection remover.
The remover method is called with one positional argument: the value
to remove. The method will be automatically decorated with
:meth:`removes_return` if not already decorated::
@collection.remover
def zap(self, entity): ...
# or, equivalently
@collection.remover
@collection.removes_return()
def zap(self, ): ...
If the value to remove is not present in the collection, you may
raise an exception or return None to ignore the error.
If the remove method is internally instrumented, you must also
receive the keyword argument '_sa_initiator' and ensure its
promulgation to collection events.
"""
fn._sa_instrument_role = "remover"
return fn
示例4: test_dict_subclass
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import remover [as 别名]
def test_dict_subclass(self):
class MyDict(dict):
@collection.appender
@collection.internally_instrumented
def set(self, item, _sa_initiator=None):
self.__setitem__(item.a, item, _sa_initiator=_sa_initiator)
@collection.remover
@collection.internally_instrumented
def _remove(self, item, _sa_initiator=None):
self.__delitem__(item.a, _sa_initiator=_sa_initiator)
self._test_adapter(
MyDict, self.dictable_entity, to_set=lambda c: set(c.values())
)
self._test_dict(MyDict)
self._test_dict_bulk(MyDict)
self.assert_(getattr(MyDict, "_sa_instrumented") == id(MyDict))
示例5: iterator
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import remover [as 别名]
def iterator(fn):
"""Tag the method as the collection remover.
The iterator method is called with no arguments. It is expected to
return an iterator over all collection members::
@collection.iterator
def __iter__(self): ...
"""
fn._sa_instrument_role = 'iterator'
return fn
示例6: clear_with_event
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import remover [as 别名]
def clear_with_event(self, initiator=None):
"""Empty the collection, firing a mutation event for each entity."""
remover = self._data()._sa_remover
for item in list(self):
remover(item, _sa_initiator=initiator)
示例7: clear_without_event
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import remover [as 别名]
def clear_without_event(self):
"""Empty the collection, firing no events."""
remover = self._data()._sa_remover
for item in list(self):
remover(item, _sa_initiator=False)
示例8: _locate_roles_and_methods
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import remover [as 别名]
def _locate_roles_and_methods(cls):
"""search for _sa_instrument_role-decorated methods in
method resolution order, assign to roles.
"""
roles = {}
methods = {}
for supercls in cls.__mro__:
for name, method in vars(supercls).items():
if not util.callable(method):
continue
# note role declarations
if hasattr(method, '_sa_instrument_role'):
role = method._sa_instrument_role
assert role in ('appender', 'remover', 'iterator',
'linker', 'converter')
roles.setdefault(role, name)
# transfer instrumentation requests from decorated function
# to the combined queue
before, after = None, None
if hasattr(method, '_sa_instrument_before'):
op, argument = method._sa_instrument_before
assert op in ('fire_append_event', 'fire_remove_event')
before = op, argument
if hasattr(method, '_sa_instrument_after'):
op = method._sa_instrument_after
assert op in ('fire_append_event', 'fire_remove_event')
after = op
if before:
methods[name] = before + (after, )
elif after:
methods[name] = None, None, after
return roles, methods
示例9: bulk_replace
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import remover [as 别名]
def bulk_replace(values, existing_adapter, new_adapter):
"""Load a new collection, firing events based on prior like membership.
Appends instances in ``values`` onto the ``new_adapter``. Events will be
fired for any instance not present in the ``existing_adapter``. Any
instances in ``existing_adapter`` not present in ``values`` will have
remove events fired upon them.
:param values: An iterable of collection member instances
:param existing_adapter: A :class:`.CollectionAdapter` of
instances to be replaced
:param new_adapter: An empty :class:`.CollectionAdapter`
to load with ``values``
"""
assert isinstance(values, list)
idset = util.IdentitySet
existing_idset = idset(existing_adapter or ())
constants = existing_idset.intersection(values or ())
additions = idset(values or ()).difference(constants)
removals = existing_idset.difference(constants)
appender = new_adapter.bulk_appender()
for member in values or ():
if member in additions:
appender(member)
elif member in constants:
appender(member, _sa_initiator=False)
if existing_adapter:
remover = existing_adapter.bulk_remover()
for member in removals:
remover(member)