pynamodb-encoder
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
Metadata
Release files for pynamodb-encoder 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pynamodb_encoder-0.2.0.tar.gz | 4.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pynamodb_encoder-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.3 kB
Release files / pynamodb_encoder-0.2.0.tar.gz
| Download URL | pynamodb_encoder-0.2.0.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
40d5b8b465ea51d3818eef7a8e085634b31b72edbb8cb6d9680e3c412945a2f6
|
|
BLAKE2b-256 checksum How to use checksums |
dfe3d3d8f91ce486da2a38995a5e0e6d8551c99ca6ec72673d5f22231da671e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.12.3 Linux/6.5.0-1018-azure
|
Release files / pynamodb_encoder-0.2.0-py3-none-any.whl
| Download URL | pynamodb_encoder-0.2.0-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5b02f07586df2ff5261a6b3e8958f6a13112a68cc49ce783982c3ebcc01094fa
|
|
BLAKE2b-256 checksum How to use checksums |
9ddcfc293de0cddf24e8a71f7c08f6378f1cf0ab8c87955d9445606e539cb2ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.2 CPython/3.12.3 Linux/6.5.0-1018-azure
|