« Twisted logging | Main | Learning about the twisted trial unittesting framework »

January 06, 2003

Do not read this.

Do not read this.

from __future__ import generators


class Text(str):
    def __init__(self, it):
        self.it = it
    def __str__(self):
        return self.it


def coerceBasic(item):
    if isinstance(item, str):
        item = Text(item)
    elif isinstance(item, tuple):
        item = lmx2(item)
    assert isinstance(item, Text) or 
           isinstance(item, lmx2), "wtf did you add %s to me for?" % item
    return item


class lmx2(object):
    def __init__(self, argument, **kw):
        if isinstance(argument, str):
            self._name = argument
            self._children = []
            self._attributes = kw
        else:
            assert not kw, "Either use a fourple or a tag name and set of keyword arguments"
            self._name, self._attributes, self._children, spare = argument
    def __lshift__(self, other):
        self._children.append(other)
        return self
    def __rshift__(self, other):
        other.__dict__ = self.__dict__
        other.__class__ = self.__class__
        return other
    def _gen(self):
        for item in self._children:
            yield coerceBasic(item)
    def __iter__(self):
        return self._gen()
    def __getitem__(self, item):
        if isinstance(item, int):
            return coerceBasic(self._children[item])
        return self._attributes[item]
    def __setitem__(self, item, value):
        if isinstance(item, int):
            self._children[item] = value
        else:
            self._attributes[item] = value
    def __delitem__(self, item):
        if isinstance(item, int):
            del self._children[item]
        else:
            del self._attributes[item]
    def __getattr__(self, name):
        if name[0] == '_':
            raise AttributeError("no private attrs")
        return lambda **kw: self._children.append(lmx2(name, **kw))
    def __str__(self):
        return ('<%s ' % self._name) + ' '.join(['%s="%s"' % (key, value) 
        for key, value in self._attributes.items()]) + ('>...' % self._name)

Man, I _told_ you not to read this. Why you always trippin like that?

Posted by Donovan at 18:01:35


Comments

Leave a Comment