[Tutor] Fwd: listing classes

Kent Johnson kent37 at tds.net
Thu May 22 12:07:39 CEST 2008


Forwarding to the list.


---------- Forwarded message ----------
From: Laureano Arcanio <listas.condhor at gmail.com>
Date: Wed, May 21, 2008 at 10:41 PM
Subject: Re: [Tutor] listing classes
To: Kent Johnson <kent37 at tds.net>


I'm building a light html serialize tool, it's going to be used to
build templates on the fly for ToscaWidgets. I have it already
working, but i'm traying to make a "user friendly" way to declare Tags
and Documents. ( that with some other facilities )

So the idea of the class containing classes, it's just with that end.
Syntactic sugar let's say.

The problem comes because i need to keep the order of the HTML tags,
and as you say dict doesn't work like that.. I've working on this
metaclass, and then extend list with it, but i have the same problem,
the dct comes in a dict...

class MetaHTML(type):
    def __new__(meta, name , bases, dct):
        # Deletes methods and attributes containing "_"
        items = []
        for key, value in dct.items():
            if '_' in key:
                dct.pop(key)

        items = [tag() for tag in dct.values()]

        def __init__(self, items=items):
            self.extend(items)
        dct.update({'__slots__':[], '__init__':__init__})
        return type.__new__(meta,name,bases,dct)

class HTML(list):
        __metaclass__ = MetaHTML


I'm write this metaclass inspired in the WidgetsList that comes
shipped with toscawidgets.

I can do definitely the same using a list and just doing:

document = [A(),
                     B()]

But it's not so nice.

Any suggestion ?

Thanks


More information about the Tutor mailing list