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 details)

Uploaded Source

Built Distribution

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

Uploaded Source

File details

Details for the file magictree-1.0.0.zip.

File metadata

  • Download URL: magictree-1.0.0.zip
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for magictree-1.0.0.zip
Algorithm Hash digest
SHA256 4b1c5817378347f0b94b78ac138e6ae86342895f9f45e69afe9cd1b13f2877d6
MD5 685e25d6dc37cf306eeccf7ab1815c73
BLAKE2b-256 10cfb3d48d02d7e673fb4ff28ca4c96743ac710dc70180943995b5d3420efbc0

See more details on using hashes here.

File details

Details for the file magictree-1.0.0.win32.exe.

File metadata

  • Download URL: magictree-1.0.0.win32.exe
  • Upload date:
  • Size: 65.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for magictree-1.0.0.win32.exe
Algorithm Hash digest
SHA256 16a57095f24c846aa2a30cb1bf64aa93224e4b9438079a218e6c663f03bf555d
MD5 6ab5c6cca99b2dd2cd2f5f887f9734c5
BLAKE2b-256 a2ba3580c3dccff3323b2db4a9dfc67da810c9fd5273ef2a3902827b7ffc19df

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page