Module listing in order.

Nick Craig-Wood nick at craig-wood.com
Tue May 29 01:33:50 EDT 2007


Ramashish Baranwal <ramashish.lists at gmail.com> wrote:
> > > I want a way to get the contents in the order of their declaration,
> > > i.e. [B, A, D]. Does anyone know a way to get it?
> >
> > My suggestion would be to actually parse the text of the module. "Brute
> > force" is what it's called ;). But doing so with, say, pyparsing
> > shouldn't be *very* difficult.
> >
> > Just out of curiosity: Why do you need the order?
> >
>  Thank you for your replies, and sorry for my late response.
> 
>  Gabriel, unfortunately I am not a python expert so don't know how to
>  play with module creation. I tried to look into __import__ function,
>  but can't see a way to get what I want.
> 
>  Wildemar, your approach seems workable. I am going to have a look at
>  it.
> 
>  Well, my requirement doesn't turn out to be an actual requirement
>  now.:) I am using a web framework Django, that lets you define classes
>  for database tables. The classes so defined can refer to other classes
>  representing db tables. It also allows you to export those table data
>  in a db-neutral format e.g. xml via the python classes so defined.
>  Exporting does not require an order, but I thought that importing the
>  data back may require data of classes which are referred by other
>  classes to be present. I just verified that its not so. So I don't
>  need to do it immediately.

Actually I had a requirement to do exactly this.  I was using python
as a definition language, making classes to define other things.  It
worked very nicely but I needed to get the classes in definition
order.

Here is how I did it with metaclasses

class _Definition_Metaclass(type):
    """
    A metaclass to add a _class_sequence attribute to each definition so we know
    which order they were defined in.
    """
    _class_sequence = 0
    def __init__(cls, name, bases, dict):
    	_class_sequence = _Definition_Metaclass._class_sequence
	_Definition_Metaclass._class_sequence += 1
        cls._class_sequence = _class_sequence

class Definition(object):
    __metaclass__ = _Definition_Metaclass

class A(Definition): pass

class B(A): pass

class C(A): pass

class D(Definition): pass

class E(C): pass

objects = []
for obj in locals().values():
    try:
	if issubclass(obj, Definition):
	    objects.append(obj)
    except TypeError: 
        pass

objects_sorted = sorted(objects, key=lambda x: x._class_sequence)

print objects

# Gives something like
# [<class '__main__.A'>, <class '__main__.Definition'>, <class
# '__main__.C'>, <class '__main__.B'>, <class '__main__.E'>, <class
# '__main__.D'>]

print objects_sorted

# Gives 
# [<class '__main__.Definition'>, <class '__main__.A'>, <class
# '__main__.B'>, <class '__main__.C'>, <class '__main__.D'>, <class
# '__main__.E'>]


-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list