Re: [Tutor] listing classes
On Wed, May 21, 2008 at 10:41 PM, Laureano Arcanio [EMAIL PROTECTED] wrote: 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 The *nested* classes need a metaclass that keeps track of order. At the point of creation of the nested class object, you can add the object to a list. I think you can do this with a metaclass... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] listing classes
well it's true, using list it's scalable too. But It's doesn't looks friendly to the user to write the document. Syntacticly looks nice to keep some of the original structure of the html ( I mind, put the thags inside the document, and so on ). I'll making some test this days to see what i get, and i keep on mind your suggestions. Thanks for all your feedback ! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] listing classes
sorry this is not true: There is a think left, i can't jus compare this: if type(somethingA) == type(somthingB): I transform type() to a string and then compare them.. (like in the code below) Thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] listing classes
I'm using the dir() function, but this give me an alphabetic ordered list, is there any way to do the same but getting an appearance ordered list ?. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] listing classes
On Wed, May 21, 2008 at 7:45 PM, Laureano Arcanio [EMAIL PROTECTED] wrote: I'm using the dir() function, but this give me an alphabetic ordered list, is there any way to do the same but getting an appearance ordered list ?. Not easily. Attributes are stored in a dict; dicts don't preserve order. You would have to make a custom metaclass that remembered the order. What is it you are trying to accomplish? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] listing classes
Hi, dir(A) will essentially give you what you want (and a little more) If you're only interested in classes, you can do something like: import types [ name for name in dir(A) if type(eval('A.'+name)) == types.ClassType ] Thomas Laureano Arcanio wrote: Hi All, I need to have a listing of all classes defined inside a class body, something like this: class A(object): class B(object): pass class C(object): pass(object): and i need to get the classes to instantiate them.. something like this. classes =[A,B] Any ideas ? do i need meta classes or something ? Thanks in advice. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] listing classes
Thomas, import types [ name for name in dir(A) if type(eval('A.'+name)) == types.ClassType ] The == types.ClassType doesn't seem to pick out the classes. Also, I think you should be returning eval( name ) vs. name so that the OP gets a list of objects vs. names? (My take on what the poster wanted). Malcolm ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] listing classes
On Tue, May 20, 2008 at 12:47 PM, Thomas Pani [EMAIL PROTECTED] wrote: Hi, dir(A) will essentially give you what you want (and a little more) If you're only interested in classes, you can do something like: import types [ name for name in dir(A) if type(eval('A.'+name)) == types.ClassType ] There is no need to use eval() here. Python has powerful introspection capabilities - use getattr() to get a named attribute of an object. For example: In [1]: class A: ...: class B: ...: pass ...: class C: ...: pass In [2]: dir(A) Out[2]: ['B', 'C', '__doc__', '__module__'] In [3]: type(A.B) Out[3]: type 'classobj' In [4]: type(A) Out[4]: type 'classobj' In [5]: [ name for name in dir(A) if type(getattr(A, name))==type(A) ] Out[5]: ['B', 'C'] Note: types.ClassObj is the type of old-style classes. The OP used new-style classes which are of type type. Using type(A) for the comparison means it will work with either kind of classes as long as they are the same. You could also use inspect.isclass() to decide if it is a class. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] listing classes
Laureano Arcanio [EMAIL PROTECTED] wrote I need to have a listing of all classes defined inside a class body, something like this: class A(object): class B(object): pass class C(object): pass(object): Others have answered but I'm curious why you would want to have such a structure. Defining nested classes is a fairly unusual construct only used under fairly specific conditions. It also limits reuse opportunities and flexibility quite a bit. Is there any reason why you can't just define the classes individually and put them in a list or dictionary? That would be much simpler. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] listing classes
On Tue, May 20, 2008 at 01:48:03PM -0400, Kent Johnson wrote: Note: types.ClassObj is the type of old-style classes. The OP used new-style classes which are of type type. Using type(A) for the comparison means it will work with either kind of classes as long as they are the same. You could also use inspect.isclass() to decide if it is a class. Using the inspect module sounds like a good suggestion to me. I'm wondering why we don't use the inspect module all the way. Here is a bit of code: import inspect # Import the module containing the classes we want to list. import test_inspect_data def test(): outer_classes = inspect.getmembers(test_inspect_data, inspect.isclass) print outer_classes for name, class_ in outer_classes: inner_classes = inspect.getmembers(class_, inspect.isclass) print inner_classes if __name__ == '__main__': test() Which prints out: [('A', class 'test_inspect_data.A')] [('B', class 'test_inspect_data.B'), ('C', class 'test_inspect_data.C'), ('__class__', type 'type')] - Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor