Skip to main content

Advanced dictionary ingestion into python objects

Project description

DictGest - Python Dictionary Ingestion

Code Coverage CI status Docs Discord

Description

When interacting with external REST APIs or with external configuration files we usually do not have control over the received data structure/format.

DictGest makes ingesting dictionary data into python objects(dataclasss objects included) easy when the dictionary data doesn't match 1 to 1 with the Python class:

  • The dictionary might have extra fields that are of no interest
  • The keys names in the dictionary do not match the class attribute names
  • The structure of nested dictionaries does not match the class structure
  • The data types in the dictionary do not match data types of the target class

Examples

Example 1: Trivial Example - Handling Extra parameters

The first most basic and trivial example is ingesting a dictionary that has extra data not of interest

from dictgest import from_dict

car = from_dict(Car, dict_data)

Example 2: Data mapping renaming & rerouting

from typing import Annotated
from dataclasses import dataclass
from dictgest import from_dict, Path

article = from_dict(Article, news_api_data)
meta = from_dict(ArticleMeta, news_api_data)
stats = from_dict(ArticleStats, news_api_data)

The full working example can be found in the examples folder

Example 3: Data type enforcing

Sometimes the data coming from external sources might have different datatypes than what we desire. dictgen can do type conversion for you.

from dataclasses import dataclass
from dictgest import from_dict, typecast 

@typecast # Makes the class type convertable when encountered as typing hint
@dataclass # The dataclass is just an example, it could have an normal class
class Measurment:
    temp: float
    humidity: float


class Sensor:
    def __init__(
        self, name: str, location: str, uptime: float, readings: list[Measurment]
    ):
        ...

The conversions shown above were enabled by setting the @typecast decorator for the targetted classes.

The full working example can be found in the examples folder

Example 4: Custom Data extraction/conversion for a specific field

from typing import Annotated
from dictgest import Path, from_dict


def extract_votes(data):
    # creating a new value from two individual fields and converting them
    return int(data["positive"]) + int(data["negative"])


class Votes:
    def __init__(
        self,
        title,
        total_votes: Annotated[int, Path("details/votes", extractor=extract_votes)],
    ):
        ...

article_data = {
    "title": "Python 4.0 will...",
    "details": {"votes": {"positive": "245", "negative": "30"}},
}


votes = from_dict(Votes, article_data)

The full working example can be found in the examples folder

Example 5: Custom Data conversion for a specific type

from dataclasses import dataclass
from dictgest import default_convertor, from_dict

# Get any already registered bool convertor
default_bool_conv = default_convertor.get(bool)

# create a custom converter
def custom_bool_conv(val):
    if val == "oups":
        return False

    # Let the other cases be treated as before
    return default_bool_conv(val)


# register the custom converter for bool
default_convertor.register(bool, custom_bool_conv)


@dataclass
class Result:
    finished: bool
    notified: bool


result = from_dict(Result, {"finished": True, "notified": "oups"})
print(result)

Installing

pip install dictgest

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

dictgest-0.2.0.tar.gz (7.6 kB view hashes)

Uploaded Source

Built Distribution

dictgest-0.2.0-py3-none-any.whl (8.3 kB view hashes)

Uploaded Python 3

Supported by

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