Skip to main content

A tool for mapping and transforming dictionaries based on predefined rules.

Project description

Dictionary Mapper

PyPI - Version PyPI - Python Version


Table of Contents

Usage

This library can map a source dictionary and generate a target new one following dot notation in source and target dictionaries.

Example:

  1. Define a SpecEntry dict, keys are the source dict paths, values are the target dict paths:
from dictionary_mapper import SpecEntry

spec: SpecEntry = {
    "body.int_field": "int_field",
    "body.str_field": "str_field",
    "body.complex_field.nested_int": {
        "path": "complex_field.nested_int",
        "default": 0,
        "transform": lambda x: cast("int", x) * 2,
    },
    "body.complex_field.nested_str": "complex_field.nested_str",
    "body.list[0].str_field": "list_field",
    "body.int_list": "int_list",
    "body.str_list": "str_list",
}
  1. For raw dict output, then use the RawDictionaryMapper class
...
from typing import cast
from dictionary_mapper import RawDictionaryMapper
...

src: dict[str, object] = {
    "body": {
        "int_field": 10,
        "str_field": "hello",
        "complex_field": {
            "nested_int": 5,
            "nested_str": "world",
        },
        "list": [
            {
                "str_field": "test field",
            },
        ],
        "int_list": [1, 2, 3],
        "str_list": ["1", "2", "3"],
    },
}


dm: RawDictionaryMapper = RawDictionaryMapper()

maped_dict: dict[str, object] = dm.create_transformed_dict(src, spec)

assert maped_dict["int_field"] == 10
assert maped_dict["str_field"] == "hello"
assert cast("dict[str, object]", maped_dict["complex_field"])["nested_int"] == 10  # Transformed
assert cast("dict[str, object]", maped_dict["complex_field"])["nested_str"] == "world"
assert maped_dict["list_field"] == "test field"
assert maped_dict["int_list"] == [1, 2, 3]
assert maped_dict["str_list"] == ["1", "2", "3"]
  1. For TypedDicts you can use the TypedDictionaryMapper as follows
...
from dictionary_mapper import TypedDictionaryMapper
...

class MyNestedDict(TypedDict):
    nested_int: int
    nested_str: str


class MyTypedDict(TypedDict):
    int_field: int
    str_field: str
    complex_field: MyNestedDict
    list_field: str
    int_list: list[int]
    str_list: list[str]

...

dm: TypedDictionaryMapper[MyTypedDict] = TypedDictionaryMapper()

maped_dict: MyTypedDict = dm.create_transformed_dict(src, spec)

assert maped_dict["int_field"] == 10
assert maped_dict["str_field"] == "hello"
assert maped_dict["complex_field"]["nested_int"] == 10  # Transformed
assert maped_dict["complex_field"]["nested_str"] == "world"
assert maped_dict["list_field"] == "test field"
assert maped_dict["int_list"] == [1, 2, 3]
assert maped_dict["str_list"] == ["1", "2", "3"]

Installation

Add recommended extensions at .vscode/extensions.json. Then run:

pip install hatch hatch-pip-deepfreeze
hatch shell

These two commands creates everithing for you and vscode to start working ASAP.

License

dictionary-mapper is distributed under the terms of the MIT license.

Project details


Download files

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

Source Distribution

dictionary_mapper-0.0.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

dictionary_mapper-0.0.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file dictionary_mapper-0.0.0.tar.gz.

File metadata

  • Download URL: dictionary_mapper-0.0.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dictionary_mapper-0.0.0.tar.gz
Algorithm Hash digest
SHA256 1274c8747b9b0ecad4c9d7e4dca0b39b90a16859d64cf8b7e60410e4e0311aa0
MD5 c5aaa496233f56ce02daeaefd13254a8
BLAKE2b-256 d7b79120917b1045f263baea1f2dfc9d9ef55ffe76235f6f73df5af4bebe38b1

See more details on using hashes here.

File details

Details for the file dictionary_mapper-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dictionary_mapper-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6513fa960ec7c16b2d41d97ad00cd524997b3eef8839c554968fc31120f19f05
MD5 308e8e140456b418bded7d5fd677bf42
BLAKE2b-256 92ca3544810dde843fad33494de4e526e6aa9dd42a71b99ac635eaa9b08f5b82

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page