PynamoDB integration with the Marshmallow (de)serialization library
Project description
Welcome to Marshmallow-Pynamo-DB
Original Project: https://github.com/mathewmarcus/marshmallow-pynamodb
PynamoDB integration with the Marshmallow (de)serialization library.
Installation
From PyPi:
$ pip install marshmallow-pynamo-db
From GitHub:
$ pip install git+https://github.com/chrismaille/marshmallow-pynamodb#egg=marshmallow_pynamodb
Declare your models
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute
class User(Model):
class Meta:
table_name = "user"
email = UnicodeAttribute(null=True)
first_name = UnicodeAttribute(range_key=True)
last_name = UnicodeAttribute(hash_key=True)
Generate marshmallow schemas
from marshmallow_pynamodb import ModelSchema
class UserSchema(ModelSchema):
class Meta:
model = User
user_schema = UserSchema()
(De)serialize your data
user = User(last_name="Smith", first_name="John")
user_schema.dump(user)
# {u'first_name': u'John', u'last_name': u'Smith', u'email': None}
user_schema.load({"last_name": "Smith", "first_name": "John"})
# user<Smith>
pynamodb-attributes support
Currently we support the following custom attributes from pynamodb-attributes library:
IntegerAttribute
– same asNumberAttribute
but whose value is typed asint
(rather thanfloat
)UUIDAttribute
- serializes aUUID
Python object as aS
type attribute (e.g.'a8098c1a-f86e-11da-bd1a-00112444be1e'
)UnicodeEnumAttribute
- serializes a string-valuedEnum
into a Unicode (S
-typed) attributeIntegerEnumAttribute
- serializes a integer-valuedEnum
into a Number (S
-typed) attribute
import uuid
from enum import Enum
from pynamodb.attributes import UnicodeAttribute
from pynamodb.models import Model
from pynamodb_attributes import IntegerAttribute, UUIDAttribute, UnicodeEnumAttribute
from marshmallow_pynamodb import ModelSchema
class Gender(Enum):
male = "male"
female = "female"
not_informed = "not_informed"
class People(Model):
class Meta:
table_name = "people"
uuid = UUIDAttribute(hash_key=True)
first_name = UnicodeAttribute()
last_name = UnicodeAttribute()
gender = UnicodeEnumAttribute(Gender)
age = IntegerAttribute()
class PeopleSchema(ModelSchema):
class Meta:
model = People
people_schema = PeopleSchema()
payload = {
"uuid": "064245dc0e5f415c95d3ba6b8f728ae4",
"first_name": "John",
"last_name": "Doe",
"gender": Gender.male.value,
"age": 43
}
people = people_schema.load(payload)
# people<064245dc-0e5f-415c-95d3-ba6b8f728ae4>
assert people.gender == Gender.male
assert people.uuid == uuid.UUID("064245dc0e5f415c95d3ba6b8f728ae4")
See more examples in tests.
Nested models? No problem
from marshmallow_pynamodb.schema import ModelSchema
from pynamodb.models import Model
from pynamodb.attributes import (
ListAttribute,
MapAttribute,
NumberAttribute,
UnicodeAttribute,
)
class Location(MapAttribute):
latitude = NumberAttribute()
longitude = NumberAttribute()
name = UnicodeAttribute()
class Person(MapAttribute):
firstName = UnicodeAttribute()
lastName = UnicodeAttribute()
age = NumberAttribute()
class OfficeEmployeeMap(MapAttribute):
office_employee_id = NumberAttribute()
person = Person()
office_location = Location()
class Office(Model):
class Meta:
table_name = 'OfficeModel'
office_id = NumberAttribute(hash_key=True)
address = Location()
employees = ListAttribute(of=OfficeEmployeeMap)
class OfficeSchema(ModelSchema):
class Meta:
model = Office
# noinspection PyTypeChecker
OfficeSchema().load(
{
'office_id': 789,
'address': {
'latitude': 6.98454,
'longitude': 172.38832,
'name': 'some_location'
},
'employees': [
{
'office_employee_id': 123,
'person': {
'firstName': 'John',
'lastName': 'Smith',
'age': 45
},
'office_location': {
'latitude': -24.0853,
'longitude': 144.87660,
'name': 'other_location'
}
},
{
'office_employee_id': 456,
'person': {
'firstName': 'Jane',
'lastName': 'Doe',
'age': 33
},
'office_location': {
'latitude': -20.57989,
'longitude': 92.30463,
'name': 'yal'
}
}
]
}
)
# Office<789>
License
MIT licensed. See the bundled LICENSE file for more details.
Not working?
Dont panic. Get a towel and, please, open a issue.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for marshmallow-pynamo-db-0.9.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60ff34b56d6dec291946122513809ce07bf6fe5374e150e3515521208a7816fd |
|
MD5 | 14452aed16e7c32f359c24967402b22d |
|
BLAKE2b-256 | f97fe4ec08a632e1eaa4d7816bc7650b57d7ac41c223666508a07bdf35a73d97 |
Close
Hashes for marshmallow_pynamo_db-0.9.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86d57093862715cbb347313073746280d4d17f89f6d7013cabb745fb5cb3b56d |
|
MD5 | e199b64cefcfc9dde9abc0b3a842501f |
|
BLAKE2b-256 | d53d52e5400943b6d11743bd9bd5fb04c6193ec497ba3080982ba81d4138fb6f |