Skip to main content

Utility lib to convert python dictionaries to valid WDDX data exchange format

Project description

dicttowddx

This is a python script that converts a python dictionary to a wddx string. This is useful when you want to exchange with endpoints that support WDDX format.

Installation

pip install dicttowddx

Supported data types

The following datatypes are supported:

Python WDDX Type
None null
int number
float number
str string
bytes binary (base64 encoded)
datetime dateTime
bool boolean

Usage

import datetime
from dicttowddx import DictToWDDX

c = {
        "a": b"ab",
        "b": None,
        "c": 1,
        "d": [
            None,
            True,
            1.8,
            1,
            "1",
            datetime.datetime(2021, 9, 15, 14, 30, 0, tzinfo=datetime.timezone.utc),
            b"as",
        ],
        "e": datetime.datetime(2021, 9, 15, 14, 30, 0, tzinfo=datetime.timezone.utc),
        "f": None,
        "g": 1.1,
        "h": True,
        "i": "1",
    }

wddx = DictToWDDX(c).to_wddx()

The value of wddx should be:

<wddxPacket version='1.0'><header/><data><struct><var name="a"><string>ab</string></var><var name="b"><null/></var><var name="c"><string>1</string></var><var name="d"><array length="7"><null/><string>True</string><string>1.8</string><string>1</string><string>1</string><string>2021-09-15 14:30:00+00:00</string><string>as</string></array></var><var name="e"><string>2021-09-15 14:30:00+00:00</string></var><var name="f"><null/></var><var name="g"><string>1.1</string></var><var name="h"><string>True</string></var><var name="i"><string>1</string></var></struct></data></wddxPacket>

By default, all data types are converted to string. If you want to keep the original data type, you can use the force_type parameter:

wddx = DictToWDDX(c, force_type=True).to_wddx()

The value of wddx should be:

<wddxPacket version='1.0'><header/><data><struct><var name="a"><binary>YWI=</binary></var><var name="b"><null/></var><var name="c"><number>1</number></var><var name="d"><array length="7"><null/><boolean>True</boolean><number>1.8</number><number>1</number><string>1</string><dateTime>2021-09-15 14:30:00+00:00</dateTime><binary>YXM=</binary></array></var><var name="e"><dateTime>2021-09-15 14:30:00+00:00</dateTime></var><var name="f"><null/></var><var name="g"><number>1.1</number></var><var name="h"><boolean>True</boolean></var><var name="i"><string>1</string></var></struct></data></wddxPacket>

To format the output, you can use pass format_output arg with True and display_indent to any integer greater than 0, default is 4

wddx = DictToWDDX(c, format_output=True, display_indent=2).to_wddx()

The value of wddx should be:

<wddxPacket version='1.0'>
    <header/>
    <data>
        <struct>
            <var name="a">
                <binary>YWI=</binary>
            </var>
            <var name="b">
                <null/>
            </var>
            <var name="c">
                <number>1</number>
            </var>
            <var name="d">
                <array length="7">
                    <null/>
                    <boolean>True</boolean>
                    <number>1.8</number>
                    <number>1</number>
                    <string>1</string>
                    <dateTime>2021-09-15 14:30:00+00:00</dateTime>
                    <binary>YXM=</binary>
                </array>
            </var>
            <var name="e">
                <dateTime>2021-09-15 14:30:00+00:00</dateTime>
            </var>
            <var name="f">
                <null/>
            </var>
            <var name="g">
                <number>1.1</number>
            </var>
            <var name="h">
                <boolean>True</boolean>
            </var>
            <var name="i">
                <string>1</string>
            </var>
        </struct>
    </data>
</wddxPacket>

Tests

python -m pytest tests

Coverage

pip install pytest-cov
python -m pytest tests --cov=dicttowddx

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

dicttowddx-0.2.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

dicttowddx-0.2.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file dicttowddx-0.2.0.tar.gz.

File metadata

  • Download URL: dicttowddx-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for dicttowddx-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8b36f9adc79bc3a6cdfda69dfc6d5df97657bb345bbe3f926bfe03fa7d54c96c
MD5 d14dcd4db65ac0781881892050be0dfc
BLAKE2b-256 9b939213880d2dd944eab6c61ff46ed80108282b36fb186e8604c58fdb84e356

See more details on using hashes here.

Provenance

File details

Details for the file dicttowddx-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: dicttowddx-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for dicttowddx-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a32a2d504117fc8af4bf7190117a2d68be7a78ce2b283bae05d42b7e0c2b4da8
MD5 93aaa1b36514b2d6c7f3f0dea4f39ec7
BLAKE2b-256 5c91fd904e89fc1553d3885b85dcbd393b6fa80949a4cec14ec2a51f0204fc19

See more details on using hashes here.

Provenance

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