本文整理匯總了Python中dynamo3.ItemUpdate.add方法的典型用法代碼示例。如果您正苦於以下問題:Python ItemUpdate.add方法的具體用法?Python ItemUpdate.add怎麽用?Python ItemUpdate.add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類dynamo3.ItemUpdate
的用法示例。
在下文中一共展示了ItemUpdate.add方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_atomic_add_num
# 需要導入模塊: from dynamo3 import ItemUpdate [as 別名]
# 或者: from dynamo3.ItemUpdate import add [as 別名]
def test_atomic_add_num(self):
""" Update can atomically add to a number """
self.make_table()
self.dynamo.put_item('foobar', {'id': 'a'})
self.dynamo.update_item('foobar', {'id': 'a'},
[ItemUpdate.add('foo', 1)])
self.dynamo.update_item('foobar', {'id': 'a'},
[ItemUpdate.add('foo', 2)])
item = list(self.dynamo.scan('foobar'))[0]
self.assertEqual(item, {'id': 'a', 'foo': 3})
示例2: test_atomic_add_set
# 需要導入模塊: from dynamo3 import ItemUpdate [as 別名]
# 或者: from dynamo3.ItemUpdate import add [as 別名]
def test_atomic_add_set(self):
""" Update can atomically add to a set """
self.make_table()
self.dynamo.put_item('foobar', {'id': 'a'})
self.dynamo.update_item('foobar', {'id': 'a'},
[ItemUpdate.add('foo', set([1]))])
self.dynamo.update_item('foobar', {'id': 'a'},
[ItemUpdate.add('foo', set([1, 2]))])
item = list(self.dynamo.scan('foobar'))[0]
self.assertEqual(item, {'id': 'a', 'foo': set([1, 2])})
示例3: test_write_add_require_value
# 需要導入模塊: from dynamo3 import ItemUpdate [as 別名]
# 或者: from dynamo3.ItemUpdate import add [as 別名]
def test_write_add_require_value(self):
""" Doing an ADD requires a non-null value """
with self.assertRaises(ValueError):
ItemUpdate.add('foo', None)
示例4: sync
# 需要導入模塊: from dynamo3 import ItemUpdate [as 別名]
# 或者: from dynamo3.ItemUpdate import add [as 別名]
def sync(self, items, raise_on_conflict=None, consistent=False, constraints=None):
"""
Sync model changes back to database
This will push any updates to the database, and ensure that all of the
synced items have the most up-to-date data.
Parameters
----------
items : list or :class:`~flywheel.models.Model`
Models to sync
raise_on_conflict : bool, optional
If True, raise exception if any of the fields that are being
updated were concurrently changed in the database (default set by
:attr:`.default_conflict`)
consistent : bool, optional
If True, force a consistent read from the db. This will only take
effect if the sync is only performing a read. (default False)
constraints : list, optional
List of more complex constraints that must pass for the update to
complete. Must be used with raise_on_conflict=True. Format is the
same as query filters (e.g. Model.fieldname > 5)
Raises
------
exc : :class:`dynamo3.CheckFailed`
If raise_on_conflict=True and the model changed underneath us
"""
if raise_on_conflict is None:
raise_on_conflict = self.default_conflict in ('update', 'raise')
if constraints is not None and not raise_on_conflict:
raise ValueError("Cannot pass constraints to sync() when raise_on_conflict is False")
if isinstance(items, Model):
items = [items]
refresh_models = []
for item in items:
# Look for any mutable fields (e.g. sets) that have changed
for name in item.keys_():
if name in item.__dirty__ or name in item.__incrs__:
continue
field = item.meta_.fields.get(name)
if field is None:
value = item.get_(name)
if Field.is_overflow_mutable(value):
if value != item.cached_(name):
item.__dirty__.add(name)
elif field.is_mutable:
cached_var = item.cached_(name)
if field.resolve(item) != cached_var:
for related in item.meta_.related_fields[name]:
item.__dirty__.add(related)
if not item.__dirty__ and not item.__incrs__:
refresh_models.append(item)
continue
fields = item.__dirty__
item.pre_save_(self)
# If the model has changed any field that is part of a composite
# field, FORCE the sync to raise on conflict. This prevents the
# composite key from potentially getting into an inconsistent state
_raise_on_conflict = raise_on_conflict
for name in itertools.chain(item.__incrs__, fields):
for related_name in item.meta_.related_fields.get(name, []):
field = item.meta_.fields[related_name]
if field.composite:
_raise_on_conflict = True
break
keywords = {}
constrained_fields = set()
if _raise_on_conflict and constraints is not None:
for constraint in constraints:
constrained_fields.update(constraint.eq_fields.keys())
constrained_fields.update(constraint.fields.keys())
keywords.update(constraint.scan_kwargs())
updates = []
# Set dynamo keys
for name in fields:
field = item.meta_.fields.get(name)
value = getattr(item, name)
kwargs = {}
if _raise_on_conflict and name not in constrained_fields:
kwargs = {'eq': item.ddb_dump_cached_(name)}
update = ItemUpdate.put(name, item.ddb_dump_field_(name),
**kwargs)
updates.append(update)
# Atomic increment fields
for name, value in six.iteritems(item.__incrs__):
kwargs = {}
# We don't need to ddb_dump because we know they're all native
if isinstance(value, SetDelta):
update = ItemUpdate(value.action, name, value.values)
else:
update = ItemUpdate.add(name, value)
updates.append(update)
#.........這裏部分代碼省略.........