[Tutor] Fwd: listing classes

W W srilyk at gmail.com
Thu May 22 13:38:32 CEST 2008


I don't know if this is the best solution, but what I usually do is
create a list that matches the key:

mydict = {}
mylist = []
for x in range(1, 10):
    key = raw_input("Enter the key: ")
    mydict[key] = value
    mylist.append(key)

You just have to change it to read from a file/hard code all the tags
or what not.

HTH,
Wayne

On Thu, May 22, 2008 at 5:07 AM, Kent Johnson <kent37 at tds.net> wrote:
> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi


More information about the Tutor mailing list