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

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
@dataclass # The dataclass is just an example, it could have an normal class
class Measurment:
    temp: float
    humidity: float


@typecast # Makes the class type convertable
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

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.1.0.tar.gz (6.4 kB view hashes)

Uploaded Source

Built Distribution

dictgest-0.1.0-py3-none-any.whl (7.0 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