Skip to main content

Easily create ElementTree with automatic Element factories

Project description

Creating tree structures like those used for HTML and XML should be dead easy. The xml.etree.ElementTree library goes quite far in creating a simple to use library for creating and modifying such structures. I’d like to go a step further, building on top of ElementTree:

from magictree import html, head, title, body, h1, p
doc = html(
        head(
          title('Chapter 1: Greeting')),
        body(
          h1('Chapter 1: Greeting'),
          p('Hello, world!')))

from xml.etree import ElementTree as et
et.dump(doc)

Results in this: (added some whitespace for formatting)

<html>
  <head>
    <title>Chapter 1: Greeting</title>
  </head>
  <body>
    <h1>Chapter 1: Greeting</h1>
    <p>Hello, world!</p>
  </body>
</html>

This works by replacing this module with a wrapper object in sys.modules that creates factory functions for elements based on their name.

I used this page as a basis for the hack: http://stackoverflow.com/questions/2447353/getattr-on-a-module

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

magictree-1.0.0.zip (3.8 kB view hashes)

Uploaded Source

Built Distribution

magictree-1.0.0.win32.exe (65.2 kB view hashes)

Uploaded Source

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