Utilities package for pynamodb.
Project description
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.
Examples are:
- Models with automatic
updated_at
,created_at
anddeleted_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
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
pynamodb_utils-1.5.1.tar.gz
(11.2 kB
view details)
Built Distribution
File details
Details for the file pynamodb_utils-1.5.1.tar.gz
.
File metadata
- Download URL: pynamodb_utils-1.5.1.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 292f9499e83a0b5de6617db4d014a1ba199e8c396de189a199b3bd5856a74bc2 |
|
MD5 | 1634abc822643cfa05343ced804dade5 |
|
BLAKE2b-256 | 869a2555a3f51b26c4640d66aa816a49894af8d2bc50ec4457b03d2a7ac075fd |
File details
Details for the file pynamodb_utils-1.5.1-py3-none-any.whl
.
File metadata
- Download URL: pynamodb_utils-1.5.1-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c87e2f36594bfe03c9c8a374a8e3b67073bd279223f8741e81adde6a699b4848 |
|
MD5 | 381e678169f83487db440ae70c702f56 |
|
BLAKE2b-256 | 856b7d78445b93be495ca5c961314d3f5c3ede8a36ce1f714428f6ff8fe9cf8b |