Listing functions in a file IN ORDER

Duncan Grisby duncan-news at grisby.org
Wed Jun 30 10:43:06 EDT 2004


In article <mailman.278.1088520506.27577.python-list at python.org>,
Ian Sparks <Ian.Sparks at etrials.com> wrote:

>I have a python file with a number of functions named with the form doX so :
>
>doTask1
>doThing
>doOther
>
>The order these are executed in is important and I want them to be
>executed top-down. They all have the same parameter signature so I'd
>like to do :

All you need to do is sort them by line number:

  from types import FunctionType

  def linecompare(a,b):
      return cmp(a.func_code.co_firstlineno, b.func_code.co_firstlineno)

  func_list = [ f for (n,f) in vars(mymodule).items()
                if isinstance(f, FunctionType) and n.startswith("do") ]

  func_list.sort(linecompare)


Notice that I looked just for functions since other callables like
classes don't have the right attributes.

Cheers,

Duncan.

-- 
 -- Duncan Grisby         --
  -- duncan at grisby.org     --
   -- http://www.grisby.org --



More information about the Python-list mailing list