本文整理汇总了Python中pynamodb.models.Model方法的典型用法代码示例。如果您正苦于以下问题:Python models.Model方法的具体用法?Python models.Model怎么用?Python models.Model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pynamodb.models
的用法示例。
在下文中一共展示了models.Model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_binary_attribute_update
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_binary_attribute_update(ddb_url):
class DataModel(Model):
class Meta:
table_name = 'binary_attr_update'
host = ddb_url
pkey = UnicodeAttribute(hash_key=True)
data = BinaryAttribute()
DataModel.create_table(read_capacity_units=1, write_capacity_units=1, wait=True)
data = b'\x00hey\xfb'
pkey = 'pkey'
DataModel(pkey, data=data).save()
m = DataModel.get(pkey)
assert m.data == data
new_data = b'\xff'
m.update(actions=[DataModel.data.set(new_data)])
assert new_data == m.data
示例2: test_binary_set_attribute_update
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_binary_set_attribute_update(ddb_url):
class DataModel(Model):
class Meta:
table_name = 'binary_set_attr_update'
host = ddb_url
pkey = UnicodeAttribute(hash_key=True)
data = BinarySetAttribute()
DataModel.create_table(read_capacity_units=1, write_capacity_units=1, wait=True)
data = {b'\x00hey\xfb', b'\x00beautiful\xfb'}
pkey = 'pkey'
DataModel(pkey, data=data).save()
m = DataModel.get(pkey)
assert m.data == data
new_data = {b'\xff'}
m.update(actions=[DataModel.data.set(new_data)])
assert new_data == m.data
示例3: test_attribute_paths_subclassing
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_attribute_paths_subclassing(self):
class SubMapAttribute(MapAttribute):
foo = UnicodeAttribute(attr_name='dyn_foo')
class SubSubMapAttribute(SubMapAttribute):
bar = UnicodeAttribute(attr_name='dyn_bar')
class SubModel(Model):
key = NumberAttribute(hash_key=True)
sub_map = SubMapAttribute(attr_name='dyn_sub_map')
class SubSubModel(SubModel):
sub_sub_map = SubSubMapAttribute()
assert SubModel.sub_map.foo.attr_name == 'dyn_foo'
assert SubModel.sub_map.foo.attr_path == ['dyn_sub_map', 'dyn_foo']
assert SubSubModel.sub_map.foo.attr_name == 'dyn_foo'
assert SubSubModel.sub_map.foo.attr_path == ['dyn_sub_map', 'dyn_foo']
assert SubSubModel.sub_sub_map.foo.attr_name == 'dyn_foo'
assert SubSubModel.sub_sub_map.foo.attr_path == ['sub_sub_map', 'dyn_foo']
assert SubSubModel.sub_sub_map.bar.attr_name == 'dyn_bar'
assert SubSubModel.sub_sub_map.bar.attr_path == ['sub_sub_map', 'dyn_bar']
示例4: test_model_update
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_model_update(assert_mypy_output):
assert_mypy_output("""
from pynamodb.attributes import NumberAttribute
from pynamodb.models import Model
class MyModel(Model):
my_attr = NumberAttribute()
my_model = MyModel()
my_model.update(actions=[
# test update expressions
MyModel.my_attr.set(MyModel.my_attr + 123),
MyModel.my_attr.set(123 + MyModel.my_attr),
MyModel.my_attr.set(MyModel.my_attr - 123),
MyModel.my_attr.set(123 - MyModel.my_attr),
MyModel.my_attr.set(MyModel.my_attr | 123),
])
""") # noqa: E501
示例5: test_list_attribute
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_list_attribute(assert_mypy_output):
assert_mypy_output("""
from pynamodb.attributes import ListAttribute, MapAttribute, UnicodeAttribute
from pynamodb.models import Model
class MyMap(MapAttribute):
my_sub_attr = UnicodeAttribute()
class MyModel(Model):
my_list = ListAttribute(of=MyMap)
my_untyped_list = ListAttribute() # E: Need type annotation for 'my_untyped_list' [var-annotated]
reveal_type(MyModel.my_list) # N: Revealed type is 'pynamodb.attributes.ListAttribute[__main__.MyMap]'
reveal_type(MyModel().my_list) # N: Revealed type is 'builtins.list*[__main__.MyMap*]'
reveal_type(MyModel().my_list[0].my_sub_attr) # N: Revealed type is 'builtins.str*'
# Untyped lists are not well supported yet
reveal_type(MyModel().my_untyped_list[0].my_sub_attr) # N: Revealed type is 'Any'
""")
示例6: test_paths
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_paths(assert_mypy_output):
assert_mypy_output("""
from pynamodb.attributes import ListAttribute, MapAttribute, UnicodeAttribute
from pynamodb.models import Model
class MyMap(MapAttribute):
my_sub_attr = UnicodeAttribute()
class MyModel(Model):
my_list = ListAttribute(of=MyMap)
my_map = MyMap()
reveal_type(MyModel.my_list[0]) # N: Revealed type is 'pynamodb.expressions.operand.Path'
reveal_type(MyModel.my_list[0] == MyModel()) # N: Revealed type is 'pynamodb.expressions.condition.Comparison'
# the following string indexing is not type checked - not by mypy nor in runtime
reveal_type(MyModel.my_list[0]['my_sub_attr'] == 'foobar') # N: Revealed type is 'pynamodb.expressions.condition.Comparison'
""")
示例7: test_exception
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_exception():
class SampleModel(Model):
class Meta:
region = 'us-west-2'
table_name = 'mytable'
sample_attribute = UnicodeAttribute(hash_key=True)
try:
SampleModel.describe_table()
except Exception:
pass
subsegments = xray_recorder.current_segment().subsegments
assert len(subsegments) == 1
subsegment = subsegments[0]
assert subsegment.name == 'dynamodb'
assert len(subsegment.subsegments) == 0
assert subsegment.error
aws_meta = subsegment.aws
assert aws_meta['region'] == 'us-west-2'
assert aws_meta['operation'] == 'DescribeTable'
assert aws_meta['table_name'] == 'mytable'
示例8: test_typed_and_raw_map_json_serialize
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_typed_and_raw_map_json_serialize(self):
class TypedMap(MapAttribute):
map_attr = MapAttribute()
class SomeModel(Model):
key = NumberAttribute(hash_key=True)
typed_map = TypedMap()
item = SomeModel(
typed_map=TypedMap(map_attr={'foo': 'bar'})
)
assert json.dumps({'map_attr': {'foo': 'bar'}}) == json.dumps(item.typed_map.as_dict())
示例9: test_complex_map_accessors
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_complex_map_accessors(self):
class NestedThing(MapAttribute):
double_nested = MapAttribute()
double_nested_renamed = MapAttribute(attr_name='something_else')
class ThingModel(Model):
key = NumberAttribute(hash_key=True)
nested = NestedThing()
t = ThingModel(nested=NestedThing(
double_nested={'hello': 'world'},
double_nested_renamed={'foo': 'bar'})
)
assert t.nested.double_nested.as_dict() == {'hello': 'world'}
assert t.nested.double_nested_renamed.as_dict() == {'foo': 'bar'}
assert t.nested.double_nested.hello == 'world'
assert t.nested.double_nested_renamed.foo == 'bar'
assert t.nested['double_nested'].as_dict() == {'hello': 'world'}
assert t.nested['double_nested_renamed'].as_dict() == {'foo': 'bar'}
assert t.nested['double_nested']['hello'] == 'world'
assert t.nested['double_nested_renamed']['foo'] == 'bar'
with pytest.raises(AttributeError):
bad = t.nested.double_nested.bad
with pytest.raises(AttributeError):
bad = t.nested.bad
with pytest.raises(AttributeError):
bad = t.nested.something_else
with pytest.raises(KeyError):
bad = t.nested.double_nested['bad']
with pytest.raises(KeyError):
bad = t.nested['something_else']
示例10: test_model
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_model(assert_mypy_output):
assert_mypy_output("""
from pynamodb.models import Model
from pynamodb.expressions.operand import Path
class MyModel(Model):
pass
reveal_type(MyModel.count('hash', Path('a').between(1, 3))) # N: Revealed type is 'builtins.int'
""")
示例11: test_model_query
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_model_query(assert_mypy_output):
assert_mypy_output("""
from pynamodb.attributes import NumberAttribute
from pynamodb.models import Model
class MyModel(Model):
my_attr = NumberAttribute()
# test conditions
MyModel.query(123, range_key_condition=(MyModel.my_attr == 5), filter_condition=(MyModel.my_attr == 5))
# test conditions are optional
MyModel.query(123, range_key_condition=None, filter_condition=None)
""")
示例12: test_pagination
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_pagination(assert_mypy_output):
assert_mypy_output("""
from pynamodb.attributes import NumberAttribute
from pynamodb.models import Model
class MyModel(Model):
my_attr = NumberAttribute()
result_iterator = MyModel.query(123)
for model in result_iterator:
reveal_type(model) # N: Revealed type is '__main__.MyModel*'
if result_iterator.last_evaluated_key:
reveal_type(result_iterator.last_evaluated_key['my_attr']) # N: Revealed type is 'builtins.dict*[builtins.str, Any]'
""")
示例13: test_number_attribute
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_number_attribute(assert_mypy_output):
assert_mypy_output("""
from pynamodb.attributes import NumberAttribute
from pynamodb.models import Model
class MyModel(Model):
my_attr = NumberAttribute()
reveal_type(MyModel.my_attr) # N: Revealed type is 'pynamodb.attributes.NumberAttribute'
reveal_type(MyModel().my_attr) # N: Revealed type is 'builtins.float*'
""")
示例14: test_map_attribute
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_map_attribute(assert_mypy_output):
assert_mypy_output("""
from pynamodb.attributes import MapAttribute, UnicodeAttribute
from pynamodb.models import Model
class MySubMap(MapAttribute):
s = UnicodeAttribute()
class MyMap(MapAttribute):
m2 = MySubMap()
class MyModel(Model):
m1 = MyMap()
reveal_type(MyModel.m1) # N: Revealed type is '__main__.MyMap'
reveal_type(MyModel().m1) # N: Revealed type is '__main__.MyMap'
reveal_type(MyModel.m1.m2) # N: Revealed type is '__main__.MySubMap'
reveal_type(MyModel().m1.m2) # N: Revealed type is '__main__.MySubMap'
reveal_type(MyModel.m1.m2.s) # N: Revealed type is 'builtins.str*'
reveal_type(MyModel().m1.m2.s) # N: Revealed type is 'builtins.str*'
reveal_type(MyMap.m2) # N: Revealed type is '__main__.MySubMap'
reveal_type(MyMap().m2) # N: Revealed type is '__main__.MySubMap'
reveal_type(MySubMap.s) # N: Revealed type is 'pynamodb.attributes.UnicodeAttribute'
reveal_type(MySubMap().s) # N: Revealed type is 'builtins.str*'
""")
示例15: test_index_query_scan
# 需要导入模块: from pynamodb import models [as 别名]
# 或者: from pynamodb.models import Model [as 别名]
def test_index_query_scan(assert_mypy_output):
assert_mypy_output("""
from pynamodb.attributes import NumberAttribute
from pynamodb.models import Model
from pynamodb.indexes import GlobalSecondaryIndex
from pynamodb.pagination import ResultIterator
class UntypedIndex(GlobalSecondaryIndex):
bar = NumberAttribute(hash_key=True)
class TypedIndex(GlobalSecondaryIndex[MyModel]):
bar = NumberAttribute(hash_key=True)
class MyModel(Model):
foo = NumberAttribute(hash_key=True)
bar = NumberAttribute()
untyped_index = UntypedIndex()
typed_index = TypedIndex()
# Ensure old code keeps working
untyped_result: ResultIterator = MyModel.untyped_index.query(123)
model: MyModel = next(untyped_result)
not_model: int = next(untyped_result) # this is legacy behavior so it's "fine"
# Allow users to specify which model their indices return
typed_result: ResultIterator[MyModel] = MyModel.typed_index.query(123)
my_model = next(typed_result)
not_model = next(typed_result) # E: Incompatible types in assignment (expression has type "MyModel", variable has type "int") [assignment]
# Ensure old code keeps working
untyped_result = MyModel.untyped_index.scan()
model = next(untyped_result)
not_model = next(untyped_result) # this is legacy behavior so it's "fine"
# Allow users to specify which model their indices return
typed_result = MyModel.typed_index.scan()
model = next(typed_result)
not_model = next(typed_result) # E: Incompatible types in assignment (expression has type "MyModel", variable has type "int") [assignment]
""")