本文整理汇总了Python中sqlalchemy.orm.collections.collection.appender方法的典型用法代码示例。如果您正苦于以下问题:Python collection.appender方法的具体用法?Python collection.appender怎么用?Python collection.appender使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.orm.collections.collection
的用法示例。
在下文中一共展示了collection.appender方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _assert_required_roles
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [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__)
示例2: test_dict
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [as 别名]
def test_dict(self):
assert_raises_message(
sa_exc.ArgumentError,
"Type InstrumentedDict must elect an appender "
"method to be a collection class",
self._test_adapter,
dict,
self.dictable_entity,
to_set=lambda c: set(c.values()),
)
assert_raises_message(
sa_exc.ArgumentError,
"Type InstrumentedDict must elect an appender method "
"to be a collection class",
self._test_dict,
dict,
)
示例3: test_dict_subclass
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [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))
示例4: converter
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [as 别名]
def converter(fn):
"""Tag the method as the collection converter.
This optional method will be called when a collection is being
replaced entirely, as in::
myobj.acollection = [newvalue1, newvalue2]
The converter method will receive the object being assigned and should
return an iterable of values suitable for use by the ``appender``
method. A converter must not assign values or mutate the collection,
its sole job is to adapt the value the user provides into an iterable
of values for the ORM's use.
The default converter implementation will use duck-typing to do the
conversion. A dict-like collection will be convert into an iterable
of dictionary values, and other types will simply be iterated::
@collection.converter
def convert(self, other): ...
If the duck-typing of the object does not match the type of this
collection, a TypeError is raised.
Supply an implementation of this method if you want to expand the
range of possible types that can be assigned in bulk or perform
validation on the values about to be assigned.
"""
fn._sa_instrument_role = 'converter'
return fn
示例5: append_multiple_without_event
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [as 别名]
def append_multiple_without_event(self, items):
"""Add or restore an entity to the collection, firing no events."""
appender = self._data()._sa_appender
for item in items:
appender(item, _sa_initiator=False)
示例6: _locate_roles_and_methods
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [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
示例7: bulk_replace
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [as 别名]
def bulk_replace(values, existing_adapter, new_adapter, initiator=None):
"""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, _sa_initiator=initiator)
elif member in constants:
appender(member, _sa_initiator=False)
if existing_adapter:
for member in removals:
existing_adapter.fire_remove_event(member, initiator=initiator)
示例8: bulk_replace
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [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)
示例9: converter
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [as 别名]
def converter(fn):
"""Tag the method as the collection converter.
This optional method will be called when a collection is being
replaced entirely, as in::
myobj.acollection = [newvalue1, newvalue2]
The converter method will receive the object being assigned and should
return an iterable of values suitable for use by the ``appender``
method. A converter must not assign values or mutate the collection,
its sole job is to adapt the value the user provides into an iterable
of values for the ORM's use.
The default converter implementation will use duck-typing to do the
conversion. A dict-like collection will be convert into an iterable
of dictionary values, and other types will simply be iterated::
@collection.converter
def convert(self, other): ...
If the duck-typing of the object does not match the type of this
collection, a TypeError is raised.
Supply an implementation of this method if you want to expand the
range of possible types that can be assigned in bulk or perform
validation on the values about to be assigned.
"""
fn._sa_instrument_role = "converter"
return fn
示例10: append_multiple_without_event
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [as 别名]
def append_multiple_without_event(self, items):
"""Add or restore an entity to the collection, firing no events."""
if self.empty:
self._refuse_empty()
appender = self._data()._sa_appender
for item in items:
appender(item, _sa_initiator=False)
示例11: _locate_roles_and_methods
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [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 callable(method):
continue
# note role declarations
if hasattr(method, "_sa_instrument_role"):
role = method._sa_instrument_role
assert role in (
"appender",
"remover",
"iterator",
"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
示例12: test_object_duck
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [as 别名]
def test_object_duck(self):
class MyCollection(object):
def __init__(self):
self.data = set()
@collection.appender
def push(self, item):
self.data.add(item)
@collection.remover
def zark(self, item):
self.data.remove(item)
@collection.removes_return()
def maybe_zark(self, item):
if item in self.data:
self.data.remove(item)
return item
@collection.iterator
def __iter__(self):
return iter(self.data)
__hash__ = object.__hash__
def __eq__(self, other):
return self.data == other
self._test_adapter(MyCollection)
self._test_object(MyCollection)
self.assert_(
getattr(MyCollection, "_sa_instrumented") == id(MyCollection)
)
示例13: converter
# 需要导入模块: from sqlalchemy.orm.collections import collection [as 别名]
# 或者: from sqlalchemy.orm.collections.collection import appender [as 别名]
def converter(fn):
"""Tag the method as the collection converter.
This optional method will be called when a collection is being
replaced entirely, as in::
myobj.acollection = [newvalue1, newvalue2]
The converter method will receive the object being assigned and should
return an iterable of values suitable for use by the ``appender``
method. A converter must not assign values or mutate the collection,
it's sole job is to adapt the value the user provides into an iterable
of values for the ORM's use.
The default converter implementation will use duck-typing to do the
conversion. A dict-like collection will be convert into an iterable
of dictionary values, and other types will simply be iterated::
@collection.converter
def convert(self, other): ...
If the duck-typing of the object does not match the type of this
collection, a TypeError is raised.
Supply an implementation of this method if you want to expand the
range of possible types that can be assigned in bulk or perform
validation on the values about to be assigned.
"""
fn._sa_instrument_role = 'converter'
return fn