Module listing in order.

Discussion in 'Python' started by Ramashish Baranwal, May 23, 2007.

  1. 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
     
    Ramashish Baranwal, May 23, 2007
    #1
    1. Advertising

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

    --
    Gabriel Genellina
     
    Gabriel Genellina, May 23, 2007
    #2
    1. Advertising

  3. 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
     
    Diez B. Roggisch, May 23, 2007
    #3
  4. 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
     
    Wildemar Wildenburger, May 23, 2007
    #4
  5. > > 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
     
    Ramashish Baranwal, May 25, 2007
    #5
  6. Ramashish Baranwal

    Peter Otten Guest

    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
     
    Peter Otten, May 25, 2007
    #6
  7. 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_solve(problem)



    Kinda scary ... :)
    W
     
    Wildemar Wildenburger, May 26, 2007
    #7
  8. Ramashish Baranwal

    Steve Holden Guest

    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_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 ----------------
     
    Steve Holden, May 26, 2007
    #8
  9. In article <>,
    Wildemar Wildenburger <> wrote:
    .
    .
    .
    >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.
     
    Cameron Laird, May 26, 2007
    #9
  10. 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.

    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 <> -- http://www.craig-wood.com/nick
     
    Nick Craig-Wood, May 29, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon

    Best way to order a listing

    Jon, Aug 11, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    338
  2. Soren Kuula
    Replies:
    2
    Views:
    541
    Soren Kuula
    Feb 1, 2004
  3. Ian Sparks

    Listing functions in a file IN ORDER

    Ian Sparks, Jun 29, 2004, in forum: Python
    Replies:
    6
    Views:
    303
    Scott David Daniels
    Jul 2, 2004
  4. Lie
    Replies:
    0
    Views:
    281
  5. Web Surfer

    How to get a listing of module functions

    Web Surfer, Jan 29, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    98
    James Willmore
    Jan 30, 2004
Loading...

Share This Page