Write XML with Pythonic Code
Project description
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"
]
]
]
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
py2xml-1.0.5.tar.gz
(4.6 kB
view details)
File details
Details for the file py2xml-1.0.5.tar.gz
.
File metadata
- Download URL: py2xml-1.0.5.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4fe63f16f18a0929554c3c45905d39b17568533229168efee27343aabb404e1e |
|
MD5 | 95d4487acbb23112a778c3d02775e3e2 |
|
BLAKE2b-256 | 6624d78a3ff0e931780facd8af48b0b622e0323ea217bc5cba8033c32b063c91 |