473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
6 1896
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Kim Jensen | last post by:
I'd like to make a directory listing where instead of the entire filename I need it to show the filename minus the extention and get the value of charname= in the file itself. I've been told...
13
by: I. Dancay | last post by:
Can someone help me work out this project? I need someone who is a C++ expert to help me work this one out. Here it is below. implement an abstract data > type (ADT) Student as a C++ class....
10
by: ibic | last post by:
Just curious: is it possible to recursively list all the directorys/files inside a given directory using standard c i/o library routines only, which can be re-compiled and run on any os supportes c...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
3
by: Arpi Jakab | last post by:
I have a main project that depends on projects A and B. The main project's additional include directories list is: ...\ProjectA\Dist\Include ...\ProjectB\Dist\Include Each of the include...
62
by: Juuso Hukkanen | last post by:
I am looking for a wish list of things which should be removed from the C (C99) - due to feature's bad security track record <OT>or Multithreading unsafety. I need this list for a project intending...
2
by: Juuso Hukkanen | last post by:
I need a list of multithreading unsafe C (C99) functions/features. comp.programming.threads provided an initial list of C:ish functions, with following ANSI C functions: asctime, gmtime,...
4
by: dnfertitta | last post by:
This is my task and I'd like some direction please: 1. Print company that houses user PDF files 2. User logs in and is brought to his own area 3. Dynamic customer inventory page is...
9
by: Ramashish Baranwal | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.