Name | dict2xml JSON |
Version |
1.7.6
JSON |
| download |
home_page | None |
Summary | Small utility to convert a python dictionary into an XML string |
upload_time | 2024-08-08 01:12:13 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.5 |
license | None |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
dict2xml
========
Super Simple utility to convert a python dictionary into an xml string
Installation
------------
Install using pip::
> python -m pip install dict2xml
example
-------
.. code-block:: python
from dict2xml import dict2xml
data = {
'a': 1,
'b': [2, 3],
'c': {
'd': [
{'p': 9},
{'o': 10}
],
'e': 7
}
}
print dict2xml(data, wrap="all", indent=" ")
Output
------
.. code-block:: xml
<all>
<a>1</a>
<b>2</b>
<b>3</b>
<c>
<d>
<p>9</p>
</d>
<d>
<o>10</o>
</d>
<e>7</e>
</c>
</all>
methods
-------
``dict2xml.dict2xml(data, *args, **kwargs)``
Equivalent to:
.. code-block:: python
dict2xml.Converter(*args, **kwargs).build(data)
``dict2xml.Converter(wrap="", indent=" ", newlines=True)``
Knows how to convert a dictionary into an xml string
* wrap: Wraps the entire tree in this tag
* indent: Amount to prefix each line for each level of nesting
* newlines: Whether or not to use newlines
``dict2xml.Converter.build(data, iterables_repeat_wrap=True, closed_tags_for=None, data_sorter=None)``
Instance method on Converter that takes in the data and creates the xml string
* iterables_repeat_wrap - when false the key the array is in will be repeated
* closed_tags_for - an array of values that will produce self closing tags
* data_sorter - an object as explained below for sorting keys in maps
``dict2xml.DataSorter``
An object used to determine the sorting of keys for a map of data.
By default an ``OrderedDict`` object will not have it's keys sorted, but any
other type of mapping will.
It can be made so even ``OrderedDict`` will get sorted by passing in
``data_sorter=DataSorter.always()``.
Or it can be made so that keys are produced from the sorting determined by
the mapping with ``data_sorter=DataSorter.never()``.
.. note:: When this library was first created python did not have deterministic
sorting for normal dictionaries which is why default everything gets sorted but
``OrderedDict`` do not.
To create custom sorting logic requires an object that has a single ``keys_from``
method on it that accepts a map of data and returns a list of strings, where only
the keys that appear in the list will go into the output and those keys must exist
in the original mapping.
Self closing tags
-----------------
To produce self closing tags (like ``<item/>``) then the ``build`` method must
be given a list of values under ``closed_tags_for``. For example, if you want
``None`` to produce a closing tag then:
.. code-block:: python
example = {
"item1": None,
"item2": {"string1": "", "string2": None},
"item3": "special",
}
result = Converter("").build(example, closed_tags_for=[None])
assert result == dedent("""
<item1/>
<item2>
<string1></string1>
<string2/>
</item2>
<item3>special</item3>
""").strip())
Here only ``string2`` gets a self closing tag because it has data of ``None``,
which has been designated as special.
If you want to dynamically work out which tags should be self closing then you
may provide an object that implements ``__eq__`` and do your logic there.
Limitations
-----------
* No attributes on elements
* Currently no explicit way to hook into how to cope with your custom data
* Currently no way to insert an xml declaration line
Changelog
---------
1.7.6 - 8 August 2024
* Fixed the ``dict2xml.dict2xml`` entry point to distribute options
correctly
1.7.5 - 13 February 2024
* Introduced the ``data_sorter`` option
1.7.4 - 16 January 2024
* Make the tests compatible with pytest8
1.7.3 - 25 Feb 2023
* This version has no changes to the installed code.
* This release converts to hatch for packaging and adds a wheel to the
package on pypi.
* CI will now run against python 3.11 as well
1.7.2 - 18 Oct 2022
* This version has no changes to the installed code.
* This release adds the tests to the source distribution put onto pypi.
1.7.1 - 16 Feb 2022
* Adding an option to have self closing tags when the value for that
tag equals certain values
1.7.0 - 16 April, 2020
* Use collections.abc to avoid deprecation warning. Thanks @mangin.
* This library no longer supports Python2 and is only supported for
Python3.6+. Note that the library should still work in Python3.5 as I
have not used f-strings, but the framework I use for the tests is only 3.6+.
1.6.1 - August 27, 2019
* Include readme and LICENSE in the package
1.6 - April 27, 2018
* No code changes
* changed the licence to MIT
* Added more metadata to pypi
* Enabled travis ci
* Updated the tests slightly
1.5
* No changelog was kept before this point.
Development
-----------
To enter a virtualenv with dict2xml and dev requirements installed run::
> source run.sh activate
Tests may be run with::
> ./test.sh
Or::
> ./run.sh tox
Linting and formatting is via::
> ./format
> ./lint
Python Black will work on the tests as long as ``NOSE_OF_YETI_BLACK_COMPAT=true``
and the correct version of black is available. This is true if your editor
is opened in the same terminal session after sourcing run.sh or if
you make sure that environment variable is set and the editor is using the
virtualenv made by running or sourcing ``run.sh`` (``tools/venv/.python``)
Raw data
{
"_id": null,
"home_page": null,
"name": "dict2xml",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.5",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "Stephen Moore <stephen@delfick.com>",
"download_url": "https://files.pythonhosted.org/packages/07/b3/2db1b077f9a80d30d1729a0af69b7cef8ce5b45c77a1f0f9ebb01b9edce1/dict2xml-1.7.6.tar.gz",
"platform": null,
"description": "dict2xml\n========\n\nSuper Simple utility to convert a python dictionary into an xml string\n\nInstallation\n------------\n\nInstall using pip::\n\n > python -m pip install dict2xml\n\nexample\n-------\n\n.. code-block:: python\n\n from dict2xml import dict2xml\n\n data = {\n 'a': 1,\n 'b': [2, 3],\n 'c': {\n 'd': [\n {'p': 9},\n {'o': 10}\n ],\n 'e': 7\n }\n }\n\n print dict2xml(data, wrap=\"all\", indent=\" \")\n\nOutput\n------\n\n.. code-block:: xml\n\n <all>\n <a>1</a>\n <b>2</b>\n <b>3</b>\n <c>\n <d>\n <p>9</p>\n </d>\n <d>\n <o>10</o>\n </d>\n <e>7</e>\n </c>\n </all>\n\nmethods\n-------\n\n``dict2xml.dict2xml(data, *args, **kwargs)``\n Equivalent to:\n\n .. code-block:: python\n\n dict2xml.Converter(*args, **kwargs).build(data)\n\n``dict2xml.Converter(wrap=\"\", indent=\" \", newlines=True)``\n Knows how to convert a dictionary into an xml string\n\n * wrap: Wraps the entire tree in this tag\n * indent: Amount to prefix each line for each level of nesting\n * newlines: Whether or not to use newlines\n\n``dict2xml.Converter.build(data, iterables_repeat_wrap=True, closed_tags_for=None, data_sorter=None)``\n Instance method on Converter that takes in the data and creates the xml string\n\n * iterables_repeat_wrap - when false the key the array is in will be repeated\n * closed_tags_for - an array of values that will produce self closing tags\n * data_sorter - an object as explained below for sorting keys in maps\n\n``dict2xml.DataSorter``\n An object used to determine the sorting of keys for a map of data.\n\n By default an ``OrderedDict`` object will not have it's keys sorted, but any\n other type of mapping will.\n\n It can be made so even ``OrderedDict`` will get sorted by passing in\n ``data_sorter=DataSorter.always()``.\n\n Or it can be made so that keys are produced from the sorting determined by\n the mapping with ``data_sorter=DataSorter.never()``.\n\n .. note:: When this library was first created python did not have deterministic\n sorting for normal dictionaries which is why default everything gets sorted but\n ``OrderedDict`` do not.\n\n To create custom sorting logic requires an object that has a single ``keys_from``\n method on it that accepts a map of data and returns a list of strings, where only\n the keys that appear in the list will go into the output and those keys must exist\n in the original mapping.\n\nSelf closing tags\n-----------------\n\nTo produce self closing tags (like ``<item/>``) then the ``build`` method must\nbe given a list of values under ``closed_tags_for``. For example, if you want\n``None`` to produce a closing tag then:\n\n.. code-block:: python\n\n example = {\n \"item1\": None,\n \"item2\": {\"string1\": \"\", \"string2\": None},\n \"item3\": \"special\",\n }\n\n result = Converter(\"\").build(example, closed_tags_for=[None])\n assert result == dedent(\"\"\"\n <item1/>\n <item2>\n <string1></string1>\n <string2/>\n </item2>\n <item3>special</item3>\n \"\"\").strip())\n\nHere only ``string2`` gets a self closing tag because it has data of ``None``,\nwhich has been designated as special.\n\nIf you want to dynamically work out which tags should be self closing then you\nmay provide an object that implements ``__eq__`` and do your logic there.\n\nLimitations\n-----------\n\n* No attributes on elements\n* Currently no explicit way to hook into how to cope with your custom data\n* Currently no way to insert an xml declaration line\n\nChangelog\n---------\n\n1.7.6 - 8 August 2024\n * Fixed the ``dict2xml.dict2xml`` entry point to distribute options\n correctly\n\n1.7.5 - 13 February 2024\n * Introduced the ``data_sorter`` option\n\n1.7.4 - 16 January 2024\n * Make the tests compatible with pytest8\n\n1.7.3 - 25 Feb 2023\n * This version has no changes to the installed code.\n * This release converts to hatch for packaging and adds a wheel to the\n package on pypi.\n * CI will now run against python 3.11 as well\n\n1.7.2 - 18 Oct 2022\n * This version has no changes to the installed code.\n * This release adds the tests to the source distribution put onto pypi.\n\n1.7.1 - 16 Feb 2022\n * Adding an option to have self closing tags when the value for that\n tag equals certain values\n\n1.7.0 - 16 April, 2020\n * Use collections.abc to avoid deprecation warning. Thanks @mangin.\n * This library no longer supports Python2 and is only supported for\n Python3.6+. Note that the library should still work in Python3.5 as I\n have not used f-strings, but the framework I use for the tests is only 3.6+.\n\n1.6.1 - August 27, 2019\n * Include readme and LICENSE in the package\n\n1.6 - April 27, 2018\n * No code changes\n * changed the licence to MIT\n * Added more metadata to pypi\n * Enabled travis ci\n * Updated the tests slightly\n\n1.5\n * No changelog was kept before this point.\n\nDevelopment\n-----------\n\nTo enter a virtualenv with dict2xml and dev requirements installed run::\n\n > source run.sh activate\n\nTests may be run with::\n\n > ./test.sh \n\nOr::\n\n > ./run.sh tox\n\nLinting and formatting is via::\n\n > ./format\n > ./lint\n\nPython Black will work on the tests as long as ``NOSE_OF_YETI_BLACK_COMPAT=true``\nand the correct version of black is available. This is true if your editor\nis opened in the same terminal session after sourcing run.sh or if\nyou make sure that environment variable is set and the editor is using the\nvirtualenv made by running or sourcing ``run.sh`` (``tools/venv/.python``)\n",
"bugtrack_url": null,
"license": null,
"summary": "Small utility to convert a python dictionary into an XML string",
"version": "1.7.6",
"project_urls": {
"Homepage": "http://github.com/delfick/python-dict2xml"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "32ad5a193b6461c713058e3c5b6dbec7a9f92230a968650129cc46f32d3c9de2",
"md5": "678a3ec57371b07136d185da7b4709e3",
"sha256": "841a0c1720e4bfa121e958b805f1062fccf5af2970e7a1f81d7fa056f49e5065"
},
"downloads": -1,
"filename": "dict2xml-1.7.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "678a3ec57371b07136d185da7b4709e3",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.5",
"size": 8038,
"upload_time": "2024-08-08T01:12:11",
"upload_time_iso_8601": "2024-08-08T01:12:11.725724Z",
"url": "https://files.pythonhosted.org/packages/32/ad/5a193b6461c713058e3c5b6dbec7a9f92230a968650129cc46f32d3c9de2/dict2xml-1.7.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "07b32db1b077f9a80d30d1729a0af69b7cef8ce5b45c77a1f0f9ebb01b9edce1",
"md5": "81bd352a1a7dc7226ce42deed9487823",
"sha256": "3e4811f4ef7fca86dede6acf382268ff9bc5735a4aa0e21b465f6eb0c4e81732"
},
"downloads": -1,
"filename": "dict2xml-1.7.6.tar.gz",
"has_sig": false,
"md5_digest": "81bd352a1a7dc7226ce42deed9487823",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.5",
"size": 15210,
"upload_time": "2024-08-08T01:12:13",
"upload_time_iso_8601": "2024-08-08T01:12:13.291726Z",
"url": "https://files.pythonhosted.org/packages/07/b3/2db1b077f9a80d30d1729a0af69b7cef8ce5b45c77a1f0f9ebb01b9edce1/dict2xml-1.7.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-08 01:12:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "delfick",
"github_project": "python-dict2xml",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "dict2xml"
}