Skip to main content

Create Python objects from XML using decorated functions to parse the nodes

Project description

xml_objects

Create Python objects from XML using decorated functions to parse the nodes.

Usage

Basic Parsers

This package relies on the creation of "parsers" to parse "nodes".

Consider this XML:

<root>
<title>Tis is a title</title>
<text>This is some text</text>
</root>

Three nodes are present: root, title, and text. All of these must be coded in Python:

from xml_objects import Builder, no_parent

builder = Builder()


@builder.parser('root')
def root_node(parent, text):
    """This is the root node, so parent should be no_parent."""
    assert parent is no_parent
    print('Parsing has begun.')


@builder.parser('title')
def show_title(parent, text):
    """Because nothing was returned by the root parser, parent should be None."""
    assert parent is None
    print('Title: %s' % text)
    return text


@builder.parser('text')
def show_text(parent, text):
    """Because we returned title from the title parser, we can show it now too."""
    print('%s -> %s' % (parent, text))

With setup now complete, we could use the from_string method of Builder to print the messages, like so:

builder.from_string(xml)

Attributes

Attributes can be used to provide keyword arguments to your parsers. Consider this example node from the Flask example:

<app name="flask-app" host="0.0.0.0" port="8000">
...
</app>```

The app parser in `xml_objects/ext/flask.py` expects `name`, `host`, and `port` as keyword arguments, and provides sensible defaults accordingly:

@builder.parser('app') def get_app(parent, text, name=name, host='0.0.0.0', port='4000'): """Returns a flask app.""" if parent is not no_parent: raise RuntimeError( 'This must be the top-level tag.\nparent: %r' % parent ) app = Flask(name) app.config['HOST'] = host app.config['PORT'] = int(port) return app


### Storage
While you could use xml_objects to build GUIs with [wxPython](https://wxpython.org/) for example, xml_objects itself doesn't provide any mechanism to store state, so this must be done by the programmer.

For examples, see `xml_objects/ext/wx.py`.

### Iterators
Sometimes it may be desirable to run code after you have created an object, and all child tags have been processed. For this reason, it is possible to use generators in your parsers:

@builder.parser('frame') def get_frame(parent, text): """Create a frame, then finalise it when everyone else has finished with it.""" f = Frame() yield f f.finalise()


If you yield more than once, RuntimeError will be raised.



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

xml_python-0.3.0.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

xml_python-0.3.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file xml_python-0.3.0.tar.gz.

File metadata

  • Download URL: xml_python-0.3.0.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.8.0

File hashes

Hashes for xml_python-0.3.0.tar.gz
Algorithm Hash digest
SHA256 dc6e6d84c46f676a36dcf278a5cd8449e8e3ffa3ea1254bd7e985e8ee9f4b512
MD5 b09992f5097f5cba93cb500be1934e8f
BLAKE2b-256 06e2dea3c537063ab801c30c8775f4a26bd1c839e20acd5e7e8a7f311508020d

See more details on using hashes here.

File details

Details for the file xml_python-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: xml_python-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.1 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.8.0

File hashes

Hashes for xml_python-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2303bc4c861d9ce3893e48c398d4608aa6ef0144126e04324ace9cff8860823a
MD5 4ff60eb6a7beae5ed2af1ab8093892c5
BLAKE2b-256 672fdb161ca1d539e8a5818dca0dd1e59beb4a97a86f57e05f3bb130c9d85fc0

See more details on using hashes here.

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