[FRPythoneers] Porting perl to python

Jack Diederich jack at performancedrivers.com
Fri Jul 11 12:37:01 MDT 2003

On Fri, Jul 11, 2003 at 11:32:15AM -0600, Erick Bodine wrote:
> In the python version ideally I would import a separate module that
> would contain all of the present and any future functions and then run
> all the functions contained in that module.
> I have tried various manipulations using the dir() and callable()
> builtins to no avail.  I think that 'recipe' 15.3 in the Python cookbook
> might contain some clues (some manipulation around __import__) but I am
> not really familiar w/ how this works in Python.

A simple way would be to name all the functions like 'test_' in the
module and then look for them using dir()

import my_test_module

for (name) in dir(my_test_module):
  if (name.startswith('test_')):
    func = getattr(my_test_module, name) # or my_test_module.__dict__[name]
    func() # and run it

A better idea is to check out the unittest module (modeled after JUnit,
sometimes referred to as PyUnit).  It is a little heavier than what you need
but if you get in the habit of writing tests for everything your life will
get better & simpler _fast_.


More information about the FRPythoneers mailing list