467,893 Members | 1,876 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,893 developers. It's quick & easy.

Listing functions in a file IN ORDER

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?

Jul 18 '05 #1
  • viewed: 1684
Share:
6 Replies
Ian Sparks 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 :

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
Jul 18 '05 #2
Ian Sparks 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 :

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

Jul 18 '05 #3
On Tue, 29 Jun 2004, Irmen de Jong wrote:
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'.

Jul 18 '05 #4
In article <ma**************************************@python.o rg>,
Ian Sparks <Ia********@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 --
-- du****@grisby.org --
-- http://www.grisby.org --
Jul 18 '05 #5
Ian Sparks 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.


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.


Jul 18 '05 #6
Istvan Albert wrote:
Ian Sparks wrote:
...python file with functions named doXXX ... order executed important...
I want the system to do code discovery because I'm too dumb to
remember to put things in the list.


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)

--
-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Kim Jensen | last post: by
13 posts views Thread by I. Dancay | last post: by
62 posts views Thread by Juuso Hukkanen | last post: by
2 posts views Thread by Juuso Hukkanen | last post: by
4 posts views Thread by dnfertitta | last post: by
9 posts views Thread by Ramashish Baranwal | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.