Skip to main content

Welcome to Marshmallow-Pynamo-DB

PyPI Build PyPI - Python Version Code style: black Code style: black pre-commit

PynamoDB integration with the Marshmallow (de)serialization library.

Installation

From PyPi:

  $ pip install marshmallow-pynamo-db

Versions

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 as NumberAttribute but whose value is typed as int (rather than float)
  • UUIDAttribute - serializes a UUID Python object as a S type attribute (e.g. 'a8098c1a-f86e-11da-bd1a-00112444be1e')
  • UnicodeEnumAttribute - serializes a string-valued Enum into a Unicode (S-typed) attribute
  • IntegerEnumAttribute - serializes a integer-valued Enum 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>

Using polyformed Models

# pip install pynamodb_attributes
import uuid
from pynamodb_attributes import UnicodeEnumAttribute, UUIDAttribute
from pynamodb.attributes import UnicodeAttribute, DiscriminatorAttribute
from pynamodb.models import Model
from marshmallow_pynamodb import ModelSchema
from enum import Enum

class MyStatus(Enum):
   CREATED = "CREATED"

class BaseDocument(Model):
  uuid = UUIDAttribute(default=uuid.uuid4)
  cls = DiscriminatorAttribute()

class MyDocument(BaseDocument, discriminator='my_document'):
  status = UnicodeEnumAttribute(MyStatus, default=MyStatus.CREATED)
  content = UnicodeAttribute()

class MyDocumentSchema(ModelSchema):
  class Meta:
    model = MyDocument

instance = MyDocumentSchema().load({"content": "foo"})
assert instance.content == "foo"
assert instance.uuid is not None

License

MIT licensed. See the bundled LICENSE file for more details.

Not working?

Dont panic. Get a towel and, please, open an issue.

Metadata

Release files for marshmallow-pynamo-db 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for marshmallow-pynamo-db 1.1.0
File Size Uploaded Provenance
marshmallow-pynamo-db-1.1.0.tar.gz 9.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for marshmallow-pynamo-db 1.1.0
File Interpreter ABI Platform
marshmallow_pynamo_db-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.3 kB

Release files / marshmallow-pynamo-db-1.1.0.tar.gz

Download URL marshmallow-pynamo-db-1.1.0.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
08d8b64508011f1d4ac842a36f14ca2b063e342eda6e3271a84722cb589902f3
BLAKE2b-256 checksum
How to use checksums
ec589662eea6c2dc8746e26d46ba0bf5becedb869059b34d545811da6c0411a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

Release files / marshmallow_pynamo_db-1.1.0-py3-none-any.whl

Download URL marshmallow_pynamo_db-1.1.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
970a6c8d678f365036f9a071aa6ce3451cbc742f0a596548d66b12157c5d84cc
BLAKE2b-256 checksum
How to use checksums
36a900de6d39c10f96a07f89a8a0e4292ad79aa7746ed3da2cfde11eab6a7732
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

Security for marshmallow-pynamo-db 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page