Skip to main content

Writing XML with Pythonic code

https://img.shields.io/github/license/mashape/apistatus.svg https://badge.fury.io/py/py2xml.svg

Installation

$ pip install py2xml

Usage

import py2xml

# typing attributes
@py2xml.to_element
def note(id: int): ...


messages = py2xml.Element("messages")
to = py2xml.Element("to")
from0 = py2xml.Element("from")
heading = py2xml.Element("heading")
body = py2xml.Element("body")
print(
    messages[
        note(id=501)[
            to[
                "Tove"
            ],
            from0[
                "Jani"
            ],
            heading[
                "Reminder"
            ],
            body[
                "Don't forget me this weekend!"
            ]
        ],
        note(id=502)[
            to[
                "Jani"
            ],
            from0[
                "Tove"
            ],
            heading[
                "Re: Reminder"
            ],
            body[
                "I will not"
            ]
        ]
    ]
)

<messages><note id=”501”><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don’t forget me this weekend!</body></note><note id=”502”><to>Jani</to><from>Tove</from><heading>Re: Reminder</heading><body>I will not</body></note></messages>

to xml ElementTree

Element.to_xml()

generate code

import py2xml

print(py2xml.util.generate_code('''<messages>
<note id="501">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>
<note id="502">
    <to>Jani</to>
    <from>Tove</from>
    <heading>Re: Reminder</heading>
    <body>I will not</body>
</note>
</messages>'''))
messages = py2xml.Element("messages")
note = py2xml.Element("note")
to = py2xml.Element("to")
from0 = py2xml.Element("from")
heading = py2xml.Element("heading")
body = py2xml.Element("body")
messages[
note(id="501")[
to[
"Tove"
],
from0[
"Jani"
],
heading[
"Reminder"
],
body[
"Don't forget me this weekend!"
]
],
note(id="502")[
to[
"Jani"
],
from0[
"Tove"
],
heading[
"Re: Reminder"
],
body[
"I will not"
]
]
]

Metadata

Release files for py2xml 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py2xml 1.0.5
File Size Uploaded Provenance Actions
py2xml-1.0.5.tar.gz 4.6 kB Without provenance Details for py2xml-1.0.5.tar.gz

Release files / py2xml-1.0.5.tar.gz

Download URL py2xml-1.0.5.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4fe63f16f18a0929554c3c45905d39b17568533229168efee27343aabb404e1e
BLAKE2b-256 checksum
How to use checksums
6624d78a3ff0e931780facd8af48b0b622e0323ea217bc5cba8033c32b063c91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.2

Security for py2xml 1.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page