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>
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.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7391ca77bdf3b924cc1364d0232423c5ec6d6246bb4ec02621528cb2d01ee5d6 |
|
MD5 | e068d686bcc06e3fcfa803880fae7f76 |
|
BLAKE2b-256 | 6f64f4e8a3d72b510709c151c4003a22389cb44ce7cb145c7f33590c3da1302a |
Close
Hashes for marshmallow_pynamo_db-0.9.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 319c8bfea85952a75ec5964bc3cb172a6925f2bf8c00885a48790202f224a0f9 |
|
MD5 | c80c6b1833f801b67d31e7ab79bfb7da |
|
BLAKE2b-256 | 8fec7e40f0df294db21a2c9da6037ec77a8bed87a24fda45ffe95cf1cefcd06a |