Python pynamodb.models.Model() Examples
The following are 22
code examples of pynamodb.models.Model().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pynamodb.models
, or try the search function
.
Example #1
Source File: test_mypy.py From PynamoDB with MIT License | 6 votes |
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' """)
Example #2
Source File: test_pynamodb.py From aws-xray-sdk-python with Apache License 2.0 | 6 votes |
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'
Example #3
Source File: test_mypy.py From PynamoDB with MIT License | 6 votes |
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' """)
Example #4
Source File: binary_update_test.py From PynamoDB with MIT License | 6 votes |
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
Example #5
Source File: test_mypy.py From PynamoDB with MIT License | 6 votes |
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
Example #6
Source File: binary_update_test.py From PynamoDB with MIT License | 6 votes |
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
Example #7
Source File: test_attributes.py From PynamoDB with MIT License | 6 votes |
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']
Example #8
Source File: test_mypy.py From PynamoDB with MIT License | 5 votes |
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] """)
Example #9
Source File: test_utils.py From graphql-pynamodb with MIT License | 5 votes |
def test_getkeyname_should_workonnumbers(): class MyModel(Model): class Meta: table_name = 'some-table' notmyid = UnicodeAttribute(null=True) myid = NumberAttribute(hash_key=True) assert get_key_name(MyModel) == 'myid'
Example #10
Source File: test_converter.py From graphql-pynamodb with MIT License | 5 votes |
def test_should_onetomany_none_for_unknown_type(): class ModelA(Model): pass class ModelB(Model): a = OneToMany(ModelA) class A(PynamoObjectType): class Meta: model = ModelB dynamic_field = convert_pynamo_attribute(ModelB.a, ModelB.a, A._meta.registry) assert isinstance(dynamic_field, Dynamic) assert dynamic_field.get_type() is None
Example #11
Source File: utils.py From graphql-pynamodb with MIT License | 5 votes |
def get_key_name(model): if not issubclass(model, Model): raise TypeError("Invalid type passed to get_key_name: %s" % model.__class__) if model in MODEL_KEY_REGISTRY: return MODEL_KEY_REGISTRY[model] for attr in vars(model): attr = getattr(model, attr) if isinstance(attr, Attribute) and attr.is_hash_key: MODEL_KEY_REGISTRY[model] = attr.attr_name return attr.attr_name
Example #12
Source File: relationships.py From graphql-pynamodb with MIT License | 5 votes |
def __init__(self, model, lazy=True, **args): if not isinstance(model, string_types) and not issubclass(model, Model): raise TypeError("Expected PynamoDB Model argument, got: %s " % model.__class__.__name__) Attribute.__init__(self, **args) self._model = model self._lazy = lazy self._hash_key_name = None
Example #13
Source File: relationships.py From graphql-pynamodb with MIT License | 5 votes |
def get_model(cls, model_name): # Resolve a model name into a model class by looking in all Model subclasses if not Relationship._models: Relationship._models = Relationship.sub_classes(Model) return next((model for model in Relationship._models if model.__name__ == model_name), None)
Example #14
Source File: relationships.py From graphql-pynamodb with MIT License | 5 votes |
def __init__(self, key_name, key, obj): if isinstance(obj, type) and not issubclass(obj, Model): raise TypeError("Invalid class passed to RelationshipResult, expected a Model class, got %s" % type(obj)) super(RelationshipResult, self).__init__(obj) self._self_key = key self._self_key_name = key_name self._self_model = obj
Example #15
Source File: test_mypy.py From PynamoDB with MIT License | 5 votes |
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' """)
Example #16
Source File: test_attributes.py From PynamoDB with MIT License | 5 votes |
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())
Example #17
Source File: test_attributes.py From PynamoDB with MIT License | 5 votes |
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']
Example #18
Source File: test_mypy.py From PynamoDB with MIT License | 5 votes |
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*' """)
Example #19
Source File: test_mypy.py From PynamoDB with MIT License | 5 votes |
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*' """)
Example #20
Source File: test_mypy.py From PynamoDB with MIT License | 5 votes |
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]' """)
Example #21
Source File: test_mypy.py From PynamoDB with MIT License | 5 votes |
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) """)
Example #22
Source File: types.py From graphql-pynamodb with MIT License | 4 votes |
def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=False, only_fields=(), exclude_fields=(), connection=None, use_connection=None, interfaces=(), id=None, **options): assert model and isclass(model) and issubclass(model, Model), ( 'You need to pass a valid PynamoDB Model in ' '{}.Meta, received "{}".' ).format(cls.__name__, model) if not registry: registry = get_global_registry() assert isinstance(registry, Registry), ( 'The attribute registry in {} needs to be an instance of ' 'Registry, received "{}".' ).format(cls.__name__, registry) pynamo_fields = yank_fields_from_attrs( construct_fields(model, registry, only_fields, exclude_fields), _as=Field, ) if use_connection is None and interfaces: use_connection = any((issubclass(interface, Node) for interface in interfaces)) if use_connection and not connection: # We create the connection automatically connection = Connection.create_type('{}Connection'.format(cls.__name__), node=cls) if connection is not None: assert issubclass(connection, Connection), ( "The connection must be a Connection. Received {}" ).format(connection.__name__) _meta = PynamoObjectTypeOptions(cls) _meta.model = model _meta.registry = registry _meta.fields = pynamo_fields _meta.connection = connection _meta.id = id or 'id' super(PynamoObjectType, cls).__init_subclass_with_meta__(_meta=_meta, interfaces=interfaces, **options) if not skip_registry: registry.register(cls)