dynamizer


Namedynamizer JSON
Version 0.2.12 PyPI version JSON
download
home_pageNone
SummaryDynamodb model maker.
upload_time2024-04-16 04:37:50
maintainerNone
docs_urlNone
authorKevin Schiroo
requires_python<4.0,>=3.10
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Dynamizer

A supportive set of defaults for dynamodb. Dynamizer doesn't aim to
abstract away the nuances of dynamodb, but rather to provide effective
defaults for the repetitive tasks of (de)serializing models to and from
dynamodb.

It provides:

- Automatic deserialization from dynamo's format. This is helpful when
  processing a change stream from dynamo.
- Overwrite detection. If a model is changed between reading it and writing
  it back to dynamodb an error will be raised, preventing the unintentional
  overwriting of existing data.
- Supportive base delete, save, and load function to make a programmers life
  easier.

## Usage

Dynamizer models are built on top of immutable data classes.

```python
import dataclasses
import dynamizer


@dataclasses.dataclass(frozen=True)
class DemoClass(dynamizer.DynamiteModel):
    """A demo class."""

    foobar: str
    fizbuzz: typing.Optional[str] = None

    @property
    def hash_key(self) -> str:
        """
        Get the hash key.

        Dynamizer can't tell you what the right hash/range key scheme is
        write for your use case and access patterns so you must define
        how to translate the model's data into a hash and range key.
        """
        return f"hash-key/{self.foobar}"

    @property
    def range_key(self) -> str:
        """Get the range key."""
        return "/range-key"

    def _gs1(self) -> str:
        """
        Get the gs1 value.

        Dynamizer will also support you in instantiating any global secondary
        indices. It expects these to be defined matching the pattern
        `^_?gs\d+$`. Dynamizer searches the class for any functions matching
        this pattern and automatically manages their lifecycle within dynamodb,
        creating them dynamically as the model is saved, and deleting them
        if they are null in some part of the models life. Note that The index
        itself still needs to be created within dynamodb.
        """
        return f"{self.foobar}/hash-key"

    @classmethod
    def load(cls, my_identifier: typing.Any) -> "DemoClass":
        """
        Load a model from dynamodb.

        Dynamizer does not provide a default load function, since it doesn't
        know all of your specific access patterns and how incoming arguments
        are to be translated into a hash key, range key pair, but it does
        provide helpful secondary methods to make writing a load function
        easier.
        """
        hash_key = _hash_key(my_identifier)
        range_key = _hash_key(my_identifier)
        client = boto3.client("dynamodb")
        result = cls._base_load(client, "my-table-name", hash_key, range_key)
        if result is None:
            raise NotFoundError(f"Could not find {my_identifier}")
        return result

    @classmethod
    def list_group(cls, my_group: typing.Any) -> typing.List["DemoClass"]:
        """
        List a model under the specified group.

        Dynamizer provides methods for easily converting a dynamodb response
        into a python model making it easier to write functions defining a
        variety of access patterns.
        """
        ...
        response = client.query("Something fancy")
        return [cls.inflate(item) for item in response.get("Items", [])]


    def save(self) -> "DemoClass":
        """
        Save a model.

        Dynamizer keeps a count of changes to a model and checks when saving
        to ensure that no changes have come in since the model was initially
        read. This prevents data from being overwritten both during initial
        creation and during updates.
        """
        ...
        return self._base_save(client, "table_to_save_to")

    def delete(self):
        """
        Delete a model.

        Like during saves, Dynamizer keeps a count of changes and will prevent
        a delete from taking place if an update has taken place since the last
        read.
        """
        ...
        self._base_delete(client, "table_to_delete_from")
```

## Mocking

Dynamizer provides a mechanism for mocking out dynamodb calls for testing. The
initial state of dynamodb can be set via yaml files.

```python
import dynamizer.mock

data = yaml.safe_load("/path/to/data.yaml")

with dynamizer.mock.from_yaml(data):
    # Within this context dynamodb will have the state defined in data.yaml
```

The expected format of the yaml file is:

```yaml
- table_name: my-table-name
  region: us-east-1 # Optional
  secondary_indexes: # Optional
    - name: gs1-gs2
      hash_key: gs1
      range_key: gs2
  objects:
    MyDynamizerSubCls:
      - foo: {S: bar}
        fiz: {S: buzz}
    AnotherDynamizerSubCls:
      - mock: {S: data}
```
            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "dynamizer",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": null,
    "author": "Kevin Schiroo",
    "author_email": "kjschiroo@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/17/91/7906327fba97a28af4fb544fbc7bcf72e0b3997d22cca6a1d3c232e1cb69/dynamizer-0.2.12.tar.gz",
    "platform": null,
    "description": "# Dynamizer\n\nA supportive set of defaults for dynamodb. Dynamizer doesn't aim to\nabstract away the nuances of dynamodb, but rather to provide effective\ndefaults for the repetitive tasks of (de)serializing models to and from\ndynamodb.\n\nIt provides:\n\n- Automatic deserialization from dynamo's format. This is helpful when\n  processing a change stream from dynamo.\n- Overwrite detection. If a model is changed between reading it and writing\n  it back to dynamodb an error will be raised, preventing the unintentional\n  overwriting of existing data.\n- Supportive base delete, save, and load function to make a programmers life\n  easier.\n\n## Usage\n\nDynamizer models are built on top of immutable data classes.\n\n```python\nimport dataclasses\nimport dynamizer\n\n\n@dataclasses.dataclass(frozen=True)\nclass DemoClass(dynamizer.DynamiteModel):\n    \"\"\"A demo class.\"\"\"\n\n    foobar: str\n    fizbuzz: typing.Optional[str] = None\n\n    @property\n    def hash_key(self) -> str:\n        \"\"\"\n        Get the hash key.\n\n        Dynamizer can't tell you what the right hash/range key scheme is\n        write for your use case and access patterns so you must define\n        how to translate the model's data into a hash and range key.\n        \"\"\"\n        return f\"hash-key/{self.foobar}\"\n\n    @property\n    def range_key(self) -> str:\n        \"\"\"Get the range key.\"\"\"\n        return \"/range-key\"\n\n    def _gs1(self) -> str:\n        \"\"\"\n        Get the gs1 value.\n\n        Dynamizer will also support you in instantiating any global secondary\n        indices. It expects these to be defined matching the pattern\n        `^_?gs\\d+$`. Dynamizer searches the class for any functions matching\n        this pattern and automatically manages their lifecycle within dynamodb,\n        creating them dynamically as the model is saved, and deleting them\n        if they are null in some part of the models life. Note that The index\n        itself still needs to be created within dynamodb.\n        \"\"\"\n        return f\"{self.foobar}/hash-key\"\n\n    @classmethod\n    def load(cls, my_identifier: typing.Any) -> \"DemoClass\":\n        \"\"\"\n        Load a model from dynamodb.\n\n        Dynamizer does not provide a default load function, since it doesn't\n        know all of your specific access patterns and how incoming arguments\n        are to be translated into a hash key, range key pair, but it does\n        provide helpful secondary methods to make writing a load function\n        easier.\n        \"\"\"\n        hash_key = _hash_key(my_identifier)\n        range_key = _hash_key(my_identifier)\n        client = boto3.client(\"dynamodb\")\n        result = cls._base_load(client, \"my-table-name\", hash_key, range_key)\n        if result is None:\n            raise NotFoundError(f\"Could not find {my_identifier}\")\n        return result\n\n    @classmethod\n    def list_group(cls, my_group: typing.Any) -> typing.List[\"DemoClass\"]:\n        \"\"\"\n        List a model under the specified group.\n\n        Dynamizer provides methods for easily converting a dynamodb response\n        into a python model making it easier to write functions defining a\n        variety of access patterns.\n        \"\"\"\n        ...\n        response = client.query(\"Something fancy\")\n        return [cls.inflate(item) for item in response.get(\"Items\", [])]\n\n\n    def save(self) -> \"DemoClass\":\n        \"\"\"\n        Save a model.\n\n        Dynamizer keeps a count of changes to a model and checks when saving\n        to ensure that no changes have come in since the model was initially\n        read. This prevents data from being overwritten both during initial\n        creation and during updates.\n        \"\"\"\n        ...\n        return self._base_save(client, \"table_to_save_to\")\n\n    def delete(self):\n        \"\"\"\n        Delete a model.\n\n        Like during saves, Dynamizer keeps a count of changes and will prevent\n        a delete from taking place if an update has taken place since the last\n        read.\n        \"\"\"\n        ...\n        self._base_delete(client, \"table_to_delete_from\")\n```\n\n## Mocking\n\nDynamizer provides a mechanism for mocking out dynamodb calls for testing. The\ninitial state of dynamodb can be set via yaml files.\n\n```python\nimport dynamizer.mock\n\ndata = yaml.safe_load(\"/path/to/data.yaml\")\n\nwith dynamizer.mock.from_yaml(data):\n    # Within this context dynamodb will have the state defined in data.yaml\n```\n\nThe expected format of the yaml file is:\n\n```yaml\n- table_name: my-table-name\n  region: us-east-1 # Optional\n  secondary_indexes: # Optional\n    - name: gs1-gs2\n      hash_key: gs1\n      range_key: gs2\n  objects:\n    MyDynamizerSubCls:\n      - foo: {S: bar}\n        fiz: {S: buzz}\n    AnotherDynamizerSubCls:\n      - mock: {S: data}\n```",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Dynamodb model maker.",
    "version": "0.2.12",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e52a0cc494239f8961464fe585ffa98133848066e9fcdc51100590783b2b5804",
                "md5": "4786038d57522e5a275d32fc0a45ea45",
                "sha256": "8ece36f172dd6120fe3ef37376bed3471c2756d434887559917b40ce47604a52"
            },
            "downloads": -1,
            "filename": "dynamizer-0.2.12-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4786038d57522e5a275d32fc0a45ea45",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 9769,
            "upload_time": "2024-04-16T04:37:48",
            "upload_time_iso_8601": "2024-04-16T04:37:48.663645Z",
            "url": "https://files.pythonhosted.org/packages/e5/2a/0cc494239f8961464fe585ffa98133848066e9fcdc51100590783b2b5804/dynamizer-0.2.12-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "17917906327fba97a28af4fb544fbc7bcf72e0b3997d22cca6a1d3c232e1cb69",
                "md5": "4ec6465209d21ca845a9afe35600758a",
                "sha256": "0b7ee1acfd84e3c9bc06f217ee0b3a79620d811c2b62baaef92bdf2f54908b4b"
            },
            "downloads": -1,
            "filename": "dynamizer-0.2.12.tar.gz",
            "has_sig": false,
            "md5_digest": "4ec6465209d21ca845a9afe35600758a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 9850,
            "upload_time": "2024-04-16T04:37:50",
            "upload_time_iso_8601": "2024-04-16T04:37:50.383869Z",
            "url": "https://files.pythonhosted.org/packages/17/91/7906327fba97a28af4fb544fbc7bcf72e0b3997d22cca6a1d3c232e1cb69/dynamizer-0.2.12.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-16 04:37:50",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "dynamizer"
}
        
Elapsed time: 0.25192s