PyMeld and PyMeldLite (was Re: Separation of content and code for web)

Richie Hindle richie at entrian.com
Tue Dec 2 12:06:34 EST 2003


Peter,

> The reason that license wouldn't be suitable for us is that we build
> embedded web server interfaces into some of our products, allowing them
> to be monitored and configured remotely via the web.  Very nice way to
> go, but of course it would by definition require us to be able to 
> redistribute the code.  It's not just going to be in our own servers,
> but in effect installed on servers that we sell to others.

Sure, I see.  (A worldwide PyMeld license is a flat $300, BTW.)

> Very interesting... we built ours around a sort of ElementTree-lite that
> we whipped up -- which is based on the expat parser, and which therefore 
> also handles only valid X[HT]ML as well.

You've written PyMeldLite exactly.  Get out of my head!  From the source:

## Super-lightweight DOM-like tree.
##
## The externally-visible `Meld` class is just a thin wrapper around a
## lightweight DOM-like tree.  The classes `_Node`, `_RootNode`,
## `_ElementNode` and `_TextNode` implement the tree, and `_TreeGenerator`
## generates it from XML source.

Mine uses expat in recent Python versions, and xmllib in older versions.

> you've done things like make
> items with "id" attributes available as named attributes of the PyMeld
> object, whereas we concluded that since an id could contain things that
> weren't legal in Python names, we should stick with a dictionary approach
> instead.

Yes.  `element.class = something` is an example that one PyMeld user ran
into.  You need to say `setattr(element, 'class', something)`, which is
messy.  Adding a dictionary-like interface alongside the attribute-based
one is a six-ish-line edit:

    def __getitem__(self, name):  # XXX ...and setdefault...
        return getattr(self, name)

    def __setitem__(self, name, value):
        setattr(self, name, value)

    def __delitem__(self, name):
        delattr(self, name)

I've smoke-tested that and it seems to work, but I'd need to write some
extra unit tests before I was sure.

[ The attribute-based system results in so much neater-looking code - one
  character of line noise in `element.attribute` vs. four in
  `element['attribute']` - that I decided I'd rather write HTML with
  Python-compatible id names.  The odd unavoidable exception like 'class'
  I can put up with.  But that's my personal opinion - PyMeld[Lite] should
  work both ways. ]

-- 
Richie Hindle
richie at entrian.com






More information about the Python-list mailing list