Module listing in order.

I want to get a module's contents (classes, functions and variables)
in the order in which they are declared. Using dir(module) therefore
doesn't work for me as it returns a list in alphabetical order. As an
example-

Advertisements

En Wed, 23 May 2007 04:32:42 -0300, Ramashish Baranwal
<> escribió:
> I want to get a module's contents (classes, functions and variables)
> in the order in which they are declared. Using dir(module) therefore
> doesn't work for me as it returns a list in alphabetical order. As an

Once the module is created, you can't: its namespace is a dictionary, with
no key ordering.
So you have to play with the module creation: get some kind of dictionary
that remembers insertion order, and use it as the globals argument to
__import__. (Some builtin operations require a true dictionary or use it
in a non-polimorphic way, so this may or may not work - you'll have to try
and please follow up with your findings)

Advertisements

Ramashish Baranwal wrote:
> Hi,
>
> I want to get a module's contents (classes, functions and variables)
> in the order in which they are declared. Using dir(module) therefore
> doesn't work for me as it returns a list in alphabetical order. As an
> example-
>
> # mymodule.py
> class B: pass
> class A: pass
> class D: pass
>
> # test.py
> import mymodule
> # This returns['A', 'B', 'D', '__builtins__', '__doc__', '__file__',
> '__name__']
> contents = dir(mymodule)
>
> 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?

Whatfor do you actually need this? Is it a general interest - then things
get difficult. But for certain usecases, metaclasses might come to the
rescue. But that depends on what you want to do.

Ramashish Baranwal 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.

> > 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.

Ramashish Baranwal 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.
> Nevertheless, it would be interesting to see how it can be done.
>>> import pyclbr
>>> classes = pyclbr.readmodule("mymodule")
>>> sorted(classes, key=lambda name: classes[name].lineno)
['B', 'A', 'D']

Peter Otten wrote:
> Ramashish Baranwal 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.
>>>
>
>
>> Nevertheless, it would be interesting to see how it can be done.
>>
>
>
>>>> import pyclbr
>>>> classes = pyclbr.readmodule("mymodule")
>>>> sorted(classes, key=lambda name: classes[name].lineno)
>>>>
> ['B', 'A', 'D']
>
>

Good God! Is there *anything* that python does not already do? I hardly
feel the need to write programs anymore ...
Its really 80% like of the questions that are asked here get answered
along the lines of:

Wildemar Wildenburger wrote:
> Peter Otten wrote:
>> Ramashish Baranwal 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.
>>>>
>>
>>> Nevertheless, it would be interesting to see how it can be done.
>>>
>>
>>>>> import pyclbr
>>>>> classes = pyclbr.readmodule("mymodule")
>>>>> sorted(classes, key=lambda name: classes[name].lineno)
>>>>>
>> ['B', 'A', 'D']
>>
>>
>
> Good God! Is there *anything* that python does not already do? I hardly
> feel the need to write programs anymore ...

Ramashish Baranwal <> 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.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!