Module listing in order.

R

Ramashish Baranwal

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
 
G

Gabriel Genellina

En Wed, 23 May 2007 04:32:42 -0300, Ramashish Baranwal
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)
 
D

Diez B. Roggisch

Ramashish said:
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
 
W

Wildemar Wildenburger

Ramashish said:
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
 
R

Ramashish Baranwal

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
 
P

Peter Otten

Ramashish said:
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
 
W

Wildemar Wildenburger

Peter said:
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_solve(problem)



Kinda scary ... :)
W
 
S

Steve Holden

Wildemar said:
Peter said:
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_solve(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 ----------------
 
C

Cameron Laird

.
.
.
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_solve(problem)



Kinda scary ... :)
W

I can tell you that one of my personal goals is to make it
so that most posts to comp.lang.python can be adequately
answered by

"See <URL: http://wiki.python.org/moin/$YourQuestion >."

Then we just concentrate on elaboration of some_fancy_module
and its Wiki correspondents, and clp manages itself.
 
N

Nick Craig-Wood

Ramashish Baranwal said:
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'>]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top