# 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
```bash
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
```python
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:
```text
<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:
```python
wddx = DictToWDDX(c, force_type=True).to_wddx()
```
The value of `wddx` should be:
```text
<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`
```python
wddx = DictToWDDX(c, format_output=True, display_indent=2).to_wddx()
```
The value of `wddx` should be:
```text
<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
```bash
python -m pytest tests
```
### Coverage
```bash
pip install pytest-cov
python -m pytest tests --cov=dicttowddx
```
Raw data
{
"_id": null,
"home_page": "https://github.com/dotman14/dicttowddx",
"name": "dicttowddx",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8.0",
"maintainer_email": "",
"keywords": "python,wddx,data exchange",
"author": "Oyedotun Oyesanmi",
"author_email": "dotunoyesanmi@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/9b/93/9213880d2dd944eab6c61ff46ed80108282b36fb186e8604c58fdb84e356/dicttowddx-0.2.0.tar.gz",
"platform": null,
"description": "# dicttowddx\n\nThis 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.\n\n## Installation\n\n```bash\npip install dicttowddx\n```\n\n## Supported data types\nThe following datatypes are supported:\n\n| Python | WDDX Type |\n|----------|-------------------------|\n| None | null |\n| int | number |\n| float | number |\n| str | string |\n| bytes | binary (base64 encoded) |\n| datetime | dateTime |\n| bool | boolean |\n\n## Usage\n\n```python\nimport datetime\nfrom dicttowddx import DictToWDDX\n\nc = {\n \"a\": b\"ab\",\n \"b\": None,\n \"c\": 1,\n \"d\": [\n None,\n True,\n 1.8,\n 1,\n \"1\",\n datetime.datetime(2021, 9, 15, 14, 30, 0, tzinfo=datetime.timezone.utc),\n b\"as\",\n ],\n \"e\": datetime.datetime(2021, 9, 15, 14, 30, 0, tzinfo=datetime.timezone.utc),\n \"f\": None,\n \"g\": 1.1,\n \"h\": True,\n \"i\": \"1\",\n }\n\nwddx = DictToWDDX(c).to_wddx()\n```\nThe value of `wddx` should be:\n```text\n<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>\n```\n\nBy default, all data types are converted to string. If you want to keep the original data type, you can use the `force_type` parameter:\n```python\nwddx = DictToWDDX(c, force_type=True).to_wddx()\n```\nThe value of `wddx` should be:\n```text\n<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>\n```\n\nTo format the output, you can use pass `format_output` arg with `True` and `display_indent` to any integer greater than `0`, default is `4`\n```python\nwddx = DictToWDDX(c, format_output=True, display_indent=2).to_wddx()\n```\nThe value of `wddx` should be:\n```text\n<wddxPacket version='1.0'>\n <header/>\n <data>\n <struct>\n <var name=\"a\">\n <binary>YWI=</binary>\n </var>\n <var name=\"b\">\n <null/>\n </var>\n <var name=\"c\">\n <number>1</number>\n </var>\n <var name=\"d\">\n <array length=\"7\">\n <null/>\n <boolean>True</boolean>\n <number>1.8</number>\n <number>1</number>\n <string>1</string>\n <dateTime>2021-09-15 14:30:00+00:00</dateTime>\n <binary>YXM=</binary>\n </array>\n </var>\n <var name=\"e\">\n <dateTime>2021-09-15 14:30:00+00:00</dateTime>\n </var>\n <var name=\"f\">\n <null/>\n </var>\n <var name=\"g\">\n <number>1.1</number>\n </var>\n <var name=\"h\">\n <boolean>True</boolean>\n </var>\n <var name=\"i\">\n <string>1</string>\n </var>\n </struct>\n </data>\n</wddxPacket>\n```\n## Tests\n\n```bash\npython -m pytest tests\n```\n\n### Coverage\n\n```bash\npip install pytest-cov\npython -m pytest tests --cov=dicttowddx\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Utility lib to convert python dictionaries to valid WDDX data exchange format",
"version": "0.2.0",
"project_urls": {
"Homepage": "https://github.com/dotman14/dicttowddx"
},
"split_keywords": [
"python",
"wddx",
"data exchange"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5c91fd904e89fc1553d3885b85dcbd393b6fa80949a4cec14ec2a51f0204fc19",
"md5": "93aaa1b36514b2d6c7f3f0dea4f39ec7",
"sha256": "a32a2d504117fc8af4bf7190117a2d68be7a78ce2b283bae05d42b7e0c2b4da8"
},
"downloads": -1,
"filename": "dicttowddx-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "93aaa1b36514b2d6c7f3f0dea4f39ec7",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8.0",
"size": 4623,
"upload_time": "2023-10-30T14:37:36",
"upload_time_iso_8601": "2023-10-30T14:37:36.454898Z",
"url": "https://files.pythonhosted.org/packages/5c/91/fd904e89fc1553d3885b85dcbd393b6fa80949a4cec14ec2a51f0204fc19/dicttowddx-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9b939213880d2dd944eab6c61ff46ed80108282b36fb186e8604c58fdb84e356",
"md5": "d14dcd4db65ac0781881892050be0dfc",
"sha256": "8b36f9adc79bc3a6cdfda69dfc6d5df97657bb345bbe3f926bfe03fa7d54c96c"
},
"downloads": -1,
"filename": "dicttowddx-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "d14dcd4db65ac0781881892050be0dfc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8.0",
"size": 5086,
"upload_time": "2023-10-30T14:37:38",
"upload_time_iso_8601": "2023-10-30T14:37:38.024476Z",
"url": "https://files.pythonhosted.org/packages/9b/93/9213880d2dd944eab6c61ff46ed80108282b36fb186e8604c58fdb84e356/dicttowddx-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-30 14:37:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dotman14",
"github_project": "dicttowddx",
"github_not_found": true,
"lcname": "dicttowddx"
}