Listing functions in a file IN ORDER

I

Ian Sparks

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 :

for name, func in vars(mymodule).items():
if not name.startswith("do") and callable(func):
apply(func,my_params)

but the problem is that the return from vars is not ordered the same as in the file. i.e. it could be

doOther
doTask1
doThing

The low tech solution is to use dir() and re-name the functions to sort alphabetically but perhaps there is a more elegant solution?
 
I

Irmen de Jong

Ian said:
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 :

for name, func in vars(mymodule).items():
if not name.startswith("do") and callable(func):
apply(func,my_params)

but the problem is that the return from vars is not ordered the same as in the file. i.e. it could be

Just add a list which sums up the function names in the order you want:

functions = ["doTask1", "doThing", "doOther"......]

and iterate over that list?

--Irmen
 
P

Peter Otten

Ian said:
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 :

for name, func in vars(mymodule).items():
if not name.startswith("do") and callable(func):
apply(func,my_params)

but the problem is that the return from vars is not ordered the same as in
the file. i.e. it could be

doOther
doTask1
doThing

The low tech solution is to use dir() and re-name the functions to sort
alphabetically but perhaps there is a more elegant solution?

The following minimal code will break with methods and nested functions.

<xyz.py>
def z(): print "ZZZ"
def y(): print "YYY"
def x(): print "XXX"
</xyz.py>

<runxyz.py>
import compiler

class Visitor:
def __init__(self, module, *args):
self.module = module
self.args = args
def visitFunction(self, node):
getattr(self.module, node.name)(*self.args)

def callInOrder(module, *args):
ast = compiler.parseFile(module.__file__)
compiler.walk(ast, Visitor(module, *args))

if __name__ == "__main__":
import xyz
callInOrder(xyz)
</runxyz.py>

Not particularly elegant, but crazy enough to be worth posting...

Peter
 
C

Christopher T King

Ian Sparks wrote:

Just add a list which sums up the function names in the order you want:

functions = ["doTask1", "doThing", "doOther"......]

and iterate over that list?

Better yet, just have a list of the functions themselves:

functions = [doTask1, doThing, doOther]

for function in functions:
function(*args)

Note the use of function(*args) instead of apply(function,args). apply()
is deprecated starting with Python 2.3 in favor of this 'extended call
syntax'.
 
D

Duncan Grisby

Ian Sparks said:
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.
 
I

Istvan Albert

Ian said:
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.

IMHO making the order in which the functions are defined in a module
define the order in which they will be called upon in another program
seems to be an awkward solution.

You are encoding program logic into layout information thus
solving the problem of 'forgetting about a function' by creating
a more devious one.

Istvan.
 
S

Scott David Daniels

Istvan said:
IMHO making the order in which the functions are defined in a module
define the order in which they will be called upon in another program
seems to be an awkward solution.
I agree. Separate the two concerns:

import sets
chosen = sets.Set(module.function_list)
actuals = sets.Set([function for name, function in vars(module)
if name.startswith('do') and callable(function))])
for function in actuals - chosen:
print >>stderr, 'Warning! forgot about', function.__name__
# now call them in the specified order.
for function in function_list:
function(*args)
 

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,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top