By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,932 Members | 1,944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,932 IT Pros & Developers. It's quick & easy.

Module listing in order.

P: n/a
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?

Thanks,
Ram

May 23 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
En Wed, 23 May 2007 04:32:42 -0300, Ramashish Baranwal
<ra*************@gmail.comescribió:
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)

--
Gabriel Genellina

May 23 '07 #2

P: n/a
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.

Diez
May 23 '07 #3

P: n/a
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?

W
May 23 '07 #4

P: n/a
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.

Nevertheless, it would be interesting to see how it can be done.:)

-Ram

May 25 '07 #5

P: n/a
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
May 25 '07 #6

P: n/a
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:

import some_fancy_module

solution = some_fancy_module.exactly_the_right_function_to_so lve(problem)

Kinda scary ... :)
W
May 26 '07 #7

P: n/a
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 ...
+1 QOTW
Its really 80% like of the questions that are asked here get answered
along the lines of:

import some_fancy_module

solution = some_fancy_module.exactly_the_right_function_to_so lve(problem)

Kinda scary ... :)
And you haven't seen the time machine working yet ...

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 26 '07 #8

P: n/a
In article <ma***************************************@python. org>,
Wildemar Wildenburger <wi******@freakmail.dewrote:
May 26 '07 #9

P: n/a
Ramashish Baranwal <ra*************@gmail.comwrote:
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 <ni**@craig-wood.com-- http://www.craig-wood.com/nick
May 29 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.