Skip to main content

Introduction

Pynamodb Utils is a collection of small helper functions, utilities and classes which make common patterns easier. It helped me make my job easier in the past.

Tests pypi

Examples are:

  • Models with automatic updated_at, created_at and deleted_at fields
  • Attributes for enums and dynamic mappings
  • Class with methods that allow to generate from JSON/dict query/scan conditions

To install:

Run pip install pynamodb-utils or execute python setup.py install in the source directory

Example of Usage

To setup pynamodb models with authomaticly generated timestamps and useful functions allowing serialization of scan conditions from JSON input from API.

from datetime import timezone, datetime
from pynamodb.attributes import UnicodeAttribute
from pynamodb_utils import DynamicMapAttribute, AsDictModel,
JSONQueryModel, TimestampedModel


class CategoryEnum(enum.Enum):
    finance = enum.auto()
    politics = enum.auto()

class PostCategoryCreatedAtGSI(GlobalSecondaryIndex):
    category = EnumAttribute(hash_key=True, enum=CategoryEnum)
    created_at = UTCDateTimeAttribute(range_key=True)

    class Meta:
        index_name = "example-index-name"
        projection = AllProjection

class Post(AsDictModel, JSONQueryModel, TimestampedModel):
    name = UnicodeAttribute(hash_key=True)
    sub_name = UnicodeAttribute(range_key=True)
    category = EnumAttribute(enum=CategoryEnum, default=CategoryEnum.finance)
    content = UnicodeAttribute()
    tags = DynamicMapAttribute(default={})
    category_created_at_gsi = PostCategoryCreatedAtGSI()

    class Meta:
        table_name = 'example-table-name'
        TZINFO = timezone.utc

Post.create_table(read_capacity_units=10, write_capacity_units=10)

post = Post(
    name='A weekly news.',
    sub_name='Shocking revelations',
    content='Last week took place...',
    category=CategoryEnum.finance,
    tags={
        "type": "news",
        "topics": ["stock exchange", "NYSE"]
    }
)
post.save()

condition = Post.make_index_query(
    query={
        "created_at__lte": str(datetime.now()),
        "sub_name__exists": None,
        "category__equals": "finance",
        "OR": {"tags.type__equals": "news", "tags.topics__contains": ["NYSE"]},
    }
) # class method executes query on the most optimal index
print(next(results).as_dict())

That lines of code should result with following output

{
        'name': 'A weekly news.',
        'created_at': '2019-01-01 00:00:00+00:00',
        'updated_at': '2019-01-01 00:00:00+00:00',
        'deleted_at': None,
        'content': 'Last week took place...',
        'tags': {
            'type': 'news',
            'topics': ['stock exchange', 'NYSE']
        }
    }

Links

Download files

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

Source Distribution

pynamodb_utils-1.5.2.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pynamodb_utils-1.5.2-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file pynamodb_utils-1.5.2.tar.gz.

File metadata

  • Download URL: pynamodb_utils-1.5.2.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for pynamodb_utils-1.5.2.tar.gz
Algorithm Hash digest
SHA256 9d6641ccc7f0b6050c05fa51860c93ec26a360a9efad54688169142a31ea18c3
MD5 f48bf7b611563a6d30306ce6f80468f5
BLAKE2b-256 3c1376132884194b460c1760309679b29f98115333e75b94a0aaab72889f04dc

See more details on using hashes here.

File details

Details for the file pynamodb_utils-1.5.2-py3-none-any.whl.

File metadata

  • Download URL: pynamodb_utils-1.5.2-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.3

File hashes

Hashes for pynamodb_utils-1.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 99aaad207d1d056a896963788dd1fb32a2d033417a1c545f7ab69720ce69546b
MD5 0619e1b4da8fca66bf5fe98b4b633c95
BLAKE2b-256 5f0a801d3062e6cabea25c509cf7af1fdf878a9f923641d097da4a894da59f14

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.5.2 This release

2 files

1.5.1

2 files

1.5.0

2 files

1.4.0

2 files

1.3.7

2 files

1.3.6

2 files

1.3.5

2 files

1.3.4

2 files

1.3.3

2 files

1.3.2

2 files

1.3.0

2 files

1.2.1

2 files

1.2.0

2 files

1.1.9

2 files

1.1.8

2 files

1.1.7

2 files

1.1.6

2 files

1.1.5

2 files

1.1.4

2 files

1.1.2

2 files

1.1.1

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

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