dynamodb-mock / ddbmock / database /

# -*- coding: utf-8 -*-

# All validations are performed on *incomming* data => already done :)

from ddbmock.errors import ValidationException

class Key(object):
    Abstraction layer over DynamoDB Keys in :py:class:`ddbmock.database.item.Item`
    def __init__(self, name, typename):
        High level Python constructor

        :param name: Valid key name. No further checks are performed.
        :param typename: Valid key typename. No further checks are performed.
        """ = name
        self.typename = typename

    def read(self, key):
        Parse a key as specified by DynamoDB API and return its value as long as
        its typename matches :py:attr:`typename`

        :param key: Raw DynamoDB request key.

        :return: the value of the key

        :raises: :py:exc:`ddbmock.errors.ValidationException` if field types does not match

        typename, value = key.items()[0]
        if self.typename != typename:
            raise ValidationException('Expected key type = {} for field {}. Got {}'.format(
                self.typename,, typename))

        return value

    def to_dict(self):
        Return the key as a Python dict.

        :return: Serialized version of the key definition metadata compatible with DynamoDB API syntax.
        return {
            "AttributeType": self.typename,

    def from_dict(cls, data):
        Alternate constructor which deciphers raw DynamoDB request data before
        ultimately calling regular ``__init__`` method.

        See :py:meth:`__init__` for more insight.

        :param data: raw DynamoDB request data.

        :return: fully initialized :py:class:`Key` instance
        return cls(data[u'AttributeName'], data[u'AttributeType'])

class PrimaryKey(Key):
    """Special marker to provide distinction between regulat Keys and PrimaryKey"""
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.