Skip to main content

Helper classes that encode/decode pynamodb models to/from JSON serializable dict

Project description

pynamodb-encoder

Build CodeQL codecov Maintainability PyPI version PyPI Supported Python Versions

Introduction

pynamodb-encoder provides helper classes that can convert PynamoDB Model objects into JSON serializable dict. It can also decode such dict back into those Model objects. Polymorphic models and attributes are also supported.

Examples

def test_encode_complex_model(encoder: Encoder):
    class Pet(DynamicMapAttribute):
        cls = DiscriminatorAttribute()
        name = UnicodeAttribute()

    class Cat(Pet, discriminator="Cat"):
        pass

    class Dog(Pet, discriminator="Dog"):
        pass

    class Human(Model):
        name = UnicodeAttribute()
        pets = ListAttribute(of=Pet)

    jon = Human(name="Jon", pets=[Cat(name="Garfield", age=43), Dog(name="Odie")])
    assert encoder.encode(jon) == {
        "name": "Jon",
        "pets": [{"cls": "Cat", "name": "Garfield", "age": 43}, {"cls": "Dog", "name": "Odie"}],
    }

def test_decode_complex_model(decoder: Decoder):
    class Pet(DynamicMapAttribute):
        cls = DiscriminatorAttribute()

    class Cat(Pet, discriminator="Cat"):
        name = UnicodeAttribute()

    class Dog(Pet, discriminator="Dog"):
        breed = UnicodeAttribute()

    class Human(Model):
        name = UnicodeAttribute()
        age = NumberAttribute()
        pets = ListAttribute(of=Pet)

    jon = decoder.decode(
        Human,
        {
            "name": "Jon",
            "age": 70,
            "pets": [{"cls": "Cat", "name": "Garfield"}, {"cls": "Dog", "breed": "Terrier"}],
        },
    )

    assert jon.name == "Jon"
    assert jon.age == 70
    assert isinstance(jon.pets, list)
    assert len(jon.pets) == 2
    assert isinstance(jon.pets[0], Cat)
    assert jon.pets[0].name == "Garfield"
    assert isinstance(jon.pets[1], Dog)
    assert jon.pets[1].breed == "Terrier"

More examples can be found in encoder_test.py and decoder_test.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynamodb_encoder-0.1.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

pynamodb_encoder-0.1.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file pynamodb_encoder-0.1.2.tar.gz.

File metadata

  • Download URL: pynamodb_encoder-0.1.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.10 Linux/5.15.0-1034-azure

File hashes

Hashes for pynamodb_encoder-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f564bc21dda1bd8a82948057c5842316da5e55b146c6193c33f73122068300e6
MD5 266a0b3cc9f5390fe56d74d3b95f573c
BLAKE2b-256 562486bf19b12cc5d5ed804d424c664e734faef1be11177a5fc17eb74bb6859a

See more details on using hashes here.

File details

Details for the file pynamodb_encoder-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pynamodb_encoder-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.10 Linux/5.15.0-1034-azure

File hashes

Hashes for pynamodb_encoder-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f8c81a2271cd9b6fadbc95496301458b0560f892a0bd622160f6ad0d654fb511
MD5 ef7ed3cd4055fad782e02bb3b7477946
BLAKE2b-256 6b8a08483c879a5b9b340ce91407977fe58e76e6f027f7f3f6cf45667492af1e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page